Sie sind auf Seite 1von 7

/* create tables of attributes */

create table Agency (

Branch_Code VARCHAR(50) not null,

primary key(Branch_Code),

Agency_Name varchar(50) not null,

Address VARCHAR(50),

Telephone NCHAR(50),

Email VARCHAR(50),

);

create table Employee (

Employee_ID VARCHAR(50)not null,

primary key (Employee_ID),

Name VARCHAR(50) not null,

Address VARCHAR(50),

Telephone NCHAR(50),

Email VARCHAR(50),

Position VARCHAR(50),

Salary VARCHAR(50),

Branch_Code VARCHAR(50),

Department VARCHAR(50),

Hire_date DATE,

Constraint fk_branch Foreign key(Branch_Code) references Agency

);

create table Customer (

Customer_ID VARCHAR(50) not null,

primary key (Customer_ID),

Name VARCHAR(50) not null,

Address VARCHAR(50),

Telephone VARCHAR(50),

Email VARCHAR(50)

);
create table Property_Owner (

Owner_ID VARCHAR(50) not null,

primary key (Owner_ID),

Name VARCHAR(50) not null,

Address VARCHAR(50),

Telephone NCHAR(20),

Email VARCHAR(50),

);

create table Property (

Property_ID VARCHAR(50) not null,

primary key (Property_ID),

Address VARCHAR(50),

Description VARCHAR(100),

Price VARCHAR(50),

Owner_ID VARCHAR(50),

Branch_Code VARCHAR(50),

Constraint fk_Owner Foreign key(Owner_ID) references Property_Owner,

Constraint fk_br Foreign key(Branch_Code) references Agency

);

create table Sales (

Sale_ID VARCHAR(50) not null,

Primary key (Sale_ID),

Property_ID VARCHAR(50),

Customer_ID VARCHAR(50),

Branch_code VARCHAR(50),

Price VARCHAR(50),

Date DATE

Constraint fk_salecs Foreign key(Customer_ID) references Customer,

Constraint fk_salebr Foreign key(Branch_Code) references Agency

);
alter table Sales

add Employee_ID varchar(50);

alter table Sales

add constraint fk_saleemp

foreign key(Employee_ID) references Employee;

/* insert records to each tables */

/* 3 records into Agency */

