pass039

表的完整性约束

二:not null与default

create table t1(
    id int primary key auto_increment,
    name varchar(16) not null,
    sex enum('male','female') not null default 'male'
);

PRIMARY KEY (PK)    标识该字段为该表的主键,可以唯一的标识记录 唯一标识且不为空
FOREIGN KEY (FK)    标识该字段为该表的外键
NOT NULL    标识该字段不能为空
UNIQUE KEY (UK)    标识该字段的值是唯一的
AUTO_INCREMENT    标识该字段的值自动增长(整数类型,而且为主键)
DEFAULT    为该字段设置默认值

UNSIGNED 无符号
ZEROFILL 使用0填充
mysql> desc t1;
+-------+-----------------------+------+-----+---------+----------------+
| Field | Type                  | Null | Key | Default | Extra          |
+-------+-----------------------+------+-----+---------+----------------+
| id    | int(11)               | NO   | PRI | NULL    | auto_increment |
| name  | varchar(16)           | NO   |     | NULL    |                |
| sex   | enum('male','female') | NO   |     | male    |                |
+-------+-----------------------+------+-----+---------+----------------+
不为空且没有设默认值,传值会报错
mysql> insert into t1(name) values('egon'),('lxx'),('alex');
Query OK, 3 rows affected (0.00 sec)
Records: 3  Duplicates: 0  Warnings: 0

mysql> select*from t1;
+----+------+------+
| id | name | sex  |
+----+------+------+
|  1 | egon | male |
|  2 | lxx  | male |
|  3 | alex | male |
+----+------+------+

 三:unique key (索引)约束角度功能  约束指定的字段唯一,传入的值不能重复

 egon命令行笔记

unique key

#针对单个字段的唯一
mysql> create database db2;
Query OK, 1 row affected (0.01 sec)

mysql> use db2;
Database changed
mysql> create table t1(
    ->     id int primary key auto_increment,
    ->     name varchar(16) not null,
    ->     sex enum('male','female') not null default 'male'
    -> );
Query OK, 0 rows affected (0.03 sec)

mysql> desc t1;
+-------+-----------------------+------+-----+---------+----------------+
| Field | Type                  | Null | Key | Default | Extra          |
+-------+-----------------------+------+-----+---------+----------------+
| id    | int(11)               | NO   | PRI | NULL    | auto_increment |
| name  | varchar(16)           | NO   |     | NULL    |                |
| sex   | enum('male','female') | NO   |     | male    |                |
+-------+-----------------------+------+-----+---------+----------------+
3 rows in set (0.04 sec)

mysql> insert into t1(name) values('egon'),('lxx'),('alex');
Query OK, 3 rows affected (0.00 sec)
Records: 3  Duplicates: 0  Warnings: 0

mysql> select*from t1;
+----+------+------+
| id | name | sex  |
+----+------+------+
|  1 | egon | male |
|  2 | lxx  | male |
|  3 | alex | male |
+----+------+------+
3 rows in set (0.00 sec)

mysql> create table t2(x int unique);
Query OK, 0 rows affected (0.02 sec)

mysql> create table t3(
    ->     x int,
    ->     y varchar(5),
    ->     unique key(x)
    -> );
Query OK, 0 rows affected (0.01 sec)

mysql> desc t2;
+-------+---------+------+-----+---------+-------+
| Field | Type    | Null | Key | Default | Extra |
+-------+---------+------+-----+---------+-------+
| x     | int(11) | YES  | UNI | NULL    |       |
+-------+---------+------+-----+---------+-------+
1 row in set (0.02 sec)

mysql> desc t3;
+-------+------------+------+-----+---------+-------+
| Field | Type       | Null | Key | Default | Extra |
+-------+------------+------+-----+---------+-------+
| x     | int(11)    | YES  | UNI | NULL    |       |
| y     | varchar(5) | YES  |     | NULL    |       |
+-------+------------+------+-----+---------+-------+
2 rows in set (0.01 sec)

mysql> show create table t2;
+-------+-----------------------------------------------------------------------
----------------------------------------+
| Table | Create Table
                                        |
