Additions and deletions to change search library table

Operation folder (library)

CRUD


create database 文件夹名;
实列
create database db1;   
删除
drop database 库名;
实列
drop database db1; 

#官方没有直接修改数据库名称的命令

use 库名
show databases;  #查看所有文件夹名
select user();  #查看当前用户名
select database() 查看当前库
show tables; #查看当前库中有多少张表 先切换

Create a table change table fields

You must study the data type table constraint to the table when you create the table of learning

Create a table

#先创建一个库
create database day01;
# 先切换到文件夹下:use day01;
show tables; #查看当前库中有多少张表 先切换
# 创建表 要先切到库
create table 表名(
字段名1 类型[(宽度) 约束条件],
字段名2 类型[(宽度) 约束条件],
字段名3 类型[(宽度) 约束条件]
);
实列
create table staff_info (
id int,name varchar(50),
age int(3),
sex enum('male','female'),
phone bigint(11),
job char(5)
);

Structural modifications table lookup table name

改表名
alter table 旧表名 rename 新名字;
实列
alter table jbiao rename xbiao;


show tables;#查看当前库中有多少张表
desc 表名;#查看表的结构,
show create table 表名;#查看建表语句 更详细

New field

已经键表在添加新字段 默认最后
alter table 表名 add (字段名称) (数据类型) (约束条件);
实例
alter table day01 add id int primary key auto_increment;
添加name字段之后
alter table 表名 add 要添加的字段名 数据类型 约束 after 某字段后面;
实列
alter table student10 add stu_num varchar(10) not null after name;     
添加到最前面
alter table 表名 add 要添加的字段名 数据类型 约束 first; 
实列
alter table student10 add sex enum('male','female') default 'male'  first; 

To delete a table or table field


drop table 表名;
实列
drop table userinfo;
删除字段
alter table 表名 drop 字段名称;
实列
alter table day01 drop id;

Add and delete foreign key relationships

创建表完成之后,后添加外键关系
alter table 表名 add foreign key(字段) references 另一个表名(字段);
实列
alter table book add foreign key(pid) references publish(id);
    
删除外键关系
先查看在删除
alter table 表名 drop foreign key (外键名称);
实列 外键名称 用show create table 表名; 查看
alter table book drop foreign key book_ibfk_1;

Delete or modify the related field

Because the default fields are associated with a strict mode, so it can not be removed unless authorized to give him a start cascading update or delete keys in the following table has a good case we must first remove the foreign key relationships in the deleted

The first case has been key to a good table

我们必须先吧表关系解除
场景:book表和publish表为多对一关系,book表的pid字段外键关联到了publish表的id字段
1 查看外键关系名称: 就是book_ibfk_1
    show create table book; 
        | book  | CREATE TABLE `book` (
          `id` int(11) NOT NULL AUTO_INCREMENT,
          `name` char(10) DEFAULT NULL,
          `pid` int(11) DEFAULT NULL,
          PRIMARY KEY (`id`),
          KEY `pid` (`pid`),
          CONSTRAINT `book_ibfk_1` FOREIGN KEY (`pid`) REFERENCES `publish` (`id`)
        ) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8 |


2 删除外键关系
    alter table book drop foreign key book_ibfk_1(外键名称);
3 删除字段
    alter table publish drop id(字段名称);
4 添加字段
    alter table publish add id(字段名称) int(数据类型) primary key auto_increment(约束条件);

5 创建表完成之后,后添加外键关系
    alter table book add foreign key(pid) references publish(id);

Foreign key name

指定的外键名称  可以随意更改  fk_t1_publish
创建表时:
create table t1(
    id int,
    pid int,
    constraint fk_t1_publish foreign key(pid) references publish(id);
)
创建表完成之后,后添加外键关系
    alter table book add constraint fk_t1_publish foreign key(pid) references publish(id);

Several cascade mode

Strict mode (default)

删除:从表记录不存在时,主表才可以删除。删除从表,主表不变
更新:从表记录不存在时,主表才可以更新。更新从表,主表不变
严格模式(默认的),外键有强制约束效果,被关联字段不能随意删除和修改

cascade mode:

删除:删除主表时自动删除从表。删除从表,主表不变
更新:更新主表时自动更新从表。更新从表,主表不变
外键有强制约束效果,**被关联字段删除或者修改,关联他的那么字段数据会随之删除或者修改

Examples

foreign key(pid) references publish(id) on delete cascade on update cascade;

set null mode:

删除:删除主表时自动更新从表值为NULL。删除从表,主表不变
更新:更新主表时自动更新从表值为NULL。更新从表,主表不变
被关联字段删除时,关联他的字段数据会置成null

Examples

foreign key(pid) references tt2(id) on delete set null
补充:级联set null的用法和示例
mysql> create table tt2(id int primary key auto_increment,name char(10));

mysql> create table tt3(id int,pid int,foreign key(pid) references tt2(id) on delete set null);

Guess you like

Origin www.cnblogs.com/saoqiang/p/12388378.html