Download Exercise 1 – SQL – Bus Depots` Database

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Entity–attribute–value model wikipedia , lookup

Concurrency control wikipedia , lookup

Database wikipedia , lookup

Oracle Database wikipedia , lookup

Microsoft Jet Database Engine wikipedia , lookup

Extensible Storage Engine wikipedia , lookup

Ingres (database) wikipedia , lookup

Microsoft SQL Server wikipedia , lookup

Open Database Connectivity wikipedia , lookup

Clusterpoint wikipedia , lookup

SQL wikipedia , lookup

Database model wikipedia , lookup

Relational model wikipedia , lookup

PL/SQL wikipedia , lookup

Transcript
SQL Exercise Solutions
Patricia Ward ‘Database Management Systems’ 2nd edition
Cengage Learning/Middlesex University Press
Exercise 1 – SQL – Bus Depots’ Database -Solutions
Refer to text book Chapter 5.
The following exercise involves writing SQL code using Oracle SQL*Plus.
You will need an Oracle account to do these exercises.
Create the database for the practical SQL and PLSQL work. You will find the
two files in the database the folder called ‘bus schema.txt’ and ‘bus data
load.txt’. Open the files and inspect them.
The first file contains Create statements to create the tables in the bus depots
database and to define primary and foreign key constraints. You should copy
and paste the contents of this file into ISQL* Plus and then execute the Create
statement – this need only be done once. The second file contains insert
statement to add rows of data into the tables created earlier. Again copy and
paste and execute once.
1.
Type ‘select * from Depot’ to inspect the contents of the Depot table.
Examine the other tables.
2.
Create table Mechanic
(mno
varchar2(5) not null,
mname
varchar2(20),
msalary
varchar2(20),
dno
varchar2(5),
constraint pk_mno primary key(mno),
constraint fk_mno foreign key(dno) references Depot(dno));
Create table MechanicBuses
(mno
varchar2(5),
regno
varchar2(10),
constraint pk_mreg primary key(mno, regno),
constraint fk_mno1 foreign key(mno) references mechanic(mno),
constraint fk_regno foreign key(regno) references bus(regno)
);
3.
Delete your table definitions.
drop table MechanicBuses;
drop table mechanic
SQL Exercise Solutions
Patricia Ward ‘Database Management Systems’ 2nd edition
Cengage Learning/Middlesex University Press
Note the order in which the tables are dropped is important.
Create table Mechanic
(mno
varchar2(5) not null,
mname
varchar2(20),
msalary
varchar2(20),
dno
varchar2(5) default '102',
constraint pk_mno primary key(mno),
constraint fk_mno foreign key(dno) references Depot(dno));
Create table MechanicBuses
(mno
varchar2(5),
regno
varchar2(10),
constraint pk_mreg primary key(mno, regno),
constraint fk_mno1 foreign key(mno) references mechanic(mno),
constraint fk_regno foreign key(regno) references bus(regno)
);
4.
alter table Mechanic
add dateEmployed date;
describe Mechanic;
Name
MNO
Null?
NOT NULL
Type
VARCHAR2(5)
MNAME
VARCHAR2(20)
MSALARY
VARCHAR2(20)
DNO
VARCHAR2(5)
DATEEMPLOYED
DATE
5.
insert into Mechanic values
('001','Carol Black',2000, '101','09-feb-2008');
etc
select * from Mechanic
SQL Exercise Solutions
Patricia Ward ‘Database Management Systems’ 2nd edition
Cengage Learning/Middlesex University Press
MNO MNAME
001
Carol Black
MSALARY DNO DATEEMPLOYED
2000
101
09-FEB-08
6.
update Mechanic
set mname = 'Carol Black'
where mname = 'Carol Brown';
select * from Mechanic
MNO
001
MNAME
Carol Brown
MSALARY
2000
DNO
101
DATEEMPLOYED
09-FEB-08
7.
update Mechanic
set msalary =msalary *1.2
where mname='Carol Brown';
select * from Mechanic
MNO
001
MNAME
Carol Brown
MSALARY
2400
DNO
101
8.
delete from MechanicBuses
where mno in
( select mno
from mechanic
where mname='Carol Brown');
delete from Mechanic
where mname='Carol Brown';
DATEEMPLOYED
09-FEB-08