日期:2014-05-17 浏览次数:20484 次
USE MyDB; GO IF OBJECT_ID('fk_tb1_QX1') IS NOT NULL ALTER TABLE tb2 DROP CONSTRAINT fk_tb1_QX1; GO IF OBJECT_ID('tb1') IS NOT NULL DROP TABLE tb1; GO IF OBJECT_ID('tb2') IS NOT NULL DROP TABLE tb2; GO CREATE TABLE tb1 (QX1 VARCHAR(10) NOT NULL ,--一类权限 DSC VARCHAR(50) NULL,--描述 CONSTRAINT pk_tb1_QX1 PRIMARY KEY (QX1) ); GO CREATE TABLE tb2 ( QX VARCHAR(10) NOT NULL ,--详细权限 QXf VARCHAR(10) NULL,--父权限 QX1 VARCHAR(10) NULL,--一类权限 CONSTRAINT pk_tb2_QX PRIMARY KEY (QX), CONSTRAINT fk_tb1_QX1 FOREIGN KEY (QX1) REFERENCES tb1 (QX1) ); GO --插入数据 INSERT INTO tb1 VALUES ('F1a',NULL ); INSERT INTO tb1 VALUES ('F1b',NULL ); INSERT INTO tb1 VALUES ('F1c',NULL ); INSERT INTO tb2 VALUES('F2a',NULL,'F1a'); INSERT INTO tb2 VALUES('F2b',NULL,'F1a'); INSERT INTO tb2 VALUES('F2c',NULL,'F1a'); INSERT INTO tb2 VALUES('F3a','F2a','F1b'); INSERT INTO tb2 VALUES('F3b','F2a','F1b'); INSERT INTO tb2 VALUES('F3c','F2b','F1c'); INSERT INTO tb2 VALUES('F3d','F2b','F1c');