Author: David Eulerdate: 2004/10 / 16Email: de_euler-david@yahoo.com.cn
If you have any questions, please contact me:) SQL Server build foreign key constraints 3 Method: 1.enterprise manager, tables, design table, setting Table Properties, you can create constraint, reference key; 2.enterprise manager , Diagrams, New Diagrams, and establish two relationships. 3. Use the Transact SQL statement directly. All three methods need to establish a data sheet. - Create a table author: create table [dbo]. [Author] [Bigint] not null, [Authorname] [char] (10) null, [address] [char] (480) NULL, [Introduction] [NTEXT] NULL)
- Create Table Mybbs: Reate Table [DBO]. [Mybbs] ([Id] [Bigint] Identity (1, 1) Not Null, [Authorid] [Bigint] NULL, [TITLE] [CHAR] (40) NULL , [DateTime] null, [Abstract] [CHAR] (480) NULL, [Content] [NTEXT] NULL) Author-mybbs relationship diagram Sets table Mybbs Authorid as foreign key, refer to the primary key ID field of the Author table , directly transact sql statement as follows: - tABLE increase mybbs (authorId) foreign key constraints FK_mybbs_author, the table myBBS AUTHORID ID primary key constraint by the author table: BEGIN TRANSACTIONalter table dbo.mybbs add constraint FK_mybbs_author foreign key ( Authorid) References dbo.author ([ID]) on Update Cascade On Delete Cascade
- remove foreign key constraint FK_mybbs_author: - alter table dbo.mybbs drop constraint FK_mybbs_author - rollback commit transaction when the above ON UPDATE CASCADE, ON DELETE CASCADE two options after the specified id field of the author table has delete, update operation, myBBS The ID in the table will also be deleted or updated cascaded. If not selected, it is not possible to update or delete operations that have been associated with myBBS tables in the Author table.