After googling about foreign keys, this is the way i understood them.
If i need to save the phone-no of people in a table with address, it will create multiple records for a single person as he can have multiple numbers. that will also store redundant address values in each repeated row. so using the user-id as foreign key, i can phone-no to another table and save the address from being repeated. So my question is if a user is deleted from the user table with address, will it also automatically remove all associated values in phone table? Or that has to be specified at the time of creating the table that deleting this will delete that? also what happens if user-id is changed only in first table and when it is changed in only 2nd table.
If i have 2 tables
table: user
+-----------------------------+
|user-id | username | address |
+-----------------------------+
table: phone-no
+--------------------------+
| pid | phone-no | user-id |
+---------------------------
Also, if it is not too much, can you show me the query for creating both of this with foreign keys.
User-id from table user would be the primary key, and user-id in the table phone-no would be the foregin key referencing user-id in user table.
create table user(
userid int identity(1,1) not null primary key,
username varchar(50) not null,
adress varchar(200) not null,
);
create table phone-no(
pid int identity(1,1) not null primary key,
phone-no int not null,
user-id int not null foreign key (userid) references user(userid) on delete cascade,
);
On delete cascade option is optional, hope you understand what i meant by that.