sql server - Primary/foreign key -


currently i've normalized couple of tables, i've got questions. keep simple:

i have 2 tables. 1: useracces 2: userinformation

in useracces "id" primary key, in userinformation "id" foreign key. how can make sure when insert both tables, id be/stay same. when insert automatically equal each other. can query join statement.

edit question.

when delete records example id 1. how can make sure records id 1 deleted? , if you've deleted it, id order strange doesn't it?

for example you"le have :

id  name 3   james 6   elona 9   bryan 

here explanation using sqlfiddle.

here code, completeness...

setup:

create table parenttable (   id int not null primary key,   name varchar(50) )  create table childtable (   id int not null primary key,   parentid int not null foreign key references parenttable(id) on delete cascade,   description varchar(50) )  insert parenttable values ( 1, 'bob'); insert parenttable values ( 2, 'tim');  insert childtable values ( 1, 1, 'bob''s description'); insert childtable values ( 2, 2, 'tim''s description'); 

examples:

select * parenttable pt inner join childtable ct on ct.parentid = pt.id;  -- fail insert childtable values (3, 3, 'judy''s description');  -- succeed after inserting parent record insert parenttable values ( 3, 'judy'); insert childtable values (3, 3, 'judy''s description'); select * parenttable pt inner join childtable ct on ct.parentid = pt.id;  -- deleting parent record "cascade" delete child table delete parenttable id = 1; select * parenttable pt inner join childtable ct on ct.parentid = pt.id; 

the foreign key constraint enforce referential integrity desire. cascading delete allow users delete parent , automatically delete referencing children. without cascading delete, attempt delete parent fail if parent referenced children.


Comments

Popular posts from this blog

android - MPAndroidChart - How to add Annotations or images to the chart -

javascript - Add class to another page attribute using URL id - Jquery -

firefox - Where is 'webgl.osmesalib' parameter? -