外鍵約束和參紹約束的實例腳本
/*
外鍵約束和參紹約束
pubs數據庫的CarIfo表創建外鍵約束參紹CarFactory表的主鍵ID
*/
USE Pubs
GO
/*不顯示行數*/
SET NOCOUNT ON
GO
/*創建示例表*/
IF EXISTS (SELECT * FROM dbosysobjects
WHERE id = object_id(NCarInfo)
AND OBJECTPROPERTY(id NIsUserTable) = )
DROP TABLE CarInfo
GO
CREATE TABLE CarInfo(
Car_ID CHAR ()
Name CHAR ()
Type CHAR ()
Driver_Type CHAR ()
)
IF EXISTS (SELECT * FROM dbosysobjects
WHERE id = object_id(NCarFactory)
AND OBJECTPROPERTY(id NIsUserTable) = )
DROP TABLE CarFactory
GO
CREATE TABLE CarFactory(
ID CHAR () NOT NULL
Name CHAR ()
PostCode CHAR ()
Telphone CHAR ()
Address CHAR ()
)
GO
ALTER TABLE CarFactory
ADD CONSTRAINT PK_Car_ID
PRIMARY KEY NONCLUSTERED (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
From:http://tw.wingwit.com/Article/program/Oracle/201311/17284.html