Вы находитесь на странице: 1из 13

Sharmin Najreen

CSC 675

Creating the Database by using MS SQL Server 2005


create table Dept1 (
dcode varchar(3),
dept_name varchar(20),
address varchar(20),
constraint pk_dept1 primary key (dcode));

insert into Dept1 values ('123','Toy','sf');


insert into Dept1 values ('234', 'Food','la');
insert into Dept1 values ('456','Bird','sf');
insert into Dept1 values ('789', 'Food', 'sf');
insert into Dept1 values ('222', 'Shoes', 'ny');

select * from Dept1;

create table Employee (


ssn varchar(9),
name varchar(20),
phone varchar(12),
office varchar(20),
constraint pk_Employee primary key (ssn));

insert into Employee values ('123456789','Smith','5102228888', 'Toy Department');


insert into Employee values ('123456788', 'Mary','4156889000','Food Department');
insert into Employee values ('123456799','John','5105274465','Bird Department');
insert into Employee values ('123456777','Jake','5104156889','Bird Department');
insert into Employee values ('999999999', 'Psmith','4153381719','Shoe Department');
insert into Employee values ('666666666', 'Pwong','4152223333', 'Toys Department');
select * from Employee;

create table Project (


dno varchar(3),
dname varchar(20),
phone varchar(12),
ssn varchar(9),
dcode varchar(3),
constraint fk_project foreign key (dcode) references Dept1(dcode),
constraint fk_project_manager foreign key (ssn) references Employee(ssn),
constraint pk_project primary key(dno,dname));

insert into Project values ('1', 'Toy', '5104156889', '123456789', '123' );


insert into Project values('2', 'Food', '4153381111', '123456788', '234' );
insert into Project values ('3', 'Bird', '4153382222', '123456799', '456' );
insert into Project values( '4', 'Shoes','5103456778', '999999999', '222');
insert into Project values('5', 'Toys', '5104156889', '666666666', '123');

select * from Project;

create table Works_for(


dno varchar(3),
dname varchar(20),
ssn varchar(9),
percent_time varchar(5),
constraint fk_works foreign key(dno, dname) references Project(dno, dname),
constraint fk_works_ssn foreign key (ssn) references Employee(ssn),
constraint pk_works primary key(dno, dname, ssn));
1
insert into Works_for values('1', 'Toy', '123456789','100%');
insert into Works_for values('2', 'Food','123456788','80%');
insert into Works_for values('4', 'Shoes','999999999','40%');
insert into Works_for values('5', 'Toys','666666666', '60%');
insert into Works_for values('1', 'Toy', '999999999','80%');
insert into Works_for values('2', 'Food', '999999999','80%');
insert into Works_for values ('3', 'Bird', '999999999', '70%');
insert into Works_for values('5', 'Toys', '999999999','30%');

select * from Works_for;

Now Executing Professor’s given Queries in MSSQL

/*Question 1.*/

Select p.dno, p.dname


From Project p, Employee e
Where p.ssn = e.ssn AND e.name = 'Psmith';

/*2.*/

SElect e.name
From Employee e, Project p
where e.ssn = p.ssn AND p.dname = 'Toy';

/*3.*/

Select distinct p.dname


From Project p, Employee e, Works_for w
Where p.ssn = e.ssn AND e.ssn = w.ssn AND w.percent_time >= '50%';

/*4.*/

select d.dept_name
2
From Dept d, Project p, Works_for w, Employee e
Where d.dcode = p.dcode AND p.dno = w.dno AND w.ssn = e.ssn AND e.name = 'Psmith' AND
e.name = 'Pwong';

/*5.a*/
select d.dept_name
From Dept1 d, Project p, Works_for w, Employee e
Where d.dcode = p.dcode AND w.dno = p.dno AND w.ssn = e.ssn AND e.name = 'Psmith'
UNION
select d2.dept_name
From Dept1 d2, Project p2, Works_for w2, Employee e2
Where d2.dcode = p2.dcode AND w2.dno = p2.dno AND w2.ssn = e2.ssn AND e2.name =
'Pwong';

/*5b.*/

Select distinct p.dno, p.dname


From Project p, Employee e
Where p.ssn = e.ssn;

/*
6.b
*/
select distinct d.dept_name
From Dept1 d
Where d.dcode IN (SELECT p.dcode
From Project p
Where p.ssn IN (Select e.ssn
From Employee e
Where e.name = 'Psmith'));

/*10*/
select e.name
From Employee e, Works_for w, Project p, Dept1 d
Where d.dcode = p.dcode AND p.dno = w.dno AND w.ssn = e.ssn AND d.dept_name = 'Toy'
UNION
select e2.name
From Employee e2, Works_for w2, Project p2, Dept1 d2
Where d2.dcode = p2.dcode AND p2.dno = w2.dno AND w2.ssn = e2.ssn AND d2.dept_name =
'Shoes';