+-------+-----------------------------------------------------------------------
----------------------------------------+
| t2    | CREATE TABLE `t2` (
  `x` int(11) DEFAULT NULL,
  UNIQUE KEY `x` (`x`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 |
+-------+-----------------------------------------------------------------------
----------------------------------------+
1 row in set (0.00 sec)

mysql> show create table t3;
+-------+-----------------------------------------------------------------------
-----------------------------------------------------------------------+
| Table | Create Table
                                                                       |
+-------+-----------------------------------------------------------------------
-----------------------------------------------------------------------+
| t3    | CREATE TABLE `t3` (
  `x` int(11) DEFAULT NULL,
  `y` varchar(5) DEFAULT NULL,
  UNIQUE KEY `x` (`x`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 |
+-------+-----------------------------------------------------------------------
-----------------------------------------------------------------------+
1 row in set (0.00 sec)

mysql> create table t4(
    ->     x int,
    ->     y varchar(5),
    ->     constraint uni_x unique key(x)
    -> );
Query OK, 0 rows affected (0.01 sec)

mysql> show create table t4;
+-------+-----------------------------------------------------------------------
---------------------------------------------------------------------------+
| Table | Create Table
                                                                           |
+-------+-----------------------------------------------------------------------
---------------------------------------------------------------------------+
| t4    | CREATE TABLE `t4` (
  `x` int(11) DEFAULT NULL,
  `y` varchar(5) DEFAULT NULL,
  UNIQUE KEY `uni_x` (`x`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 |
+-------+-----------------------------------------------------------------------
---------------------------------------------------------------------------+
1 row in set (0.00 sec)

mysql> desc t2;
+-------+---------+------+-----+---------+-------+
| Field | Type    | Null | Key | Default | Extra |
+-------+---------+------+-----+---------+-------+
| x     | int(11) | YES  | UNI | NULL    |       |
+-------+---------+------+-----+---------+-------+
1 row in set (0.01 sec)

mysql> insert into t2 values(null);
Query OK, 1 row affected (0.00 sec)

mysql> select*from t2;
+------+
| x    |
+------+
| NULL |
+------+
1 row in set (0.00 sec)

mysql> select*from t2;
+------+
| x    |
+------+
| NULL |
+------+
1 row in set (0.00 sec)

mysql> insert into t2 values(null);
Query OK, 1 row affected (0.00 sec)

mysql> select*from t2;
+------+
| x    |
+------+
| NULL |
| NULL |
+------+
2 rows in set (0.00 sec)

mysql> insert into t2 values(1);
Query OK, 1 row affected (0.00 sec)

mysql> select*from t2;
+------+
| x    |
+------+
| NULL |
| NULL |
|    1 |
+------+
3 rows in set (0.00 sec)


 unique多列联合起来唯一
mysql> create table service(
    ->     ip varchar(15),
    ->     port int,
    ->     unique key(ip,port)
    ->     );
Query OK, 0 rows affected (0.02 sec)

mysql> desc service;
+-------+-------------+------+-----+---------+-------+
| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| ip    | varchar(15) | YES  | MUL | NULL    |       |
| port  | int(11)     | YES  |     | NULL    |       |
+-------+-------------+------+-----+---------+-------+
2 rows in set (0.02 sec)

mysql> insert into service values('1.1.1.1',3306);
Query OK, 1 row affected (0.00 sec)

mysql> select*from service;
+---------+------+
| ip      | port |
+---------+------+
| 1.1.1.1 | 3306 |
+---------+------+
1 row in set (0.00 sec)

mysql> insert into service values('1.1.1.1',3306);
ERROR 1062 (23000): Duplicate entry '1.1.1.1-3306' for key 'ip'
mysql> insert into service values('1.1.1.1',3356);
Query OK, 1 row affected (0.00 sec)

mysql> insert into service values('1.1.1.2',3306);
Query OK, 1 row affected (0.00 sec)

mysql> select*from service;
+---------+------+
| ip      | port |
+---------+------+
| 1.1.1.1 | 3306 |
| 1.1.1.1 | 3356 |
| 1.1.1.2 | 3306 |
+---------+------+
3 rows in set (0.00 sec)

四 primary key
站在约束角度看primary key=not null unique  相当于结合体
以后但凡建表,必须注意:
1、必须有且只有一个主键
2、通常是id字段被设置为主键

create table t5(
    id int primary key auto_increment,
);

mysql> desc t5;
+-------+---------+------+-----+---------+----------------+
| Field | Type    | Null | Key | Default | Extra          |
+-------+---------+------+-----+---------+----------------+
| id    | int(11) | NO   | PRI | NULL    | auto_increment |
+-------+---------+------+-----+---------+----------------+
1 row in set (0.01 sec)

mysql> insert into t5 values(null);
Query OK, 1 row affected (0.00 sec)

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

mysql> create table t6(x int primary key);
Query OK, 0 rows affected (0.02 sec)

mysql> desc t6;
+-------+---------+------+-----+---------+-------+
| Field | Type    | Null | Key | Default | Extra |
+-------+---------+------+-----+---------+-------+
| x     | int(11) | NO   | PRI | NULL    |       |
+-------+---------+------+-----+---------+-------+
1 row in set (0.03 sec)

mysql> insert into t6 values(null);
ERROR 1048 (23000): Column 'x' cannot be null
mysql> insert into t6 values(1);
Query OK, 1 row affected (0.00 sec)

mysql> insert into t6 values(1);
ERROR 1062 (23000): Duplicate entry '1' for key 'PRIMARY'
mysql> select*from t6;
+---+
| x |
+---+
| 1 |
+---+
1 row in set (0.00 sec)


#不能指定多个主键的情况
mysql> create table t7(x int primary key,y int primary key);
ERROR 1068 (42000): Multiple primary key defined

#未指定主键默认自定义主键
mysql> create table t7(x int,y varchar(5),z int not null unique);
Query OK, 0 rows affected (0.02 sec)

mysql> desc t7;
+-------+------------+------+-----+---------+-------+
| Field | Type       | Null | Key | Default | Extra |
+-------+------------+------+-----+---------+-------+
| x     | int(11)    | YES  |     | NULL    |       |
| y     | varchar(5) | YES  |     | NULL    |       |
| z     | int(11)    | NO   | PRI | NULL    |       |
+-------+------------+------+-----+---------+-------+

只要是innodb存储引擎一定会要主键
mysql> create table t8(x int,y varchar(5),z int);
Query OK, 0 rows affected (0.03 sec)

mysql> create table t9(x int,y varchar(5),z int not null unique,m int not null u
nique);
Query OK, 0 rows affected (0.22 sec)

mysql> desc t9;
+-------+------------+------+-----+---------+-------+
| Field | Type       | Null | Key | Default | Extra |
+-------+------------+------+-----+---------+-------+
| x     | int(11)    | YES  |     | NULL    |       |
| y     | varchar(5) | YES  |     | NULL    |       |
| z     | int(11)    | NO   | PRI | NULL    |       |
| m     | int(11)    | NO   | UNI | NULL    |       |
+-------+------------+------+-----+---------+-------+
4 rows in set (0.03 sec)

表的存储引擎

自己处理自己的数据用不同的文件来处理,文件分不同的类型,表也分不同的类型

表的类型有一个专有的名词,称之为存储引擎,为一个表指定了存储引擎,相当于创建了表的类型

 transaction交易

rowlevel行级锁

四:primary key(索引)约束角度的功能

站在约束角度看primary key=not null unique

index key(索引)

目的是为了加速查询用的,不常用

五:foreign key

存储引擎

create table t12(x int)engine='myisam';
create table t13(x int)engine='innodb';
create table t14(x int)engine='memory';
create table t15(x int)engine='blackhole';

frm存表的结构
myd代表的是myisam存储引擎的数据文件
myi代表的是index的意思存放的是索引
ibd代表的是又存数据又存索引

客户端不存数据,存的是服务端

  

外键

表之间的关系

表其他相关操作

猜你喜欢

转载自www.cnblogs.com/wangmiaolu/p/9333610.html