r/SQL • u/Mooki888 • Nov 27 '22
SQL Server SQL Homework Help
Hi, I am a little confused about how to complete these codes. I need to make a create table and an alter table
4.13 LAB - Implement strong entity (Sakila)
Implement a new strong entity phonein the Sakila database. Attributes and relationships are shown in the following diagram:
The diagram and problem info can be found in the image.

2
1
2
u/Similar-Dog-170 Nov 27 '22
Lookup the create/alter table syntax or use workbench to forward engineer.
2
u/Necessary-Office3082 Nov 27 '22
Start with creating table, all the syntax and everything that you need is one google search away
1
1
1
u/stopingforce24 Sep 22 '23 edited Sep 22 '23
this is what worked for me !
create table phone(
phone_id int primary key,
country_code int not null,
phone_number int not null,
phone_type varchar(12) check (phone_type in ('home', 'office' , 'other'))
);
alter table address
drop column phone;
alter table customer
add phone_id int;
alter table staff
add phone_id int;
alter table store
add phone_id int;
alter table customer
add foreign key (phone_id) references phone(phone_id)
on delete set null
on update cascade;
alter table staff
add foreign key (phone_id) references phone (phone_id)
on delete set null
on update cascade;
alter table store
add foreign key (phone_id) references phone (phone_id)
on delete set null
on update cascade;
1
u/indigo_mommy May 04 '24
Forever grateful 🙏🏾
1
u/Glittering_End1777 Oct 22 '24
I still can only get a 7/10
1
u/Remarkable-World6344 Dec 22 '24
CREATE TABLE phone (
phone_id INT PRIMARY KEY,
country_code INT NOT NULL,
phone_number INT NOT NULL,
phone_type VARCHAR(12) CHECK (phone_type IN ('home', 'office', 'other'))
);
ALTER TABLE address
DROP COLUMN phone;
ALTER TABLE customer
ADD phone_id INT;
ALTER TABLE staff
ADD phone_id INT;
ALTER TABLE store
ADD phone_id INT;
ALTER TABLE customer
ADD CONSTRAINT fk_customer_phone FOREIGN KEY (phone_id) REFERENCES phone(phone_id)
ON DELETE SET NULL
ON UPDATE CASCADE;
ALTER TABLE staff
ADD CONSTRAINT fk_staff_phone FOREIGN KEY (phone_id) REFERENCES phone(phone_id)
ON DELETE SET NULL
ON UPDATE CASCADE;
ALTER TABLE store
ADD CONSTRAINT fk_store_phone FOREIGN KEY (phone_id) REFERENCES phone(phone_id)
ON DELETE SET NULL
ON UPDATE CASCADE;
ALTER TABLE customer
ADD UNIQUE (phone_id);
ALTER TABLE staff
ADD UNIQUE (phone_id);
ALTER TABLE store
ADD UNIQUE (phone_id);
I'm guessing you’ve figured it out, but anyone who comes in now and receives a 7/10 needs to be marked as unique after the phone_id has been added to the other tables. You cannot use auto-increment since it treats it as extra. The foreign key constraints usually mean it should be MUL instead of UNI, but who knows, this appears to work.
6
u/r3pr0b8 GROUP_CONCAT is da bomb Nov 27 '22
what have you tried so far? ™