/*12*/
select e.name
From Employee e, Works_for w
Where e.ssn = w.ssn AND w.percent_time = (Select MAX(w2.percent_time)
From Works_for w2);

Using Oracle on SFSU’s UNIX Server:


At Libra’s UNIX server, I used a script sqll.sql to execute all home-work queries in Oracle, which is at below:
Select p.dno, p.dname
From Project p, Employee e
Where p.manager_ssn = e.ssn AND e.name = 'Psmith';

SElect e.name
From Employee e, Project p
where e.ssn = p.manager_ssn AND p.dname = 'Toy';
Select p.dname

3
From Project p, Employee e, Works_for w
Where p.manager_ssn = e.ssn AND e.ssn = w.ssn AND w.percent_time >= '50%' ;

select d.dept_name
From Dept d, Project p, Works_for w, Employee e
Where d.dcode = p.housed_dcode AND p.dno = w.dno AND w.ssn = e.ssn AND e.name =
'Psmith' AND e.name = 'Pwong';

select d.dept_name
From Dept d, Project p, Works_for w, Employee e
Where d.dcode = p.housed_dcode AND w.dno = p.dno AND w.ssn = e.ssn AND e.name = 'Psmith'
UNION
select d2.dept_name
From Dept d2, Project p2, Works_for w2, Employee e2
Where d2.dcode = p2.housed_dcode AND w2.dno = p2.dno AND w2.ssn = e2.ssn AND e2.name =
'Pwong';

select p.dno, p.dname


From Project p
Where NOT EXISTS ((Select e.ssn
From Employee e)
minus
(SElect w.ssn
From Works_for w, Employee e2
Where e2.ssn = w.ssn));

select distinct d.dept_name


From Dept d
Where d.dcode IN (SELECT p.housed_dcode
From Project p
Where p.manager_ssn IN (Select e.ssn
From Employee e
Where e.name = 'Psmith'));

select e.name
From Employee e, Works_for w, Project p, Dept d
Where d.dcode = p.housed_dcode AND p.dno = w.dno AND w.ssn = e.ssn AND d.dept_name =
'Toy'
UNION
select e2.name
From Employee e2, Works_for w2, Project p2, Dept d2
Where d2.dcode = p2.housed_dcode AND p2.dno = w2.dno AND w2.ssn = e2.ssn AND
d2.dept_name = 'Shoes';

select e.name
From Employee e, Works_for w
Where e.ssn = w.ssn AND w.percent_time = (Select MAX(w2.percent_time)
From Works_for w2);

I used the following Oracle Queries to create tables in the


UNIX Server, definetely before I have created the tables at
UNIX Server
create table Dept (
dcode varchar(3),
dept_name varchar(20),
address varchar(20),
primary key (dcode));

4
insert into Dept values ('123','Toy','sf');
insert into Dept values ('234', 'Food','la');
insert into Dept values ('456','Bird','sf');
insert into Dept values ('789', 'Food', 'sf');
insert into Dept values ('222', 'Shoes', 'ny');

select * from Dept;

create table Employee (


ssn varchar(9),
name varchar(20),
phone varchar(12),
office varchar(20),
primary key (ssn));

insert into Employee values ('123456789','Smith','5102228888', 'Toy Department');


insert into Employee values ('123456788', 'Mary','4156889000','Food Department');
insert into Employee values ('123456799','John','5105274465','Bird Department');
insert into Employee values ('123456777','Jake','5104156889','Bird Department');
insert into Employee values ('999999999', 'Psmith','4153381719','Shoe Department');
insert into Employee values ('666666666', 'Pwong','4152223333', 'Toys Department');
select * from employee;

create table Project (


dno varchar(3),
dname varchar(20),
phone varchar(12),
manager_ssn varchar(9),
housed_dcode varchar(3),
foreign key (housed_dcode) references Dept(dcode),
foreign key (manager_ssn) references Employee(ssn),
primary key(dno,dname));

insert into Project values ('1', 'Toy', '5104156889', '123456789', '123' );


insert into Project values('2', 'Food', '4153381111', '123456788', '234' );
insert into Project values ('3', 'Bird', '4153382222', '123456799', '456' );
insert into Project values( '4', 'Shoes','5103456778', '999999999', '222');
insert into Project values('5', 'Toys', '5104156889', '666666666', '123');

select * from Project;

create table Works_for(


dno varchar(3),
dname varchar(20),
ssn varchar(9),
percent_time varchar(5),
foreign key(dno, dname) references Project,
foreign key (ssn) references Employee,
primary key(dno, dname, ssn));

insert into Works_for values('1', 'Toy', '123456789','100%');


insert into Works_for values('2', 'Food','123456788','80%');
insert into Works_for values('4', 'Shoes','999999999','40%');
insert into Works_for values('5', 'Toys','666666666', '60%');
insert into Works_for values('1', 'Toy', '999999999','80%');
insert into Works_for values('2', 'Food', '999999999','80%');
insert into Works_for values ('3', 'Bird', '999999999', '70%');
insert into Works_for values('5', 'Toys', '999999999','30%');

