首先咱们回忆联系上几节内容。先看code
CREATE TABLE student( studentid SERIAL PRIMARY KEY , ---主键约束 studentname VARCHAR(20) NOT NULL , ---非空约束 studentsex INT CHECK (studentsex = '0' OR studentsex ='1' ) DEFAULT 0 , ---默认值 studentbirthday DATE CHECK ( studentbirthday <CURRENT_TIMESTAMP ) , ---检查约束,表约束 studentaddress CHAR(30) , studentadmissiontime DATE , ---入学时间 CONSTRAINT checkstudentadmissiontimeandstudentbirthday CHECK (studentbirthday > studentadmissiontime ) ---检查约束,表约束 );
下面代码练习删除已命名约束。io
---删除约束checkstudentadmissiontimeandstudentbirthday ALTER TABLE student DROP CONSTRAINT checkstudentadmissiontimeandstudentbirthday; ---删除非空约束 ALTER TABLE "public".student ALTER COLUMN studentname DROP NOT NULL;
给列添加约束值。im
---设置入学默认时间为当前时间 ALTER TABLE student ALTER COLUMN studentadmissiontime SET DEFAULT CURRENT_TIMESTAMP; ---删除默认值 ALTER TABLE student ALTER COLUMN studentadmissiontime DROP DEFAULT;
为“保持原样”,请从新执行设置入学默认时间为当前时间。命名
修改列名称。时间
---修改列名称,将列名studentid 修改成新列名studentNO ALTER TABLE "public".student RENAME COLUMN studentid TO "studentNO";
修改表名称。co
ALTER TABLE student RENAME TO "Student";
参考资料:PostgreSQL参考手册9.6版第五章第五节等。time