MySQL中的常用约束条件
导读
在进行表记录清空操作时,
truncate > delete
同时,truncate直接清空全部记录且自增从头开始。
truncate 表名 -> 直接清空这个表的内容
where
select * from 表名 where 字段名 条件 值;
语法格式
create table 表名 (字段名 数据类型 约束条件, 字段名 数据类型 约束条件, ...);
主键
create table 表名(id int unsigned auto_increment primary key, name varchar(10));)
非空
create table 表名 (字段名 数据类型 not null);
唯一
create table 表名 (字段名 数据类型 unique);
默认值
create table 表名 (字段名 数据类型 default 默认值);
mysql> create table demo05 (id int unsigned auto_increment primary key, name varchar(10) default '菜鸟');
Query OK, 0 rows affected (0.02 sec)
mysql> desc demo05
-> ;
+-------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+------------------+------+-----+---------+----------------+
| id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| name | varchar(10) | YES | | 菜鸟 | |
+-------+------------------+------+-----+---------+----------------+
2 rows in set (0.00 sec)
mysql> insert into demo05 (name) values(), ('初来乍到');
ERROR 1136 (21S01): Column count doesn't match value count at row 1
mysql> insert into demo05 (name) values(''), ('初来乍到');
Query OK, 2 rows affected (0.00 sec)
Records: 2 Duplicates: 0 Warnings: 0
mysql> select * from demo05
-> ;
+----+----------+
| id | name |
+----+----------+
| 1 | |
| 2 | 初来乍到 |
+----+----------+
2 rows in set (0.00 sec)
mysql> truncate demo05;
Query OK, 0 rows affected (0.02 sec)
mysql> insert into demo05(id) values(1);
Query OK, 1 row affected (0.00 sec)
mysql> insert into demo05(name) valuest('初来乍到');
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'valuest('初来乍到')' at line 1
mysql> insert into demo05(name) values('初来乍到');
Query OK, 1 row affected (0.00 sec)
mysql> select * from demo05;
+----+----------+
| id | name |
+----+----------+
| 1 | 菜鸟 |
| 2 | 初来乍到 |
+----+----------+
2 rows in set (0.00 sec)
mysql>
***************************************************************
完