Insert into Agency (Branch_Code, Agency_Name, Address, Telephone, Email) values ('219-01-
1614', 'BDN Group', '3 South Place EC2M 2AF', '02048843625','bdn@constantcontact.com');

Insert into Agency (Branch_Code, Agency_Name, Address,Telephone, Email) values ('219-02-


1615','BDN Group','2 Novelis Road NP10 9NN','01633711877','bdn_02@constantcontact.com');

Insert into Agency (Branch_Code, Agency_Name, Address,Telephone, Email) values ('219-03-


1616','BDN Group','1 Tomlinson Court AL6
9GD','01707350658','bnd_03@constantcontact.com');

/* 8 records into Employee */

insert into Employee (Employee_ID, Name, Address, Telephone, Email, Position, Salary,
Branch_Code, Department, Hire_date) values ('38-0029575', 'Eileen Endrici', ' Flat 8 10
Craven Street WC2N 5PE', '02086621013', 'eendrici0@barnesandnoble.com',
'Manager','£45.000', '219-01-1614', 'Management', '26-Mar-2019');

insert into Employee (Employee_ID, Name, Address, Telephone, Email, Position, Salary,
Branch_Code, Department, Hire_date) values ('87-9925056','Waneta Bein', '16 Clifford
Court Westbourne Park Villas W2 5EE', '02043354561', 'wbein1@topsy.com', 'Administrator',
'£30.000', '219-01-1614', 'Administration', '05-Apr-2019');

insert into Employee (Employee_ID, Name, Address, Telephone, Email, Position, Salary,
Branch_Code, Department, Hire_date)values ('24-6183398', 'Corrine Hacker', 'Flat 13 Nova
Court West E14 9SA', '020 4733 4312', 'chacker2@dagondesign.com', 'Sales Agent',
'£28.000', '219-01-1614', 'Sales', '07-Apr-2019');

insert into Employee (Employee_ID, Name, Address, Telephone, Email, Position, Salary,
Branch_Code, Department, Hire_date)values ('66-2739947', 'Fedora Winckle', 'Villa Rosa
Oxhey Lane HA5 4AN', '01923454607', 'fwinckle3@sogou.com', 'Sales Agent', '£25.000',
'219-01-1614', 'Sales', '21-May-2019');

insert into Employee (Employee_ID, Name, Address, Telephone, Email, Position, Salary,
Branch_Code, Department, Hire_date) values ('19-6281699', 'John Horlick', 'The Coach
House NP15 2AU', '01873026466', 'jhorlick4@mediafire.com', 'Sales Agent', '£24.500',
'219-02-1615', 'Sales', '31-May-2019');
insert into Employee (Employee_ID, Name, Address, Telephone, Email, Position, Salary,
Branch_Code, Department, Hire_date) values ('18-2082662', 'Ertha Ruff', '16 Greenfield
Street NP24 6LG ', '01685800853','eruff5@hud.gov','Sales Agent', '£21.000', '219-02-
1615','Sales', '02-Jun-2019');

insert into Employee (Employee_ID, Name, Address, Telephone, Email, Position, Salary,
Branch_Code, Department, Hire_date)values ('40-6920219', 'Guss Bailes', '35 Robbery
Bottom Lane AL6 0TB', '01707138585', 'gbailes6@gov.uk', 'Sales Agent', '£23.000', '219-
03-1616', 'Sales', '24-Jun-2019');

insert into Employee (Employee_ID, Name, Address, Telephone, Email, Position, Salary,
Branch_Code, Department, Hire_date)values ('84-3336654', 'Ringo Pardy', '1 Coopers
Meadow, Redbourn AL3 7EY', '01442677055', 'rpardy7@techcrunch.com','Sales Agent',
'£21.000', '219-03-1616', 'Sales', '09-Jul-2019');

/* 6 records into Customer */

insert into Customer (Customer_ID, Name, Address, Telephone, Email) values ('264',
'Mariette Nardi', '22 Douglas Road, AL5 2EW', '01582 872765', 'mnardi0@ucoz.ru');

insert into Customer (Customer_ID, Name, Address, Telephone, Email) values ('356', 'Sande
Feronet', '33 Bon-Accord Crescent Lane AB11 6EH', '01224 481633',
'sferonet1@latimes.com');

insert into Customer (Customer_ID, Name, Address, Telephone, Email) values ('769',
'Jorgan Reimers', '12 Lenox Avenue CH45 5AL', '0151 5387610', 'jreimers2@vimeo.com');

insert into Customer (Customer_ID, Name, Address, Telephone, Email) values ('799',
'Karla Leftridge', '10 Cranley Gardens SM6 9PR', '01883 315445',
'kleftridge3@tuttocitta.it');

insert into Customer (Customer_ID, Name, Address, Telephone, Email) values ('108', 'Arnie
Denham', '12 Oundle Road B44 8EL', '0121 4032555', 'adenham4@rediff.com');

insert into Customer (Customer_ID, Name, Address, Telephone, Email) values ('838', 'Luce
Torrance', '30 Blackett Cottages NE20 0RR','01661 135671',
'ltorrance5@surveymonkey.com');

/* 6 records into Property_Owner */

insert into Property_Owner (Owner_ID, Name, Address,Telephone, Email) values ('859-83',


'George Winterburn', '97 King Square Gardens EC1V 8DA','020 7163 0480',
'gwinterburn0@live.com');

insert into Property_Owner (Owner_ID, Name, Address, Telephone, Email) values ('743-93',
'Taddeusz Shawcross', '16 Hampton Road IG1 1PS','01708 574417',
'tshawcross1@dyndns.org');

insert into Property_Owner (Owner_ID, Name, Address, Telephone, Email) values ('757-27',
'Florence Studdal','16-17 Bonville Road BS4 5QF','0117 078 1676',
'fstuddal2@tumblr.com');

insert into Property_Owner (Owner_ID, Name, Address, Telephone, Email) values ('652-12',
'Vernor Mossom', '63 Bradshaws AL10 9QW','01727 052763', 'vmossom3@omniture.com');
insert into Property_Owner (Owner_ID, Name, Address, Telephone, Email) values ('573-54',
'Gusty Winter', '1 Butterstile Lane M25 9PW','0161 8724000', 'gannion4@ca.gov');

insert into Property_Owner (Owner_ID, Name, Address, Telephone, Email) values ('453-
63','Isla Barnes','Coppers Sarratt Lane WD3 4AD','01923 577570',
'islyman5@columbia.edu');

/* 6 records into Property */

insert into Property (Property_ID, Address, Description , Price, Owner_ID, Branch_Code)


values ('189-51', '14 Catherine Griffiths Court EC4P 4BZ', 'for rent 2 bedroom house 1
bathroom garden','£1.800','859-83', '219-01-1614');

insert into Property (Property_ID, Address, Description , Price, Owner_ID, Branch_Code)


values ('410-89', '33 Broomhill Road IG8 9H','for sale studio flat with terrace',
'£250.000','743-93', '219-01-1614');

insert into Property (Property_ID, Address, Description , Price, Owner_ID, Branch_Code)


values ('518-13', '2 Atherston BS30 8YB','for rent 4 bedroom house 2 bathrooms driveway
and garden ','£2100','757-27', '219-03-1616');

insert into Property (Property_ID, Address, Description , Price, Owner_ID, Branch_Code)


values ('228-86', '73 St Andrews AB51 7HS','for sale 1 bedroom flat with parking
space','£165.000','652-12', '219-02-1615');

insert into Property (Property_ID, Address, Description , Price, Owner_ID, Branch_Code)


values ('204-97', '2 Kildare Road M21 0YR','for sell 2 bedroom flat 2
bathrooms','£185.000','573-54', '219-02-1615');

insert into Property (Property_ID, Address, Description , Price, Owner_ID, Branch_Code)


values ('499-42', '14 The Brow WD25 7TE','for rent 3 bedroom house 2 bathrooms
garden','£1700','453-63', '219-03-1616');

/* 6 records into Sales */

insert into Sales (Sale_ID, Property_ID, Customer_ID, Branch_Code, Price, Date,


Employee_ID) values ('474861528-1', '859-83', '264', '219-01-1614', '£1800', '24-Jun-
2019','24-6183398');

insert into Sales (Sale_ID, Property_ID, Customer_ID, Branch_Code, Price, Date,


Employee_ID) values ('502559282-8', '743-93', '356', '219-01-1614', '£250.000', '10-Apr-
2019','66-2739947');

insert into Sales (Sale_ID, Property_ID, Customer_ID, Branch_Code, Price, Date,


Employee_ID) values ('508196376-3', '757-27', '769', '219-03-1616', '£2100', '16-Aug-
2019','40-6920219');

insert into Sales (Sale_ID, Property_ID, Customer_ID, Branch_Code, Price, Date,


Employee_ID) values ('623649393-6', '652-12', '799', '219-02-1615', '£165.000', '21-Jan-
2019','19-6281699');
insert into Sales (Sale_ID, Property_ID, Customer_ID, Branch_Code, Price, Date,
Employee_ID) values ('703101970-2', '573-54', '108', '219-02-1615', '£185.000', '06-Nov-
2019','18-2082662');

insert into Sales (Sale_ID, Property_ID, Customer_ID, Branch_Code, Price, Date,


Employee_ID) values ('861513576-2', '453-63', '838', '219-03-1616', '£1700', '05-Jan-
2019','84-3336654');

/* queries */
/* 1st query */
select * from Agency

/* 2nd query */
select * from Employee

/* 3rd query */
select * from Customer

/* 4th query */
select * from Property_Owner

/* 5th query */
select * from Property

/* 6th query */
select * from Sales

/* 7th query */
select *
from Agency as A, Employee as E
where A.Branch_Code = E.Employee_ID;

/* 8th query */
ALTER TABLE Employee
ADD CHECK (Salary>=£21.000);

/* 9th query */
SELECT Customer_ID, Name, Address
FROM Customer
WHERE Address IS NULL;

/* 10th query */
SELECT Employee_ID, Name, Address
FROM Employee
WHERE Address IS NULL;

/* 11th query - update and roolback*/


Begin Transaction;
Update Property set Price=£1.950 where Property_ID=189-51;

select * from Property;

Begin Transaction;
Update Property set Price=£1.950 where Property_ID=189-51;
Rollback;

select * from Property;

/* 12th query - create user*/


create login NewLogin with password = 'abcdefg'
go
if not exists (select * from sys.database_principals Where name = N'NewAdminName')
begin
create user [NewLogin] for login [NewLogin]
exec sp_addrolemember N'db_owner',
N'NewLogin'
end;

grant select, update on dbo.employee to NewLogin

/* 13th query - commit and roolback*/


begin transaction
delete from Employee
where Salary = £28.000;
commit;

Select * from Employee

begin transaction
delete from Employee
where Salary = £28.000;
rollback;

select * from Employee

Das könnte Ihnen auch gefallen