SQL-DDL(13) 数据定义语言(2)

4.常见的数据类型

(1)数值型:

整数
小数:
    定点数
    浮点数

(2)字符型:

 较短的文本:char , varchar 
 较长的文本:text ,blob (较长的二进制数据,存放图片)
 较短的二进制:binary varbinary
 枚举型:ENUM(可供选择的字符) (单选)
       if列表成员为1~255,则需要一个字节存储
       if列表成员为256~65535(最多就存65535个),则需要两个字节存储
 set类型:set(可供选择的字符)(可多选)
         成员数   字节数
         1~8     1
         9~16    2
         17~24   3      
         25~32    4
         33~64    8  

(3)日期型:
date
datetime
timestamp
time
year

4-1-1 整型

@1. 类型分类: tinyint(1个字节) , smallint(两个字节) , mediumint(三个字节) ,int/integer(四个字节),bigint(八个字节)

@2. 特点:1.默认有符号,如果想设置无符号,需要添加 unsigned 关键字
         2.如果插入的数值超出整型的范围,会报 out of range 异常,老师演示的时候插入的是临界值,然而我在我的机器中,是报错,并插入失败。
         3.如果不设置长度,会有默认长度,长度代表了显示的最大宽度,如果不够回用‘0’在左边填充,但必须使用 ‘zerofill’!。
注意⚠️:这些整型类型能存的数的范围依然是原本的范围。eg:int(1),int(2),存最大值依然是2147483647.
这个显示长度并不会限制该字段的数字存储范围,显示长度与zerofilly一起用才会有效果,比如房间号一般就用 roomId int(4) zerofill.为了显示效果而已吧。

@3.练习验证

#1.验证特点1:
mysql> create table tab_int( 
   -> t1 int(8),
   -> t2 int(8) zerofill
   -> );
   
mysql> desc tab_int;
+-------+--------------------------+------+-----+---------+-------+
| Field | Type                     | Null | Key | Default | Extra |
+-------+--------------------------+------+-----+---------+-------+
| t1    | int(8)                   | YES  |     | NULL    |       |
| t2    | int(8) unsigned zerofill | YES  |     | NULL    |       |
+-------+--------------------------+------+-----+---------+-------+
2 rows in set (0.01 sec)

#2.验证特点2:
mysql> insert into tab_int values (-2147483649,12345678);
ERROR 1264 (22003): Out of range value for column 't1' at row 1
mysql> select * from tab_int;
Empty set (0.00 sec)

#3.验证特点3:(插入t3时,我没给定宽度,它默认宽度是11.

