4.表的操作

表就相当于文件,表中的一条记录就相当于文件的一行内容,不同的是,表中的一条记录有对应的标题,称为表的字段

还记得我们之前写过的‘员工信息表作业’么?存储这员工信息的文件是这样的:

```
id,name,age,sex,phone,job
1,Alex,83,female,13651054608,IT
2,Egon,26,male,13304320533,Tearcher
3,nezha,25,male,13332353222,IT
4,boss_jin,40,male,13332353333,IT
```

如果把上面这个文件改成一张表,应该是下面这个样子

| id   | name     | age  | sex    | phone       | job     |
| ---- | -------- | ---- | ------ | ----------- | ------- |
| 1    | Alex     | 83   | female | 13651054608 | IT      |
| 2    | Egon     | 26   | male   | 13304320533 | Teacher |
| 3    | nezha    | 25   | male   | 13332353222 | IT      |
| 4    | boss_jin | 40   | male   | 13332353333 | IT      |

```
id,name,age,sex,phone,job称为字段,实际上是表头,其余的,一行内容称为一条记录 
```

##### 1.表的创建

```mysql
 create table 表名(字段 数据类型(宽度) 约束,
                      字段 数据类型(宽度) 约束,
                      字段 数据类型(宽度) 约束,
    )
```

##### 2.表的约束

```mysql
unsigned 无符号的 :数字
not null 非空约束 :约束不能为空  注意'严格模式'
default  默认值 : 设置默认值 # sex enum('male','female') not null default 'male', #大部分是男的
unique 唯一约束 : 约束不能重复
约束的角度上 : not null + unique = primary key
auto_increment 自动增加
primary key 主键约束
foreign key 外键约束
```

###### 2.1 not null非空约束

```mysql
create table t1(id int not null,
                name char(12) not null,
                age tinyint unsigned
);
create table t2(id int not null,
                name char(12) not null,
                age tinyint unsigned not null default 18
);
create table t2(id int not null,
                name char(12) not null,
                age tinyint unsigned default 18
);
select name from 表名 where name is null;
select * from 表名 where name is null;出现所有列
```

非空不生效

```sql
设置严格模式:
    不支持对not null字段插入null值
    不支持对自增长字段插入”值
    不支持text字段有默认值

直接在mysql中生效(重启失效):
mysql>set sql_mode="STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION";

配置文件添加(永久失效):
sql-mode="STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION"
```

###### 2.2 unique 唯一约束

```mysql
id username ident phone mail_number
create table t3(
    id int unique,
    username char(18) unique,
    age tinyint unsigned
    );
```

###### 2.3 非空+唯一

约束的角度上: not null +unique

```mysql
    如果一张表中没有设置primary key 主键,那么第一个设置非空+唯一的字段会被设置成主键
    一张表中只能有一个主键
    create table t4(
        id int not null unique,
        username  char(18) not null unique,
        age tinyint unsigned
    );
```

主键的约束作用 = 非空+唯一 特点:一张表只能有一个主键

```mysql
create table t5(
    id int primary key,
    username char(18) not null unique,
    age tinyint unsigned
    );
```

###### 2.4 database 当前库

```mysql
show databases;查看一共有多少库
select database();查看当前所在的库
```

###### 2.5 null能不能重复

**unique 能不能重复插入多个null ---可以**

对于mysql 来说 数据与数据之间相等就是重复 

null 不能用 '='判断

```mysql
create table t(
    id1 int unique,
    id2 int 
)
值不能重复,可以insert多个null,
insert into t values(null,1);
insert into t values(null,1);
```

###### 2.6 联合唯一

```mysql
create table t2(
    id int primary key,
    servername char(12) not null,
    ip char(15),
    port int,
    unique(ip,port)#联合唯一    
    );
insert into t2 values(1,'输入法皮肤','10.10.3.1',8800);
insert into t2 values(2,'mysql','10.10.2.4',3306);
insert into t2 values(3,'mysql','10.10.2.5',3306);
insert into t2 values(4,'输入法推荐','10.10.3.1',8802);
```

![1571551623670](typora-user-images/1571551623670.png)

![1564560572244](.	ypora-user-images1564560572244.png)

###### 2.7 auto_increment 自增的

+ 第一 只能操作数字
+ 第二 自带非空属性
+ 第三 只能对unique字段进行设置
+ 第四 不受删除影响的

```mysql
create table t3(
    id int unique auto_increment,
    name char(12) not null
);
truncate table t;删除自增
prinary key 主键,在不设置的情况下,默认为第一个
    约束的角度上 : not null + unique
    一张表只能有一个主键
create table t4(
    id int primary key auto_increment,
    name char(14)
)
```


###### 2.8 联合主键

```mysql
create table t(
    family_name char(4),
    name char(12),
    primary key(family_name,name)
);
等同于
create table t5(
    family_name char(4) not null,
    name char(12) not null,
    unique(family_name,name)
)
```


###### 2.9 外键约束

外键约束 : 对应外表中的字段至少是unique的,推荐使用主键作为关联字段

foreign key

+ 约束的字段至少unique

+ 级联删除 on delete cascade

+ 级联更新 on update cascade

```mysql
需要先创建class 表,并且需要先创建班
create table class(
    id int primary key auto_increment,
    cname char(12) not null unique,
    start_date date,
    period char(12),
    course char(12),
    teacher char(12)
)
create table student(
    id int primary key auto_increment,
    name char(12) not null,
    gender enum('male','female'),
    cid int,
    foreign key(cid) references class(id) on delete cascade on update cascade
)
```


###### 2.10 数据的插入操作

```mysql
insert into class select * from class1(将class1里的数据写入到class这个表中)
insert into class3(id,name) select id,cname from class;
```
原文地址:https://www.cnblogs.com/pythonblogs/p/11963797.html