0%

4.5.2 默认值

4.5.2 默认值

SQL允许为属性指定默认值,如下面的create table语句所示:

1
2
3
4
5
6
7
create table student(
ID varchar(5),
name varchar(20) not null,
dept_name varchar(20),
tot_cred numeric(3,0) default 0. ,
primary key(ID)
);

tot_cred属性的默认值被声明为0。这样,当一个元组被插入到student关系中,如果没有给出tot_cred属性的值,那么该元组在此属性上的取值就被置为0
下面的插入语句说明了在插入操作中如何省略tot_cred属性的值:

1
2
insert into student(ID, name, dept_name)
values('12789','Newman','Comp. sci. ');
1
2
3
4
5
6
7
8
9
10
11
mysql> insert into student(ID, name, dept_name)
values('12789','Newman','Comp. sci. ');
Query OK, 1 row affected (0.01 sec)

mysql> select * from student;
+-------+--------+-------------+----------+
| ID | name | dept_name | tot_cred |
+-------+--------+-------------+----------+
| 12789 | Newman | Comp. sci. | 0 |
+-------+--------+-------------+----------+
1 row in set (0.02 sec)

原文链接: 4.5.2 默认值