Table-level constraints : Foreign Key « Constraints « SQL Server / T-SQL






Table-level constraints


1> CREATE TABLE employee (emp_no INTEGER NOT NULL CONSTRAINT prim_empl PRIMARY KEY,
2>                    emp_fname CHAR(20) NOT NULL,
3>                    emp_lname CHAR(20) NOT NULL,
4>                    dept_no CHAR(4) NULL)
5>
6> GO
1> CREATE TABLE works_on (emp_no     INTEGER NOT NULL,
2>                        project_no CHAR(4) NOT NULL,
3>                        job        CHAR (15) NULL,
4>                        enter_date DATETIME NULL,
5>                        CONSTRAINT prim_works PRIMARY KEY (emp_no, project_no),
6>                        CONSTRAINT foreign_works FOREIGN KEY (emp_no) REFERENCES employee (emp_no))
7> GO
1> -- Specified with two declarative integrity constraints
2> -- Both constraints are table-level constraints,
3>
4> drop table works_on
5> GO
1>
2> drop table employee
3> GO
1>
           
       








Related examples in the same category

1.Define Primary key and foreign key
2.How to use foreign key constraints
3.Adding a Foreign Key to an Existing Table