2023-12-08|閱讀時間 ‧ 約 2 分鐘

MySQL 基本語法 (三) Constraints

Constraints 限制、約束

在創建表格的時候可以新增一些限制/約束

NOT NULL : 該屬性的值不能是空的,一定要有值

UNIQUE : 該屬性的每個值都不能重複,都是唯一的

DEFAULT : 設定預設值

AUTO_INCREMENT : 新增資料時可以不用再寫student_id,便能夠往下加1

CREATE TABLE `student`(
`student_id` INT,
#`student_id` INT AUTO_INCREMENT,
`name` VARCHAR(20) NOT NULL,
`major` VARCHAR(20) UNIQUE,
#`major` VARCHAR(20) DEFAULT '歷史',
PRIMARY KEY(`student_id`)
);

實例 :

INSERT INTO `student` VALUES(1,'小白','英語');
INSERT INTO `student` VALUES(2,NULL,'英語');
INSERT INTO `student` VALUES(2,'小黑','英語');

使用 `student_id` INT AUTO_INCREMENT 後,便可以不用再寫student_id的編號,即可依序新增資料

INSERT INTO `student`(`name`,`major`) VALUES('小白','英語');




分享至
成為作者繼續創作的動力吧!
從 Google News 追蹤更多 vocus 的最新精選內容從 Google News 追蹤更多 vocus 的最新精選內容

Youna's Devlog 的其他內容

發表回應

成為會員 後即可發表留言
© 2024 vocus All rights reserved.