1、默認值約束
特點:
(1)一個表可以有很多的默認值約束
(2)默認值約束只能針對某一個欄位來說
(3)默認值約束意味著,該欄位如果沒有手動賦值,會按默認值處理
2、如何在建表時指定默認值約束?
create table 【資料庫名.】表名稱(
欄位名1 資料型別 primary key,
欄位名2 資料型別 【unique key】【not null】 default 默認值,
欄位名3 資料型別 default 默認值,
,,,,
);
例如:
create table test.t_stu(
sid int primary key,
sname varchar(20) not null,
gender char default '男'
);
insert into t_stu values(1,'張三');
ERROR 1136 (21S01): Column count doesn't match value count at row 1 ' 列數與值數量
不匹配
insert into t_stu values(1,'張三',default);
insert into t_stu(sid,sname) values(2,'李四');
3、建表后如何指定某個欄位有默認值呢?
alter table 【資料庫名.】表名稱 modify 欄位名 資料型別 default 默認值;
create table test.t_stu(
sid int primary key,
sname varchar(20) not null,
gender char
);
alter table t_stu modify gender char default '男';
提醒:如果某個欄位既要非空,又要默認值,那么alter 陳述句后面兩個都要寫,
create table test.t_stu(
sid int primary key,
sname varchar(20) not null,
gender char not null
);
增加gender還有默認值
alter table t_stu modify gender char default '男' not null;
4、如何取消某個欄位的默認值約束
alter table 【資料庫名.】表名稱 modify 欄位名 資料型別 【not null】; #不寫默認值約束
insert into t_stu values (1,'張三','女');
update t_stu set gender = default where sid = 1;
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/227657.html
標籤:其他
上一篇:Oracle資料庫匯出匯入
