PostgreSQL數據庫平常學習筆記15-修改表

首先咱們回憶聯繫上幾節內容。先看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

相關文章
相關標籤/搜索