SQL Server FOREIGN KEY constraint

Source: Internet
Author: User

Definition of the primary foreign key in SQL Server:

1.

Create Table int Primary Key  not NULL  into Values (Ten, 'IT '), (+,' Finance'), (+, 'engneer ')
Create Table int Primary Key  not NULL int Foreign Key References Dept (DEPT_NO))
The above notation, is the column level definition, defines the column field directly when defining foreign key

2.

create  table  Dept (dept_no int  primary  key , Dept_name nvarchar (+) not  null  Prmary key  (dept_no)) Insert into  dept values  (10, ' IT ' ), (20, ' Finance ' ), (30, Engneer ' ) 
Create Table int Primary Key  not NULL int,constraintforeignkeyreferences dept (DEPT_NO))
This is a table-level definition.

3. You have created a table that does not have a primary foreign key defined, you can use ALTER TABLE to modify

alter  table  employee add  foreign  key  (DEPT_NO) references  Dept (dept_no) alter  table  employeeadd  constraint  dept_no_fk  Foreign  key  (dept_no) references  Dept (dept_no) 

Add primary Key

Alter Table Deptaddconstraintprimarykey(dept_no)

4. Remove the primary key

Alter Table Drop constraint

Remove foreign keys

Alter Table Drop constraint DEPT_NO_FK

The building was deleted, the building was deleted, the hand hurts originally intended to put the MySQL statement also written, the reason is that last night to read about the MySQL video, about check does not work, and their foreign key constraints are also vague, do not know how to write, the original is two ways of writing, which is to see the MySQL video to see. I'm going to have to write MySQL again sometime.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.