赞
踩
select null;
+------+
| NULL |
+------+
| NULL |
+------+
select 1+null;
+--------+
| 1+null |
+--------+
| NULL |
+--------+
mysql> create table myclass(
-> class_name varchar(20) not null,
-> class_room varchar(10) not null);
mysql> desc myclass;
+------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+------------+-------------+------+-----+---------+-------+
| class_name | varchar(20) | NO | | NULL | |
| class_room | varchar(10) | NO | | NULL | |
+------------+-------------+------+-----+---------+-------+
# 插入数据时,没有给教室数据插入失败:
mysql> insert into myclass(class_name) values('class1');
ERROR 1364 (HY000): Field 'class_room' doesn't have a default value
mysql> create table t1 ( -> name varchar(20) not null, -> age tinyint unsigned default 0, -> sex char(2) default '男' -> ); mysql> desc t1; +-------+---------------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+---------------------+------+-----+---------+-------+ | name | varchar(20) | NO | | NULL | | | age | tinyint(3) unsigned | YES | | 0 | | | sex | char(2) | YES | | 男 | | +-------+---------------------+------+-----+---------+-------+ mysql> insert into t1(name) values('zhangsan'); mysql> select * from tt10; +----------+------+------+ | name | age | sex | +----------+------+------+ | zhangsan | 0 | 男 | +----------+------+------+ --注意:只有设置了default的列,才可以在插入值的时候,对列进行省略
mysql> create table t2 (
-> name varchar(20) not null comment '姓名',
-> age tinyint unsigned default 0 comment '年龄',
-> sex char(2) default '男' comment '性别'
-> );
mysql> desc t2;
+-------+---------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+---------------------+------+-----+---------+-------+
| name | varchar(20) | NO | | NULL | |
| age | tinyint(3) unsigned | YES | | 0 | |
| sex | char(2) | YES | | 男 | |
+-------+---------------------+------+-----+---------+-------+
mysql> show create table t2\G
*************************** 1. row ***************************
Table: t2
Create Table: CREATE TABLE `t2` (
`name` varchar(20) NOT NULL COMMENT '姓名',
`age` tinyint(3) unsigned DEFAULT '0' COMMENT '年龄',
`sex` char(2) DEFAULT '男' COMMENT '性别'
) ENGINE=MyISAM DEFAULT CHARSET=gbk
mysql> show create table t3\G
***************** 1. row *****************
Table: t3
Create Table: CREATE TABLE `t3` (
`a` int(10) unsigned DEFAULT NULL,
`b` int(10) unsigned DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=gbk
mysql> insert into t3 values(1,2);
mysql> select * from tt3;
+------+------+
| a | b |
+------+------+
| 1 | 2 |
+------+------+
mysql> alter table t3 change a a int(5) unsigned zerofill;
# 也可用 alter table t3 modify a int(5) unsigned zerofill;
mysql> show create table t3\G
*************************** 1. row ***************************
Table: tt3
Create Table: CREATE TABLE `tt3` (
`a` int(5) unsigned zerofill DEFAULT NULL, --具有了zerofill
`b` int(10) unsigned DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=gbk
mysql> select * from t3;
+-------+------+
| a | b |
+-------+------+
| 00001 | 2 |
+-------+------+
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。