Uploaded by Hassaan Ali

L181292EEA2 Lab3DBMS

advertisement
use [l181292]
create table employee
( fname varchar(15) not null,
Minit char,
Lname varchar(15) not null,
ssn char(9) not null,
bdate date,
address varchar(30),
sex char,
salary decimal(10, 2) check (salary<100000),
super_ssn
char(9),
Dno int not null,
constraint emppk primary key (ssn)
)
create table department
( Dname varchar(15) not null,
Dnumber int not null,
Mgr_ssn
char(9) ,
mgr_start_date
date,
primary key (dnumber),
unique (dname),
foreign key (mgr_ssn) references employee(ssn) on delete set null)
create table project
( Pname varchar(15) not null,
Pnumber
int not null,
Plocation varchar(15),
Dnum int not null,
constraint projkey primary key (Pnumber),
foreign key(Dnum) references department(Dnumber)
)
create table works_on
(essn char(9) not null,
Pno int not null,
hours decimal(3,1) not null,
primary key (essn, Pno),
foreign key(essn) references employee(ssn),
foreign key (Pno) references project(Pnumber)
)
create table dependent
( essn char(9) not null,
dependent_name varchar(15) not null,
sex char,
bdate date,
relationship varchar(8),
primary key (essn, dependent_name),
foreign key(essn) references employee(ssn)
)
create table dept_locations
( Dnumber int ,
dlocation varchar(20),
primary key (dnumber, dlocation),
foreign key (dnumber) references
department(dnumber)
)
Select querys
use [l181292]
select * from employee
select * from department
select * from dependent
select * from dept_locations
select * from project
select * from works_on
Post Lab:
create table book
( bookID char(3) not null,
bookName varchar(25) not null,
author char(3) not null,
primary key (author),
)
create table author
( authorID char(3) not null,
authorName varchar(25) not null,
authorAddress char(5) not null,
PRIMARY KEY(authorName),
FOREIGN KEY (authorID) REFERENCES book(author)
)
Insert values into book table
INSERT INTO book (bookID, bookName, author)
VALUES (1, 'Chamber of Secrets', 101)
INSERT INTO book (bookID, bookName, author)
VALUES (2, 'Alchemist', 102)
INSERT INTO book (bookID, bookName, author)
VALUES (3, 'Philosopher’s stone', 103)
INSERT INTO book (bookID, bookName, author)
VALUES (4, 'Heaven', 104)
INSERT INTO book (bookID, bookName, author)
VALUES (5, 'Pride and Prejudice', 105)
Insert into Author
INSERT INTO author
VALUES (101
,'J K Rowling','UK')
INSERT INTO author (authorID, authorName, authorAddress)
VALUES (102,'Paulo Coelho','Brazil')
INSERT INTO author
VALUES (103,'Neil','US')
INSERT INTO author
VALUES (104,'Jane Austen','US')
FULL CODE:
use [l181292postlab]
create table book
( bookID char(3) not null,
bookName varchar(25) not null,
author char(3) not null,
primary key (author),
)
create table author
( authorID char(3) not null,
authorName varchar(25) not null,
authorAddress char(10) not null,
PRIMARY KEY(authorName),
FOREIGN KEY (authorID) REFERENCES book(author)
)
INSERT INTO book (bookID, bookName, author)
VALUES (1, 'Chamber of Secrets', 101)
INSERT INTO book (bookID, bookName, author)
VALUES (2, 'Alchemist', 102)
INSERT INTO book (bookID, bookName, author)
VALUES (3, 'Philosopher’s stone', 103)
INSERT INTO book (bookID, bookName, author)
VALUES (4, 'Heaven', 104)
INSERT INTO book (bookID, bookName, author)
VALUES (5, 'Pride and Prejudice', 105)
INSERT INTO author
VALUES (101
,'J K Rowling','UK')
INSERT INTO author (authorID, authorName, authorAddress)
VALUES (102,'Paulo Coelho','Brazil')
INSERT INTO author
VALUES (103,'Neil','US')
INSERT INTO author
VALUES (104,'Jane Austen','US')
select * from book
select * from author
Download