DataBase Company Employee
CREATE Table Employee(
emp_id INT PRIMARY KEY ,
first_name VARCHAR(10),
second_name VARCHAR(10),
dob DATE ,
sex VARCHAR(1),
salary int ,
supervi_Id int,
branch_id int
);
CREATE table Branch(
branch_id int PRIMARY KEY,
branch_name VARCHAR(10),
mngr_id int,
mngr_strt_date date,
FOREIGN KEY (mngr_id) REFERENCES employee(emp_id) on delete set null
);
CREATE Table client(
clientId int PRIMARY key,
client_name VARCHAR(10),
branch_id int,
FOREIGN KEY(branch_id) REFERENCES Branch(branch_id) on delete set null
);
CREATE Table Works_with(
emp_id INT,
clientId int,
total_sales int
-- PRIMARY key(emp_id, clientId),
-- FOREIGN key(emp_id) REFERENCES employee(emp_id) on delete CASCADE,
-- FOREIGN key(clientId) REFERENCES client(clientId) on delete CASCADE
);
CREATE Table Branch_Suppliers(
branch_id int ,
supplier_name VARCHAR(15),
supply_type VARCHAR(15),
PRIMARY key (branch_id, supplier_name),
FOREIGN key (branch_id) REFERENCES Branch (branch_id) on delete CASCADE
);
ALTER Table employee
add foreign key(branch_id)
references branch(branch_id)
on delete set null;
ALTER Table works_with
add foreign key(emp_id)
references employee(emp_id)
on delete cascade;
ALTER Table works_with
add foreign key(clientId)
references client(clientId)
on delete cascade;
ALTER Table employee
add foreign key(supervi_id)
references employee(emp_id)
on delete set null;
ALTER Table Works_with
add primary key(emp_id, clientId);
Comments
Post a Comment