FOREIGN KEY constraints and parameter constraints
The Carifo table of the pubs database creates a foreign key constraint, and the primary key ID of the table is carfactory.
*/
Use Pubs
Go
/* Do not display the number of lines *
SETNOCOUNT on
Go
/* Create Sample Table * *
IF EXISTS (SELECT * from dbo.sysobjects
WHERE id = object_id (N ' Carinfo ')
and OBJECTPROPERTY (ID, N ' isusertable ') = 1)
DROPTABLECarinfo
Go
CREATE TABLE Carinfo (
car_id CHAR (10),
Name CHAR (10),
Type CHAR (20),
Driver_type CHAR (15)
)
IF EXISTS (SELECT * from dbo.sysobjects
WHERE id = object_id (N ' carfactory ')
and OBJECTPROPERTY (ID, N ' isusertable ') = 1)
DROP TABLE Carfactory
Go
CREATE TABLE Carfactory (
ID CHAR (a) not NULL,
Name CHAR (20),
Postcode CHAR (6),
Telphone CHAR (15),
AddressCHAR (30),
)
Go
ALTER TABLE Carfactory
ADD CONSTRAINT pk_car_id
PRIMARYKEYNonclustered (ID)
Go
ALTER TABLE Carinfo
With NOCHECK
ADD CONSTRAINT Car_id_link
FOREIGN KEY (car_id) REFERENCES carfactory (ID)
Go
Use master
EXEC sp_dboption Pubs, ' dbo use only ', TRUE
Go
Use Pubs
SET NOCOUNT on
Go