mysql> alter table tab_int add column t3 int;
Query OK, 0 rows affected (0.59 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> select * from tab_int;
Empty set (0.00 sec)

mysql> desc tab_int;
+-------+--------------------------+------+-----+---------+-------+
| Field | Type                     | Null | Key | Default | Extra |
+-------+--------------------------+------+-----+---------+-------+
| t1    | int(8)                   | YES  |     | NULL    |       |
| t2    | int(8) unsigned zerofill | YES  |     | NULL    |       |
| t3    | int(11)                  | YES  |     | NULL    |       |
+-------+--------------------------+------+-----+---------+-------+
3 rows in set (0.01 sec)

#验证注意⚠️:

mysql> #int:有符号范围 -21474833648 ~21474833647 ,无符号:0~4294967295
宽度只是了显示用途,与存储大小无关。

mysql>  insert into tab_int (t1,t2) values (2147483647,4294967295);
Query OK, 1 row affected (0.10 sec)

#大于存储范围是存不进去的!!!
mysql>  insert into tab_int (t1,t2) values (2147483648,4294967296);
ERROR 1264 (22003): Out of range value for column 't1' at row 1
mysql> select * from tab_int;
+------------+------------+------+
| t1         | t2         | t3   |
+------------+------------+------+
|        123 |   00000123 | NULL |
| 2147483647 | 4294967295 | NULL |
+------------+------------+------+
2 rows in set (0.01 sec)

#这是我操作的版本。
mysql> select version();
+-----------+
| version() |
+-----------+
| 8.0.11    |
+-----------+
1 row in set (0.06 sec)

4-1-2 小数

 (1)浮点型:
    float(M ,D) (4个字节)
    double(M,D) (8个字节)
(2)定点型
     dec(M,D) (M+2个字节)(最大取值范围与double相同)         
     decimal(M,D) (M+2个字节(最大取值范围与double相同,给定的decimal的有效范围有M和D决定)
     
 (3)特点:     
       @1. M:整数部位+小数部位 D:小数部位,如果整数超出范围,则显示Out of range value错误,当小数部位超出范围,会根据后一位四舍五入,小于范围会补0.
       @2. M和D都可以省略
           如果是decimal,则M默认为10,D默认为0.
           如果是float和double,则会根据插入的数值的精度来决定精度。(测试时,发现dec,decimal也可以呀)
       @3. 定点型的精确度较高,如果要求插入数值的精度较高时如:货币运算等则考虑使用(我测试的时候好像没体验出来哦!!!)
       @选择原则:
          所选择的类型越简单越好,能保存数值的类型越小越好。

(4)测试M和D

#验证@1:
mysql> desc tab_float2;
+-------+--------------+------+-----+---------+-------+
| Field | Type         | Null | Key | Default | Extra |
+-------+--------------+------+-----+---------+-------+
| f1    | float(6,2)   | YES  |     | NULL    |       |
| f2    | double(6,2)  | YES  |     | NULL    |       |
| d1    | decimal(6,2) | YES  |     | NULL    |       |
| d2    | decimal(6,2) | YES  |     | NULL    |       |
+-------+--------------+------+-----+---------+-------+
4 rows in set (0.01 sec)

mysql> insert into tab_float2 values(1234.234,1234.234,1234.234,1234.234);
Query OK, 1 row affected, 2 warnings (0.32 sec)

mysql> select * from tab_float2;
+---------+---------+---------+---------+
| f1      | f2      | d1      | d2      |
+---------+---------+---------+---------+
| 1234.23 | 1234.23 | 1234.23 | 1234.23 |
+---------+---------+---------+---------+
1 row in set (0.00 sec)

mysql> insert into tab_float2 values(12345.23,12345.23,12345.23,12345.23);
ERROR 1264 (22003): Out of range value for column 'f1' at row 1
mysql> select * from tab_float2;
+---------+---------+---------+---------+
| f1      | f2      | d1      | d2      |
+---------+---------+---------+---------+
| 1234.23 | 1234.23 | 1234.23 | 1234.23 |
+---------+---------+---------+---------+
1 row in set (0.00 sec)

mysql> insert into tab_float2 values(1234.3,1234.3,1234.3,1234.3);
Query OK, 1 row affected (0.34 sec)

mysql> select * from tab_float2;
+---------+---------+---------+---------+
| f1      | f2      | d1      | d2      |
+---------+---------+---------+---------+
| 1234.23 | 1234.23 | 1234.23 | 1234.23 |
| 1234.23 | 1234.23 | 1234.23 | 1234.23 |
| 1234.30 | 1234.30 | 1234.30 | 1234.30 |
+---------+---------+---------+---------+
3 rows in set (0.00 sec)

mysql> insert into tab_float2 values(1234.366,1234.366,1234.366,1234.366);
Query OK, 1 row affected, 2 warnings (0.28 sec)

mysql> select * from tab_float2;
+---------+---------+---------+---------+
| f1      | f2      | d1      | d2      |
+---------+---------+---------+---------+
| 1234.23 | 1234.23 | 1234.23 | 1234.23 |
| 1234.23 | 1234.23 | 1234.23 | 1234.23 |
| 1234.30 | 1234.30 | 1234.30 | 1234.30 |
| 1234.37 | 1234.37 | 1234.37 | 1234.37 |
+---------+---------+---------+---------+
4 rows in set (0.00 sec)

mysql> insert into tab_float2 values(1234.344,1234.344,1234.344,1234.344);
Query OK, 1 row affected, 2 warnings (0.05 sec)

mysql> select * from tab_float2;
+---------+---------+---------+---------+
| f1      | f2      | d1      | d2      |
+---------+---------+---------+---------+
| 1234.23 | 1234.23 | 1234.23 | 1234.23 |
| 1234.23 | 1234.23 | 1234.23 | 1234.23 |
| 1234.30 | 1234.30 | 1234.30 | 1234.30 |
| 1234.37 | 1234.37 | 1234.37 | 1234.37 |
| 1234.34 | 1234.34 | 1234.34 | 1234.34 |
+---------+---------+---------+---------+
5 rows in set (0.00 sec)

mysql> insert into tab_float2 values(1234.345,1234.345,1234.345,1234.345);
Query OK, 1 row affected, 2 warnings (0.14 sec)

mysql> select * from tab_float2;
+---------+---------+---------+---------+
| f1      | f2      | d1      | d2      |
+---------+---------+---------+---------+
| 1234.23 | 1234.23 | 1234.23 | 1234.23 |
| 1234.23 | 1234.23 | 1234.23 | 1234.23 |
| 1234.30 | 1234.30 | 1234.30 | 1234.30 |
| 1234.37 | 1234.37 | 1234.37 | 1234.37 |
| 1234.34 | 1234.34 | 1234.34 | 1234.34 |
| 1234.35 | 1234.35 | 1234.35 | 1234.35 |
+---------+---------+---------+---------+
6 rows in set (0.00 sec)
mysql> insert into tab_float2 values(1234.3456,1234.3456,1234.3456,1234.3456);
Query OK, 1 row affected, 2 warnings (0.32 sec)

mysql> select * from tab_float2;
+---------+---------+---------+---------+
| f1      | f2      | d1      | d2      |
+---------+---------+---------+---------+
| 1234.23 | 1234.23 | 1234.23 | 1234.23 |
| 1234.23 | 1234.23 | 1234.23 | 1234.23 |
| 1234.30 | 1234.30 | 1234.30 | 1234.30 |
| 1234.37 | 1234.37 | 1234.37 | 1234.37 |
| 1234.34 | 1234.34 | 1234.34 | 1234.34 |
| 1234.35 | 1234.35 | 1234.35 | 1234.35 |
| 1234.35 | 1234.35 | 1234.35 | 1234.35 |
+---------+---------+---------+---------+
7 rows in set (0.00 sec)

mysql> insert into tab_float2 values(1234.3126,1234.3126,1234.3126,1234.3126);
Query OK, 1 row affected, 2 warnings (0.05 sec)

mysql> select * from tab_float2;
+---------+---------+---------+---------+
| f1      | f2      | d1      | d2      |
+---------+---------+---------+---------+
| 1234.23 | 1234.23 | 1234.23 | 1234.23 |
| 1234.23 | 1234.23 | 1234.23 | 1234.23 |
| 1234.30 | 1234.30 | 1234.30 | 1234.30 |
| 1234.37 | 1234.37 | 1234.37 | 1234.37 |
| 1234.34 | 1234.34 | 1234.34 | 1234.34 |
| 1234.35 | 1234.35 | 1234.35 | 1234.35 |
| 1234.35 | 1234.35 | 1234.35 | 1234.35 |
| 1234.31 | 1234.31 | 1234.31 | 1234.31 |
+---------+---------+---------+---------+
8 rows in set (0.00 sec)

mysql> insert into tab_float2 values(1234.3146,1234.3146,1234.3146,1234.3146);
Query OK, 1 row affected, 2 warnings (0.06 sec)

mysql> select * from tab_float2;
+---------+---------+---------+---------+
| f1      | f2      | d1      | d2      |
+---------+---------+---------+---------+
| 1234.23 | 1234.23 | 1234.23 | 1234.23 |
| 1234.23 | 1234.23 | 1234.23 | 1234.23 |
| 1234.30 | 1234.30 | 1234.30 | 1234.30 |
| 1234.37 | 1234.37 | 1234.37 | 1234.37 |
| 1234.34 | 1234.34 | 1234.34 | 1234.34 |
| 1234.35 | 1234.35 | 1234.35 | 1234.35 |
| 1234.35 | 1234.35 | 1234.35 | 1234.35 |
| 1234.31 | 1234.31 | 1234.31 | 1234.31 |
| 1234.31 | 1234.31 | 1234.31 | 1234.31 |
+---------+---------+---------+---------+
9 rows in set (0.00 sec)





#验证@2:
mysql> create table tab_float(
   -> f1 float,
   -> f2 double,
   -> f3 decimal
   -> );
Query OK, 0 rows affected (0.31 sec)

mysql> select * from tab_float;
Empty set (0.01 sec)

mysql> desc tab_float;
+-------+---------------+------+-----+---------+-------+
| Field | Type          | Null | Key | Default | Extra |
+-------+---------------+------+-----+---------+-------+
| f1    | float         | YES  |     | NULL    |       |
| f2    | double        | YES  |     | NULL    |       |
| f3    | decimal(10,0) | YES  |     | NULL    |       |
+-------+---------------+------+-----+---------+-------+
3 rows in set (0.01 sec)

mysql> insert into tab_float values(123.4563,123.4563,123.4563);
Query OK, 1 row affected, 1 warning (0.11 sec)

mysql> select * from tab_float;
+---------+----------+------+
| f1      | f2       | f3   |
+---------+----------+------+
| 123.456 | 123.4563 |  123 |
+---------+----------+------+
1 row in set (0.00 sec)

4-1-3 字符数

 (1)较短文本:char , varchar
  (2) 较长文本:text ,blob(较大的二进制)
  
  char vs varchar :
  char(M) M:最大的字符数(固定长度) 省略默认为1  存储的字符长度小于固定长度用空格补全 存储字符长度大于固定长度将报错  比较耗费空间 但查询效率高(整体匹配查询)。
  varchar(M) M:最大的字符数(可变长度) 不可省略  存储的字符长度小于固定长度,只分配实际空间 存储字符长度大于固定长度将报错  省空间 但查询效率低(一个一个匹配查询)。

(3)测试验证

mysql> create table tab_ch(
  -> c1 char,
  -> c2 vachar
  -> );
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 'vachar
)' at line 3
mysql> create table tab_ch(
  -> c1 char ,
  -> c2 varchar(8)
  -> );
Query OK, 0 rows affected (0.22 sec)

mysql> desc tab_ch;
+-------+------------+------+-----+---------+-------+
| Field | Type       | Null | Key | Default | Extra |
+-------+------------+------+-----+---------+-------+
| c1    | char(1)    | YES  |     | NULL    |       |
| c2    | varchar(8) | YES  |     | NULL    |       |
+-------+------------+------+-----+---------+-------+
2 rows in set (0.01 sec)

mysql> insert into tab_ch values ('he','hello');
ERROR 1406 (22001): Data too long for column 'c1' at row 1
mysql> insert into tab_ch values ('h','hellomomo');
ERROR 1406 (22001): Data too long for column 'c2' at row 1
mysql>

Enum类型

mysql> desc tab_char;
+-------+-------------------+------+-----+---------+-------+
| Field | Type              | Null | Key | Default | Extra |
+-------+-------------------+------+-----+---------+-------+
| c1    | enum('男','女')   | YES  |     | NULL    |       |
+-------+-------------------+------+-----+---------+-------+
1 row in set (0.00 sec)

mysql> insert into tab_char values('男');
Query OK, 1 row affected (0.22 sec)

mysql> insert into tab_char values('女');
Query OK, 1 row affected (0.15 sec)

mysql> select * from tab_char;
+------+
| c1   |
+------+
| 男   |
| 女   |
+------+
2 rows in set (0.00 sec)

set类型

mysql> desc tab_set;
+-------+------------------------+------+-----+---------+-------+
| Field | Type                   | Null | Key | Default | Extra |
+-------+------------------------+------+-----+---------+-------+
| s1    | set('a','b\n','c','d') | YES  |     | NULL    |       |
+-------+------------------------+------+-----+---------+-------+
1 row in set (0.00 sec)

mysql> delete from tab_set;
Query OK, 1 row affected (0.13 sec)

mysql> insert into tab_set values('a,c');
Query OK, 1 row affected (0.10 sec)

mysql> insert into tab_set values('a,c,d');
Query OK, 1 row affected (0.10 sec)

mysql> insert into tab_set values('a');
Query OK, 1 row affected (0.15 sec)

mysql> select * from tab_set;
+-------+
| s1    |
+-------+
| a,c   |
| a,c,d |
| a     |
+-------+
3 rows in set (0.00 sec)


4-1-4 日期型

 date 只保存日期
 time 只保存时间
 datetime  保存日期+时间 受时区改变的影响 8个字节
 timestamp  保存日期+时间 受时区变化的影响 4个字节

 
 year 只保存年
mysql> show variables like 'time_zone';
+---------------+--------+
| Variable_name | Value  |
+---------------+--------+
| time_zone     | SYSTEM |
+---------------+--------+
1 row in set (0.01 sec)

mysql> insert into tab_date values(now(),now());
Query OK, 1 row affected (0.15 sec)

mysql> select * from tab_date;
+---------------------+---------------------+
| t1                  | t2                  |
+---------------------+---------------------+
| 2018-08-26 10:59:18 | 2018-08-26 10:59:18 |
+---------------------+---------------------+
1 row in set (0.00 sec)

mysql> set time_zone='+9:00';
Query OK, 0 rows affected (0.00 sec)
#当修改时区后,t2 就显示该时区的时间了,所以一般我们会用timestamp,这样实时反应更准确些。

mysql> select * from tab_date;
+---------------------+---------------------+
| t1                  | t2                  |
+---------------------+---------------------+
| 2018-08-26 10:59:18 | 2018-08-26 11:59:18 |
+---------------------+---------------------+
1 row in set (0.00 sec)

4.常见的约束

 (1)含义:一种限制,用于限制表中的数据,为了保证表中的数据的准确性和可靠性
 (2)分类:六大约束
          primary key:主键,用于保证该字段值是唯一,并且不能为空。
          unique:唯一,用于保证该字段的值是唯一,可以为空,但只能一个为空。
          foreign key:外键,用于限制两个表的关系,用于保证该字段的值必须来自于另一个表(设为表2)的键,用于引用表2的中某一列的值。如学生表的专业编号,员工表的部门编号。
          notnull:非空,用于保证该字段值不能为空。
          default:默认,用于保证该字段有默认值
          check:检查约束【mysql不支持】
  (3)添加约束的时机:1.创建表时 2.修改表时
  (4)约束的添加分类:
          列级约束:六大约束,语法上都不报错,但是只支持默认,非空,主键,唯一。其他无效果。
          表级约束:只支持主键,外键,唯一,检查在语法上支持,但无效果。
   (4)语法:
        create table 表名(
            字段名 字段类型 列级约束,
            字段名 字段类型 ,
            表级约束
        
        );

(5)练习

   #1.列级约束
  mysql> create table stuInfo(
    -> id int primary key,
    -> stuName varchar(2) not null,
    -> gender char(1) check (gender='男'or gender = '女'),
    -> seat int unique,
    -> age int default 18,
    -> majorId int references major(id)
    -> );
    
    #查看stuinfo表中的所有的索引,包括主键,外键,唯一
    mysql> show index from stuInfo;
+---------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+---------+
| Table   | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment | Visible |
+---------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+---------+
| stuInfo |          0 | PRIMARY  |            1 | id          | A         |           0 |     NULL |   NULL |      | BTREE      |         |               | YES     |
| stuInfo |          0 | seat     |            1 | seat        | A         |           0 |     NULL |   NULL | YES  | BTREE      |         |               | YES     |
+---------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+---------+
2 rows in set (0.18 sec)

#表级约束
mysql> create table stuInfo(
    -> id int,
    -> stuName varchar(12),
    -> gender char(1),
    -> seat int,
    -> age int,
    -> majorid int,
    -> constraint pk primary key(id),
    -> constraint uq unique(seat),
    -> constraint ck check(gender='男'or gender='女'),
    -> constraint fk_stuinfo_major foreign key(majorid) references major(id)
    -> );
Query OK, 0 rows affected (0.22 sec)
mysql> show index from stuInfo;
+---------+------------+------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+---------+
| Table   | Non_unique | Key_name         | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment | Visible |
+---------+------------+------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+---------+
| stuInfo |          0 | PRIMARY          |            1 | id          | A         |           0 |     NULL |   NULL |      | BTREE      |         |               | YES     |
| stuInfo |          0 | uq               |            1 | seat        | A         |           0 |     NULL |   NULL | YES  | BTREE      |         |               | YES     |
| stuInfo |          1 | fk_stuinfo_major |            1 | majorid     | A         |           0 |     NULL |   NULL | YES  | BTREE      |         |               | YES     |
+---------+------------+------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+---------+
3 rows in set (0.12 sec)

√√通用的写法

mysql> create table stuInfo(
    -> id int primary key,
    -> stuName varchar(20) not null,
    -> gender char(1),
    -> age int default 18,
    -> seat int unique,
    -> majorid int,
    -> constraint fk_stuinfo_major foreign key(majorid) references major(id)
    -> );
Query OK, 0 rows affected (0.27 sec)

√主键和唯一的对比

          保证唯一性       是否允许为空    一个表中可以有多少个      是否允许组合
 主键        √              x            至多一个                允许,但不推荐
 唯一        √              √            可以多个                允许,但不推荐
  

√外键

1.要求在从表设置外键关系
2.从表的外键列的类型和主表的关联列的类型要求一致或兼容,名称无要求
3.主表的关联列必须是一个key (一般是主键或唯一)
4.插入数据时,先插入主表,再插入从表,删除数据时,先删除从表,再删除主表。

补充:

  1. 级联删除
    alter table 从表名 constraint 索引名 foreign key(外键名) references 主表名(被引用列名) on delete cascade;
  2. 级联置空
    alter table 从表名 constraint 索引名 foreign key(外键名) references 主表名(被引用列名) on delete set null;

修改约束

语法
1.添加列级约束
alter table 表名 modify column 字段名 字段类型 新约束;
2.添加表级约束
alter table 表名 add 【constraint 约束名】 约束类型(字段名)【外键的引用】;
mysql> create table stuinfo(
   -> id int,
   -> stuName varchar(20),
   -> seat int,
   -> age int,
   -> majorid int,
   -> gender char(1)
   -> );
   
#修改列级约束    
mysql> #1.添加主键
mysql> alter table stuinfo modify column id int primary key;
Query OK, 0 rows affected (0.22 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> #2.添加非空约束
mysql> alter table stuinfo modify column stuname varchar(20) not null;
Query OK, 0 rows affected (0.20 sec)
Records: 0  Duplicates: 0  Warnings: 0


mysql> #3.添加默认约束
mysql> alter table stuinfo modify column age int default 18;
Query OK, 0 rows affected (0.12 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> #4.添加唯一约束
mysql> alter table stuinfo modify column seat int unique;
Query OK, 0 rows affected (0.16 sec)
Records: 0  Duplicates: 0  Warnings: 0

#修改表级约束
mysql> #添加外键
mysql> alter table stuinfo add foreign key(majorid) references major(id);
Query OK, 0 rows affected (0.19 sec)
Records: 0  Duplicates: 0  Warnings: 0

√删除约束

mysql> #删除主键
mysql> alter table stuinfo modify column id int;
Query OK, 0 rows affected (0.16 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> #删除非空约束
mysql> alter table stuinfo modify column stuName varchar(20);
Query OK, 0 rows affected (0.23 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> #删除默认的约束
mysql> alter table stuinfo modify column age int;
Query OK, 0 rows affected (0.20 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> #删除唯一约束
mysql> alter table stuinfo modify column seat int;
Query OK, 0 rows affected (0.17 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> #删除外键
mysql> alter table stuinfo drop foreign key fk_stuinfo_major;
Query OK, 0 rows affected (0.11 sec)
Records: 0  Duplicates: 0  Warnings: 0

5.标志列

1. 含义:又叫自增长列,可以不用手动的插入值,系统提供默认的序列值。
2. 特点:1. 标志列必须和键搭配使用(主键,外键,唯一)
        2.一个表至多一个标志列
        3.标志列的类型只能是数值型
        4.标志列可以通过 set auto_increment_increment=3 设置步长。可以通过手动设置起始值。

3.练习

mysql> create table tab_identity(
    -> id int primary key auto_increment,
    -> name varchar(20)
    -> );
Query OK, 0 rows affected (0.29 sec)

 mysql> select * from tab_identity ;
+----+------+
| id | name |
+----+------+
|  1 | keen |
+----+------+
1 row in set (0.00 sec)

mysql> 
mysql> insert into tab_identity (name) values('keen');
Query OK, 1 row affected (0.40 sec)

mysql> insert into tab_identity (name) values('keen');
Query OK, 1 row affected (0.13 sec)

mysql> insert into tab_identity (name) values('keen');
Query OK, 1 row affected (0.11 sec)

mysql> insert into tab_identity (name) values('keen');
Query OK, 1 row affected (0.18 sec)

mysql> select * from tab_identity ;
+----+------+
| id | name |
+----+------+
|  1 | keen |
|  2 | keen |
|  3 | keen |
|  4 | keen |
|  5 | keen |
+----+------+
5 rows in set (0.00 sec)
mysql> set auto_increment_increment=3;
Query OK, 0 rows affected (0.00 sec)

#设置步长。
mysql> insert into tab_identity (name) values('keen');
Query OK, 1 row affected (0.07 sec)

mysql> select * from tab_identity ;
+----+------+
| id | name |
+----+------+
|  1 | keen |
|  2 | keen |
|  3 | keen |
|  4 | keen |
|  5 | keen |
|  7 | keen |
+----+------+
6 rows in set (0.00 sec)



注:这是本人的学习笔记及练习,如果有错误的地方望指出一起讨论,谢谢!

你可能感兴趣的:(SQL-DDL(13) 数据定义语言(2))