5
select * from Works_for;
…………………

I execute s.sql and get:

SQL> @s;

Table created.

1 row created.

1 row created.

1 row created.

1 row created.

1 row created.

DCO DEPT_NAME ADDRESS


--- -------------------- --------------------
123 Toy sf
234 Food la
456 Bird sf
789 Food sf
222 Shoes ny

Table created.

1 row created.

1 row created.

1 row created.

1 row created.

1 row created.

1 row created.

SSN NAME PHONE OFFICE


--------- -------------------- ------------ --------------------
123456789 Smith 5102228888 Toy Department
123456788 Mary 4156889000 Food Department
123456799 John 5105274465 Bird Department
123456777 Jake 5104156889 Bird Department
999999999 Psmith 4153381719 Shoe Department
6
666666666 Pwong 4152223333 Toys Department

6 rows selected.

Table created.

1 row created.

1 row created.

1 row created.

1 row created.

1 row created.

DNO DNAME PHONE MANAGER_S HOU


--- -------------------- ------------ --------- ---
1 Toy 5104156889 123456789 123
2 Food 4153381111 123456788 234
3 Bird 4153382222 123456799 456
4 Shoes 5103456778 999999999 222
5 Toys 5104156889 666666666 123

Table created.

1 row created.

1 row created.

1 row created.

1 row created.

1 row created.

1 row created.

1 row created.

1 row created.

DNO DNAME SSN PERCE


--- -------------------- --------- -----
1 Toy 123456789 100%
7
2 Food 123456788 80%
4 Shoes 999999999 40%
5 Toys 666666666 60%
1 Toy 999999999 80%
2 Food 999999999 80%
3 Bird 999999999 70%
5 Toys 999999999 30%

8 rows selected.

SQL>
………………………..

The Oracle Query Result:


SQL> @sqll;

DNO DNAME
--- --------------------
4 Shoes

NAME
--------------------
Smith

DNAME
--------------------
Food
Toys
Shoes
Shoes
Shoes

no rows selected

DEPT_NAME
--------------------
Bird
Food
Shoes
Toy

no rows selected

DEPT_NAME
--------------------
Shoes

NAME
--------------------
Psmith
Pwong
Smith

8
NAME
--------------------
Mary
Psmith
Psmith

SQL>

……………………….
Note: Some queries does not give any output as there is no match, such as “fnd the names of
departments that house a project employing "Psmith" and "Pwong" as Psmith and Pwong is not employed in
same project.

Now Creating the Same Database by Using Java Program


For part 3 of home-work, tables have to be created for query to be executed. I have created these 4 tables by using
CreateData.java file. These are the 4 tables at MS SQL Server that got created.

Table Dept Table Employee

Dcode Dept_name Address Ssn Name Phone Office


123
Toy
sf

222
Shoes
ny

234
Food
la

456
Bird
sf

789
Food
sf

NULL
NULL
NULL

9
123456777
Jake
510222888
8
Bird
Departmen
t

123456788
Mary
415688900
0
Food
Departmen
t

123456789
Smith
510222888
8
Toy
Departmen
t

123456799
John
510527446
5
Bird
Departmen
t

666666666
Pwong
415222333
3
Toy
Departmen
t

999999999
Psmith
415338171
9
Shoe
Departmen
t

NULL
NULL
NULL
NULL
10
Table Project Table Works_for
Dno Dname Phone Ssn Dcode Dno Dname ssn Percent_time
1
Toy
12345
6789
100%

2
Food
12345
6788
80%

3
Bird
66666
6666
70%

4
Shoes
99999
9999
40%

5
Toy
66666
6666
60%

NULL
NULL
NULL
NULL

11
1
Toy
51041
56889
12345
6789
123

2
Food
41533
81111
12345
6788
234

3
Bird
41533
82222
12345
6799
456

4
Shoes
51034
56778
99999
9999
222

5
Toy
51041
56889
66666
6666
123

NULL
NULL
NULL
NULL
NULL

12
After that, I have executed the first query, which is “find the dno’s and dname’s of all projects that employees
named ‘Psmith’ work on”. I execute the query by executing DisplayEmployee.java file and I get the following
resultant applet:

Above is the applet output which shows the dno and dname where Psmith works.

Now Again the Same Database Got Created by Using


VBScript
For Scripting part, I have used VBScript. I’ve used the same 4 tables to execute query 2, which is “find the
names of all employees that work for a project housed in the “Toy” Department”, by VB Script. After I execute
the VB script, for result, I get the following MsgBox outputs:

The output of VBScript can also go to a text file; please see the attached hw7_script.vbs and fsoutput.txt at
home-work folder.

At the project folder, CreateData.java, DisplayEmployee.java, and hw7.vbs file is included.

13

Вам также может понравиться