CREATE TABLE Student
(Sno CHAR(9) PRIMARY KEY, /*在列级定义主码*/
Sname CHAR(20) NOT NULL,
Ssex CHAR(2),
Sage SMALLINT,
Sdept CHAR(20)
);
或者
CREATE TABLE Student
(Sno CHAR(9),
Sname CHAR(20) NOT NULL,
Ssex CHAR(2),
Sage SMALLINT,
Sdept CHAR(20),
PRIMARY KEY(Sno) /*在表级定义主码*/
);
将SC表中的Sno,Cno属性组定义为码。
CREATE TABLE SC
(Sno CHAR(9) NOT NULL,
Cno CHAR(4) NOT NULL,
Grade SMALLINT,
PRIMARY KEY(Sno,Cno) /*只能在表级定义主码*/
);
CREATE TABLE SC
(Sno CHAR(9) NOT NULL,
Cno CHAR(4) NOT NULL,
Grade SMALLINT ,
PRIMARY KEY (Sno,Cno), /*在表级定义实体完整性*/
FOREIGN KEY(Sno) REFERENCES Student(Sno), /*在表级定义参照完整性*/
FOREIGN KEY(Cno) REFERENCES Course(Cno) /*在表级定义参照完整性*/
);
CREATE TABLE SC
(Sno CHAR(9) NOT NULL,
Cno CHAR(4) NOT NULL,
Grade SMALLINT,
PRIMARY KEY(Sno,Cno), /*在表级定义实体完整性*/
FOREIGN KEY(Sno) REFERENCES Student(Sno) /*在表级定义参照完整性*/
ON DELETE CASCADE /*当删除student表中的元组时,级连删除SC表中相应的元组*/
ON UPDATE CASCADE, /*当更新student表中的Sno时,级连更新SC表中相应的元组*/
FOREIGN KEY(Cno) REFERENCES Course(Cno) /*在表级定义参照完整性*/
ON DELETE NO ACTION /*当删除course表中的元组造成了与SC表不一致时拒绝删除*/
ON UPDATE CASCADE, /*当更新course表中的cno时,级连更新SC表中相应的元组*/
);
CREATE TABLE SC
(Sno CHAR(9) NOT NULL. /*Sno属性不允许取空值*/
Cno CHAR(4) NOT NULL, /*Cno属性不允许取空值*/
Grade SMALLINT NOT NULL, /*Grade属性不允许取空值*/
PRIMARY KEY(Sno,Cno), /*如果在表级定义实体完整性,隐含了Sno,Cno不允许取空值则在列级不允许取空值的定义就不必写了*/
…… /*其它语句省略*/
);
CREATE TABLE DEPT
(Deptno NUMERIC(2),
Dname CHAR(9) UNIQUE, /*要求Dname列值唯一*/
Location CHAR(10),
PRIMARY KEY(Deptno)
);
CREATE TABLE Student
(Sno CHAR(9) PRIMARY KEY, /*在列级定义主码*/
Sname CHAR(8) NOT NULL, /*Sname属性不允许取空值*/
Ssex CHAR(2) CHECK(Ssex IN('男‘,’女‘), /*性别属性Ssex只允许取’男‘或’女‘*/
Sage SMALLINT,
Sdept CHAR(20)
);
CREATE TABLE SC
(Sno CHAR(9) NOT NULL,
Cno CHAR(4) NOT NULL,
Grade SMALLINT CHECK(Grade>=0AND Grade<=100),
PRIMARY KEY (Sno,Cno),
FOREIGN KEY(Sno) REFERENCES Student(Sno),
FOREIGN KEY(Cno) REFERENCES Course(Cno)
);
CREATE TABLE Student
(Sno CHAR(9),
Sname CHAR(8) NOT NULL,
Ssex CHAR(2),
Sage SMALLINT,
Sdept CHAR(20),
PRIMARY KEY(Sno),
CHECK (Ssex='女' OR Sname NOT LIKE 'Ms.%' /*定义了元组中Sname和Ssex两个属性值之间的约束条件*/
);
CONSTRAINT <完整性约束条件名>[PRIMARY KEY 短语|FOREIGN KEY 短语|CHECK 短语]
建立学生登记表Student,要求学号在90000-99999之间,姓名不能取空值,年龄小于30,性别只能是’男‘或’女‘。
CREATE TABLE Student
(Sno NUMERIC(6)
CONSTRAINT C1 CHECK(Sno BETWEEN 90000 AND 99999),
Sname CHAR(20)
CONSTRAINT C2 NOT NULL,
Sage NUMERIC(3)
CONSTRAINT C3 CHECK(Sage<30),
Ssex CHAR(2)
CONSTRAINT C4 CHECK(Ssex IN('男' ,'女'),
CONSTRAINT StudentKey PRIMARY KEY(Sno)
);
CREATE TABLE TEACHER
(Eno NUMERIC(4) PRIMARY KEY,
Ename CHAR(10),
Job CHAR(8),
Sal NUMERIC(7,2),
Deduct NUMERIC(7,2),
Deptno NUMERIC(2),
CONSTRAINT EMPFKey FOREIGN KEY(Deptno) REFERENCES DEPT(Deptno),
CONSTRAINT C1 CHECK (Sal+Deduct>=3000)
);
ALTER TABLE Student
DROP CONSTRAINT C4;
ALTER TABLE Student
DROP CONSTRAINT C1;
ALTER TABLE Student
ADD CONSTRAINT C1 CHECK (Sno BETWEEN 900000 AND 999999),
ALTER TABLE Student
DROP CONSTRAINT C3;
ALTER TABLE Student
ADD CONSTRAINT C3 CHECK(Sage<40);
CREATE DOMAIN GenderDomain CHAR(2)
CHECK (VALUE IN('男','女');
Ssex GenderDomain;
CREATE DOMAIN GenderDomain CHAR(2)
CONSTRAINT GD CHECK (VALUE IN('男','女');
ALTER DOMAIN GenderDomain
DROP CONSTRAINT GD;
ALTER DOMAIN GenderDomain
ADD CONSTRAINT GDD CHECK(VALUE IN ('1','0'));
CREATE TRIGGER <触发器>
{ BEFORE|AFTER } <触发事件> ON <表名>
FOR EACH { ROW | STATEMENT }
[WHEN <触发条件>]
<触发动作体>
CREATE TRIGGER InSert_Or_UpDate_Sal /*在教师表Teacher上定义触发器*/
BEFORE INSERT OR UPDATE ON Teacher /*触发事件是插入或更新操作*/
FOR EACH ROW /*这是行级触发器*/
AS BEGIN /*定义触发动作体,这是一个PL/SQL过程块*/
IF(new.job='教授')AND(new.Sal<4000)THEN /*因为是行级触发器,可在过程体中使用插入或更新操作后的新值*/
new.Sal=4000;
END IF;
END; /*触发动作体结束*/
CREATE TABLE Sal_log
(Eno NUMERIC(4) REFERENCES teacher(Eno),
Sal NUMERIC(7,2),
Username CHAR(10),
Date TIMESTAMP
);
CREATE TRIGGER Insert_Sal /*建立了一个触发器*/
AFTER INSERT OR Teacher /*触发事件是INSERT*/
FOR EACH ROW
AS BEGIN
INSERT INTO Sal_log VALUES(
new.Eno,new.Sal,CURRENT_USER,CURRENT_TIMESTAMP);
END;
CREATE TRIGGER Update_Sal /*建立了一个触发器*/
AFTER UPDATE ON Teacher /*触发事件是UPDATE*/
FOR EACH ROW
AS BEGIN
IF(new.Sal<>old.Sal) THEN
INSERT INTO Sal_log VALUES(
new.Eno,new.Sal,CURRENT_USER,CURRENT_TIMESTAMP);
END IF;
END;
UPDATE Teacher SET Sal =800 WHERE Ename="陈平";
DROP TRIGGER<触发器名>ON<表名>;
DROP TRIGGER Insert_Sal ON Teacher;