用户及权限操作:  

管理员登录:mysql -uroot -p
用户设置密码:set password=password(密码);
查看数据库所有用户:select * from mysql.user;
查看用户权限:show grants for 用户名@网络地址 [identified by 密码]普通用户登录:mysql -u用户名 -h网络地址 -p
查看当前用户:select user();
查看当前数据库:select database();
当前时间:now()
查看当前数据库是否是严格模式:select @@sql_mode;创建用户:create user 用户名@网络地址 identified by 密码;
用户授权:grant all[SELECT INSERT UPDATE DELETE] on 数据库名.表名 to 用户名@网络地址(%表示所有) identified by 用户密码;
取消授权:revoke all[SELECT INSERT UPDATE DELETE ] on  数据库名.表名 from 用户名@网络地址 [identified by 密码];
修改普通用户密码:set password for 用户名=password(密码);
查看当前用户权限:show grants;

mysql用户管理基础操作

库操作:

查看数据库:show  databases;查看当前数据库:select database();创建数据库:create database 数据库名 [charset utf-8];选择数据库:use 数据库名;删除数据库:drop database 数据库名;修改数据库:alter database 数据库名 charset utf8;

mysql数据库操作

表操作:  

MySQL支持的数据类型   参考博客:https://www.cnblogs.com/Eva-J/articles/9683316.html  参考博客:https://www.cnblogs.com/clschao/articles/9959559.html 

1.数值类型(有约束条件无符号unsigned):int:        整型4字节  -2^31-2147483647      无符号:2^32-1float:      单精度浮点数4字节double:     双精度浮点数8字节decimal:     小数值2.字符类型:char:       定长字符,能表示的字符个数有限(0-255),读写快varchar:    变长字符,能表示的字符个数多(0-65535),读写慢3. 时间和日期类型:(系统内置函数now()获取当前时间)year:       年date:       年月日time:       时分秒datetime:   年月日时分秒timestamp:  年月日时分秒(1970-2038年之间,如果不设置这默认显示当前时间)4.enum 和set 类型:enum():     枚举,单选,自动屏蔽不存在的项set():      集合,多选,自动屏蔽不存在且去重mysql支持的数据类型

mysql支持的数据类型

MySQL中的约束条件  参考博客:https://www.cnblogs.com/Eva-J/articles/9687915.html  参考博客:https://www.cnblogs.com/clschao/articles/9968396.html

MySQL中的约束条件(可配合使用):1.整型无符号:    unsiged2.唯一:         unique            只能约束数据类型内不能重复,但不能约束null3.非空:         not null4.默认值         default 值5.自增:         auto_increment    必须为数值类型,且设置唯一unique6.主键:         primary key7.外键:         foreign key
在没有设置主键的情况下,遇到约束条件为非空唯一时系统默认为主键!

MySQL中的完整性约束条件

MySQL中表的操作
  创建表:     

#语法:
create table 表名(
字段名1 类型[(宽度) 约束条件],
字段名2 类型[(宽度) 约束条件],
字段名3 类型[(宽度) 约束条件]
);#注意:
1. 在同一张表中,字段名是不能相同
2. 宽度和约束条件可选
3. 字段名和类型是必须的表创建语句

表创建语句

create table t1(id int,name char...);实例:# create table class(#     cid int primary key auto_increment,#     caption char(4) unique not null         #不设置非空且唯一的话按主键排序# );表创建实例

表创建实例

  查看表及表结构:  

查看数据库中的表:show tables;
查看表结构:describe t1; #查看表结构,可简写为:desc 表名show create table t1\G; #查看表详细结构,可加\Gdesc 表名;

查看数据库表和表结构

  删除表:

删:drop table 表名;

数据库中表的删除

  修改表结构(字段、约束等):  

表的操作(alter table 表名......):(1)改名alter table 表名 rename 新表名;(2)添加表字段alter table 表名 add 字段 类型(长度) [约束] [first/after 字段];(3)删除表字段alter table 表名 drop 字段 ;(4)修改表字段alter table 表名 change 字段  新字段 类型(长度);(5)修改表字段的类型(长度)、约束alter table 表名 modify 字段 类型(长度)[约束] ;(6)修改表中字段的顺序alter table 表名 change 字段  新字段 类型(长度)[约束] [first/after 字段];alter table 表名 modify 字段 类型(长度)[约束] [first/after 字段] ;(7)添加/删除外键alter table 表名  add constraint 名称描述 foreign key (字段) references 关系表名 (关系表内字段)on update cascade on delete cascade;alter table 表名 drop  foreign key    名称描述;

表结构修改操作

(1)创建一个无约束表t1
create table t1(id int,name char(4),sex enum('male','female')
);mysql> insert into t1 values(2147483647,'yang',null);  #有符号正常在规定范围内正常写入
mysql> select * from t1;+------------+------+------+| id         | name | sex  |+------------+------+------+| 2147483647 | yang | NULL |+------------+------+------+1 row in set (0.00 sec)mysql> insert into t1 values(2147483648,'peng','nihao');  #超出范围之后会自动按最大值写入,enum超出单选范围自动屏蔽
Query OK, 1 row affected, 2 warnings (0.01 sec)mysql> select * from t1;+------------+------+------+| id         | name | sex  |+------------+------+------+| 2147483647 | yang | NULL || 2147483647 | peng |      |            +------------+------+------+2 rows in set (0.00 sec)(2)约束条件的表格t2
create table t2(id int unsigned,name char(4),sex enum('male','female') not null
);mysql> insert into t2 values(2147483648,'peng','nihao');  #约束无符号之后,超出有符号的范围正常写入;enum超出单选范围自动屏蔽
Query OK, 1 row affected, 1 warning (0.00 sec)mysql> select * from t2;+------------+------+-----+| id         | name | sex |+------------+------+-----+| 2147483648 | peng |     |+------------+------+-----+1 row in set (0.00 sec)mysql>insert into t2 (id,name) values((21474836488,'yang');   #sex二选一没给值报错
mysql> insert into t2 values(2147483648,'peng','male');        #给正确的值直接写入
Query OK, 1 row affected (0.01 sec)mysql> select * from t2;+------------+------+------+| id         | name | sex  |+------------+------+------+| 2147483648 | peng |      || 2147483648 | peng | male |+------------+------+------+2 rows in set (0.00 sec)

1.unsigned、not null约束

# create table t3(
#     id int unique,
#     name char(4),
#     sex enum('male','female')default 'male'
# );mysql> desc t3;+-------+-----------------------+------+-----+---------+-------+| Field | Type                  | Null | Key | Default | Extra |+-------+-----------------------+------+-----+---------+-------+| id    | int(11)               | YES  | UNI | NULL    |       || name  | char(4)               | YES  |     | NULL    |       || sex   | enum('male','female') | YES  |     | male    |       |+-------+-----------------------+------+-----+---------+-------+3 rows in set (0.02 sec)
mysql> insert into t3(id,name) values(1,'yang');        #不加sex的值会自动使用default默认的
Query OK, 1 row affected (0.00 sec)mysql> select * from t3;+------+------+------+| id   | name | sex  |+------+------+------+|    1 | yang | male |+------+------+------+1 row in set (0.00 sec)mysql> insert into t3 values(1,'peng','female');    #id设置唯一,给了一个重复值直接报错
ERROR 1062 (23000): Duplicate entry '1' for key 'id'

2.default 、unique约束

# ip + port
# 192.168.16.13  mysql 3306
# 192.168.16.13  kugou 8080
# 192.168.16.13  flask 5000
# 192.168.16.15  mysql 3306
# 192.168.16.16  mysql 3306# create table service(
#   id int,
#   ip char(15),
#   name char(15),
#   port int(5),
#   unique(ip,port)             #ip+port联合唯一
# );
mysql> desc service;+-------+----------+------+-----+---------+-------+| Field | Type     | Null | Key | Default | Extra |+-------+----------+------+-----+---------+-------+| id    | int(11)  | YES  |     | NULL    |       || ip    | char(15) | YES  | MUL | NULL    |       || name  | char(15) | YES  |     | NULL    |       || port  | int(5)   | YES  |     | NULL    |       |+-------+----------+------+-----+---------+-------+4 rows in set (0.01 sec)mysql> insert into service values(1,'192.168.16.13',  'mysql', 3306),(2,'192.168.16.13',  'kugou', 8080),(3,'192.168.16.13',  'flask', 5000)-> ,(4,'192.168.16.15' ,'mysql',3306),(5,'192.168.16.16','mysql', 3306);
Query OK, 5 rows affected (0.01 sec)
Records: 5  Duplicates: 0  Warnings: 0mysql> select * from service;+------+---------------+-------+------+| id   | ip            | name  | port |+------+---------------+-------+------+|    1 | 192.168.16.13 | mysql | 3306 ||    2 | 192.168.16.13 | kugou | 8080 ||    3 | 192.168.16.13 | flask | 5000 ||    4 | 192.168.16.15 | mysql | 3306 ||    5 | 192.168.16.16 | mysql | 3306 |+------+---------------+-------+------+5 rows in set (0.00 sec)mysql> insert into service values(6,'192.168.16.13',  'hha', 3306);         #重复的ip+port直接报错
ERROR 1062 (23000): Duplicate entry '192.168.16.13-3306' for key 'ip'

3.联合唯一unique(字段1,字段2)

# create table t6(
#   id int unique auto_increment,
#   name char(4) ,
#   sex enum('male','female')
# );
mysql> desc t6;+-------+-----------------------+------+-----+---------+----------------+| Field | Type                  | Null | Key | Default | Extra          |+-------+-----------------------+------+-----+---------+----------------+| id    | int(11)               | NO   | PRI | NULL    | auto_increment || name  | char(4)               | YES  |     | NULL    |                || sex   | enum('male','female') | YES  |     | NULL    |                |+-------+-----------------------+------+-----+---------+----------------+3 rows in set (0.01 sec)mysql> insert into t6 (name,sex) values('yang','male');         #id 自动从1开始
Query OK, 1 row affected (0.01 sec)mysql> select * from t6;+----+------+------+| id | name | sex  |+----+------+------+|  1 | yang | male |+----+------+------+1 row in set (0.00 sec)mysql> insert into t6 (name,sex) values('peng','haha');         #id自动变成2
Query OK, 1 row affected, 1 warning (0.01 sec)
mysql> select * from t6;    +----+------+------+| id | name | sex  |+----+------+------+|  1 | yang | male ||  2 | peng |      |+----+------+------+2 rows in set (0.00 sec)   

4.auto_increment自增(int型且unique)

(1)
# create table t4(
#     id int unique ,
#     name char(4) unique not null,             #没有设置主键时,默认为主键PRI
#     sex enum('male','female')
# );
mysql> desc t4;+-------+-----------------------+------+-----+---------+-------+| Field | Type                  | Null | Key | Default | Extra |+-------+-----------------------+------+-----+---------+-------+| id    | int(11)               | YES  | UNI | NULL    |       || name  | char(4)               | NO   | PRI | NULL    |       || sex   | enum('male','female') | YES  |     | NULL    |       |+-------+-----------------------+------+-----+---------+-------+3 rows in set (0.01 sec)(2)
create table t5(id int primary key ,            #设置了id为主键name char(4) unique not null,sex enum('male','female')
);
mysql> desc t5;+-------+-----------------------+------+-----+---------+-------+| Field | Type                  | Null | Key | Default | Extra |+-------+-----------------------+------+-----+---------+-------+| id    | int(11)               | NO   | PRI | NULL    |       || name  | char(4)               | NO   | UNI | NULL    |       || sex   | enum('male','female') | YES  |     | NULL    |       |+-------+-----------------------+------+-----+---------+-------+3 rows in set (0.01 sec)

5.primary key 约束==unique+not null(没有设置主键默认为主键)

# create table t7(
#     id1 int,
#     num int,
#     primary key(id1,num)
# );
mysql> desc t7;+-------+---------+------+-----+---------+-------+| Field | Type    | Null | Key | Default | Extra |+-------+---------+------+-----+---------+-------+| id1   | int(11) | NO   | PRI | 0       |       || num   | int(11) | NO   | PRI | 0       |       |+-------+---------+------+-----+---------+-------+2 rows in set (0.01 sec)

6.联合主键 : 约束多个字段各自不能为空,并且联合唯一

# 表2 班级表 cid class_name
# create table clas(
#     cid int primary key,
#     class_name char(20)
# )
# 表1 学生表 id name class_id
# create table stu(
#     id int primary key ,
#     name char(18),
#     class_id int,
#     foreign Key(class_id) references clas(cid)            #设置外键连接到clas表的主键cid
# )
mysql> insert into clas values(1,'一');          #在clas表中创建一个班级
Query OK, 1 row affected (0.01 sec)mysql> select * from clas;+-----+------------+| cid | class_name |+-----+------------+|   1 | 一         |+-----+------------+1 row in set (0.00 sec)mysql> insert into stu values(1,'yang',1);  #stu表中添加外键班级信息(在clas中必须存在,否则报错)
Query OK, 1 row affected (0.00 sec)mysql> select * from stu;+----+------+----------+| id | name | class_id |+----+------+----------+|  1 | yang |        1 |+----+------+----------+1 row in set (0.00 sec)mysql> insert into stu values(1,'yang',2);  #stu表中添加外键不存在的班级信息直接报错
ERROR 1062 (23000): Duplicate entry '1' for key 'PRIMARY'mysql> insert into clas values(2,'二');      #在clas表中创建一个班级2
Query OK, 1 row affected (0.00 sec)
mysql> select * from clas;+-----+------------+| cid | class_name |+-----+------------+|   1 | 一         ||   2 | 二         |+-----+------------+2 rows in set (0.00 sec)
#在设置了外键之后会对关联表的数据更新、删除产生约束
mysql> update clas set cid=3 where cid=1;
ERROR 1451 (23000): Cannot delete or update a parent row: a foreign key constraint fails (`day038`.`stu`, CONSTRAINT `stu_ibfk_1` FOREIGN KEY (`class_id`) REFERENCES `clas` (`cid`))
mysql> delete from clas where cid=1;
ERROR 1451 (23000): Cannot delete or update a parent row: a foreign key constraint fails (`day038`.`stu`, CONSTRAINT `stu_ibfk_1` FOREIGN KEY (`class_id`) REFERENCES `clas` (`cid`))
#未关联的可以自行修改
mysql> update clas set cid=3 where cid=2;
Query OK, 1 row affected (0.00 sec)
mysql> delete from clas where cid=3;
Query OK, 1 row affected (0.00 sec)

7.foreign key 外键约束(导致关联的相关数据的更新、删除操作不成)

# 表2 班级表 cid class_name
# create table clas(
#     cid int primary key,
#     class_name char(20)
# )
(1)外键关联: 表1 学生表 id name class_id
# create table stu(
#     id int primary key ,
#     name char(18),
#     class_id int,
#     foreign Key(class_id) references clas(cid)            #设置外键连接到clas表的主键cid
# )# (2)级联更新 级联删除(# 表1 学生表 id name class_id)
# create table stu(
#     id int primary key ,
#     name char(18),
#     class_id int,
#     foreign key(class_id) references clas(cid) on update cascade on delete cascade
# );

8.外键的级联更新、删除操作(修改一个自动同步更新,删除字后默认为空)

  参考博客:https://www.cnblogs.com/Eva-J/articles/9677452.html#_label2

1、首先创建一个数据表table_test:
create table table_test(
`id` varchar(100) NOT NULL,
`name` varchar(100) NOT NULL,
PRIMARY KEY (`name`)
);
2、如果发现主键设置错了,应该是id是主键,但如今表里已经有好多数据了,不能删除表再重建了,仅仅能在这基础上改动表结构。
先删除主键
alter table table_test drop primary key;
然后再增加主键
alter table table_test add primary key(id);
注:在增加主键之前,必须先把反复的id删除掉。

alter操作主键

创建press表
CREATE TABLE `press` (`id` int(11) NOT NULL,`name` char(10) DEFAULT NULL,PRIMARY KEY (`id`)
) ;创建book表
CREATE TABLE `book` (`id` int(11) DEFAULT NULL,`bk_name` char(12) DEFAULT NULL,`press_id` int(11) NOT NULL,KEY `press_id` (`press_id`)
) ;为book表添加外键
alter table book add constraint fk_id foreign key(press_id) references press(id);删除外键
alter table book drop foreign key fk_id;

alter操作外键

create table t(id int unique,name char(10) not null);#去掉null约束
alter table t modify name char(10) null;
# 添加null约束
alter table t modify name char(10) not null;# 去掉unique约束
alter table t drop index id;
# 添加unique约束
alter table t modify id int unique;alter处理null和unique约束

alter操作非空唯一和索引

语法:
1. 修改表名ALTER TABLE 表名 RENAME 新表名;2. 增加字段ALTER TABLE 表名ADD 字段名  数据类型 [完整性约束条件…],ADD 字段名  数据类型 [完整性约束条件…];3. 删除字段ALTER TABLE 表名 DROP 字段名;4. 修改字段ALTER TABLE 表名 MODIFY  字段名 数据类型 [完整性约束条件…];ALTER TABLE 表名 CHANGE 旧字段名 新字段名 旧数据类型 [完整性约束条件…];ALTER TABLE 表名 CHANGE 旧字段名 新字段名 新数据类型 [完整性约束条件…];5.修改字段排列顺序/在增加的时候指定字段位置ALTER TABLE 表名ADD 字段名  数据类型 [完整性约束条件…]  FIRST;ALTER TABLE 表名ADD 字段名  数据类型 [完整性约束条件…]  AFTER 字段名;ALTER TABLE 表名CHANGE 字段名  旧字段名 新字段名 新数据类型 [完整性约束条件…]  FIRST;ALTER TABLE 表名MODIFY 字段名  数据类型 [完整性约束条件…]  AFTER 字段名;

alter表结构修改详解

  单表操作实例:  

mysql> desc staff_info;
+-------+-----------------------+------+-----+---------+-------+
| Field | Type                  | Null | Key | Default | Extra |
+-------+-----------------------+------+-----+---------+-------+
| id    | int(11)               | YES  |     | NULL    |       |
| name  | varchar(50)           | YES  |     | NULL    |       |
| age   | int(3)                | YES  |     | NULL    |       |
| sex   | enum('male','female') | YES  |     | NULL    |       |
| phone | bigint(11)            | YES  |     | NULL    |       |
| job   | varchar(11)           | YES  |     | NULL    |       |
+-------+-----------------------+------+-----+---------+-------+
rows in set (0.00 sec)# 表重命名
mysql> alter table staff_info rename staff;
Query OK, 0 rows affected (0.00 sec)mysql> desc staff;
+-------+-----------------------+------+-----+---------+-------+
| Field | Type                  | Null | Key | Default | Extra |
+-------+-----------------------+------+-----+---------+-------+
| id    | int(11)               | YES  |     | NULL    |       |
| name  | varchar(50)           | YES  |     | NULL    |       |
| age   | int(3)                | YES  |     | NULL    |       |
| sex   | enum('male','female') | YES  |     | NULL    |       |
| phone | bigint(11)            | YES  |     | NULL    |       |
| job   | varchar(11)           | YES  |     | NULL    |       |
+-------+-----------------------+------+-----+---------+-------+
rows in set (0.00 sec)# 删除sex列
mysql> alter table staff drop sex;
Query OK, 0 rows affected (0.02 sec)
Records: 0  Duplicates: 0  Warnings: 0mysql> desc staff;
+-------+-------------+------+-----+---------+-------+
| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id    | int(11)     | YES  |     | NULL    |       |
| name  | varchar(50) | YES  |     | NULL    |       |
| age   | int(3)      | YES  |     | NULL    |       |
| phone | bigint(11)  | YES  |     | NULL    |       |
| job   | varchar(11) | YES  |     | NULL    |       |
+-------+-------------+------+-----+---------+-------+
rows in set (0.01 sec)# 添加列
mysql> alter table staff add sex enum('male','female');
Query OK, 0 rows affected (0.03 sec)
Records: 0  Duplicates: 0  Warnings: 0# 修改id的宽度
mysql> alter table staff modify id int(4);
Query OK, 0 rows affected (0.02 sec)
Records: 0  Duplicates: 0  Warnings: 0mysql> desc staff;
+-------+-----------------------+------+-----+---------+-------+
| Field | Type                  | Null | Key | Default | Extra |
+-------+-----------------------+------+-----+---------+-------+
| id    | int(4)                | YES  |     | NULL    |       |
| name  | varchar(50)           | YES  |     | NULL    |       |
| age   | int(3)                | YES  |     | NULL    |       |
| phone | bigint(11)            | YES  |     | NULL    |       |
| job   | varchar(11)           | YES  |     | NULL    |       |
| sex   | enum('male','female') | YES  |     | NULL    |       |
+-------+-----------------------+------+-----+---------+-------+
rows in set (0.01 sec)# 修改name列的字段名
mysql> alter table staff change name sname varchar(20);
Query OK, 4 rows affected (0.03 sec)
Records: 4  Duplicates: 0  Warnings: 0mysql> desc staff;
+-------+-----------------------+------+-----+---------+-------+
| Field | Type                  | Null | Key | Default | Extra |
+-------+-----------------------+------+-----+---------+-------+
| id    | int(4)                | YES  |     | NULL    |       |
| sname | varchar(20)           | YES  |     | NULL    |       |
| age   | int(3)                | YES  |     | NULL    |       |
| phone | bigint(11)            | YES  |     | NULL    |       |
| job   | varchar(11)           | YES  |     | NULL    |       |
| sex   | enum('male','female') | YES  |     | NULL    |       |
+-------+-----------------------+------+-----+---------+-------+
rows in set (0.00 sec)# 修改sex列的位置
mysql> alter table staff modify sex enum('male','female') after sname;
Query OK, 0 rows affected (0.02 sec)
Records: 0  Duplicates: 0  Warnings: 0mysql> desc staff;
+-------+-----------------------+------+-----+---------+-------+
| Field | Type                  | Null | Key | Default | Extra |
+-------+-----------------------+------+-----+---------+-------+
| id    | int(4)                | YES  |     | NULL    |       |
| sname | varchar(20)           | YES  |     | NULL    |       |
| sex   | enum('male','female') | YES  |     | NULL    |       |
| age   | int(3)                | YES  |     | NULL    |       |
| phone | bigint(11)            | YES  |     | NULL    |       |
| job   | varchar(11)           | YES  |     | NULL    |       |
+-------+-----------------------+------+-----+---------+-------+
rows in set (0.00 sec)# 创建自增id主键
mysql> alter table staff modify id int(4) primary key auto_increment;
Query OK, 4 rows affected (0.02 sec)
Records: 4  Duplicates: 0  Warnings: 0mysql> desc staff;
+-------+-----------------------+------+-----+---------+----------------+
| Field | Type                  | Null | Key | Default | Extra          |
+-------+-----------------------+------+-----+---------+----------------+
| id    | int(4)                | NO   | PRI | NULL    | auto_increment |
| sname | varchar(20)           | YES  |     | NULL    |                |
| sex   | enum('male','female') | YES  |     | NULL    |                |
| age   | int(3)                | YES  |     | NULL    |                |
| phone | bigint(11)            | YES  |     | NULL    |                |
| job   | varchar(11)           | YES  |     | NULL    |                |
+-------+-----------------------+------+-----+---------+----------------+
rows in set (0.00 sec)# 删除主键,可以看到删除一个自增主键会报错
mysql> alter table staff drop primary key;
ERROR 1075 (42000): Incorrect table definition; there can be only one auto column and it must be defined as a key# 需要先去掉主键的自增约束,然后再删除主键约束
mysql> alter table staff modify id int(11);
Query OK, 4 rows affected (0.02 sec)
Records: 4  Duplicates: 0  Warnings: 0mysql> desc staff;
+-------+-----------------------+------+-----+---------+-------+
| Field | Type                  | Null | Key | Default | Extra |
+-------+-----------------------+------+-----+---------+-------+
| id    | int(11)               | NO   | PRI | 0       |       |
| sname | varchar(20)           | YES  |     | NULL    |       |
| sex   | enum('male','female') | YES  |     | NULL    |       |
| age   | int(3)                | YES  |     | NULL    |       |
| phone | bigint(11)            | YES  |     | NULL    |       |
| job   | varchar(11)           | YES  |     | NULL    |       |
+-------+-----------------------+------+-----+---------+-------+
rows in set (0.01 sec)mysql> alter table staff drop primary key;
Query OK, 4 rows affected (0.06 sec)
Records: 4  Duplicates: 0  Warnings: 0# 添加联合主键
mysql> alter table staff add primary key (sname,age);
Query OK, 0 rows affected (0.02 sec)
Records: 0  Duplicates: 0  Warnings: 0# 删除主键
mysql> alter table staff drop primary key;
Query OK, 4 rows affected (0.02 sec)
Records: 4  Duplicates: 0  Warnings: 0# 创建主键id
mysql> alter table staff add primary key (id);
Query OK, 0 rows affected (0.02 sec)
Records: 0  Duplicates: 0  Warnings: 0mysql> desc staff;
+-------+-----------------------+------+-----+---------+-------+
| Field | Type                  | Null | Key | Default | Extra |
+-------+-----------------------+------+-----+---------+-------+
| id    | int(11)               | NO   | PRI | 0       |       |
| sname | varchar(20)           | NO   |     |         |       |
| sex   | enum('male','female') | YES  |     | NULL    |       |
| age   | int(3)                | NO   |     | 0       |       |
| phone | bigint(11)            | YES  |     | NULL    |       |
| job   | varchar(11)           | YES  |     | NULL    |       |
+-------+-----------------------+------+-----+---------+-------+
rows in set (0.00 sec)# 为主键添加自增属性
mysql> alter table staff modify id int(4) auto_increment;
Query OK, 4 rows affected (0.02 sec)
Records: 4  Duplicates: 0  Warnings: 0mysql> desc staff;
+-------+-----------------------+------+-----+---------+----------------+
| Field | Type                  | Null | Key | Default | Extra          |
+-------+-----------------------+------+-----+---------+----------------+
| id    | int(4)                | NO   | PRI | NULL    | auto_increment |
| sname | varchar(20)           | NO   |     |         |                |
| sex   | enum('male','female') | YES  |     | NULL    |                |
| age   | int(3)                | NO   |     | 0       |                |
| phone | bigint(11)            | YES  |     | NULL    |                |
| job   | varchar(11)           | YES  |     | NULL    |                |
+-------+-----------------------+------+-----+---------+----------------+
rows in set (0.00 sec)

操作示例

 多表关系创建----foreign  key约束:

  多表关系分析:

分析步骤:
#1、先站在左表的角度去找
是否左表的多条记录可以对应右表的一条记录,如果是,则证明左表的一个字段foreign key 右表一个字段(通常是id)#2、再站在右表的角度去找
是否右表的多条记录可以对应左表的一条记录,如果是,则证明右表的一个字段foreign key 左表一个字段(通常是id)#3、总结:
#多对一:
如果只有步骤1成立,则是左表多对一右表
如果只有步骤2成立,则是右表多对一左表#多对多
如果步骤1和2同时成立,则证明这两张表时一个双向的多对一,即多对多,需要定义一个这两张表的关系表来专门存放二者的关系#一对一:
如果1和2都不成立,而是左表的一条记录唯一对应右表的一条记录,反之亦然。这种情况很简单,就是在左表foreign key右表的基础上,将左表的外键字段设置成unique即可

多表关系分析

  多对一或一对多:

 #一对多或称为多对一

三张表:出版社,作者信息,书一对多(或多对一):一个出版社可以出版多本书关联方式:foreign key

=====================多对一=====================
create table press(
id int primary key auto_increment,
name varchar(20)
);create table book(
id int primary key auto_increment,
name varchar(20),
press_id int not null,
foreign key(press_id) references press(id)
on delete cascade
on update cascade
);insert into press(name) values
('北京工业地雷出版社'),
('人民音乐不好听出版社'),
('知识产权没有用出版社')
;insert into book(name,press_id) values
('九阳神功',1),
('九阴真经',2),
('九阴白骨爪',2),
('独孤九剑',3),
('降龙十巴掌',2),
('葵花宝典',3)
;

示例:一对多或多对一

多对一:其它关系实例

  多对多:

#多对多
三张表:出版社,作者信息,书多对多:一个作者可以写多本书,一本书也可以有多个作者,双向的一对多,即多对多关联方式:foreign key+一张新的表 

=====================多对多=====================
create table author(
id int primary key auto_increment,
name varchar(20)
);#这张表就存放作者表与书表的关系,即查询二者的关系查这表就可以了
create table author2book(
id int not null unique auto_increment,
author_id int not null,
book_id int not null,
constraint fk_author foreign key(author_id) references author(id)
on delete cascade
on update cascade,
constraint fk_book foreign key(book_id) references book(id)
on delete cascade
on update cascade,
primary key(author_id,book_id)
);#插入四个作者,id依次排开
insert into author(name) values('egon'),('alex'),('yuanhao'),('wpq');#每个作者与自己的代表作如下
egon:
九阳神功
九阴真经
九阴白骨爪
独孤九剑
降龙十巴掌
葵花宝典
alex:
九阳神功
葵花宝典
yuanhao:
独孤九剑
降龙十巴掌
葵花宝典
wpq:
九阳神功insert into author2book(author_id,book_id) values
(1,1),
(1,2),
(1,3),
(1,4),
(1,5),
(1,6),
(2,1),
(2,6),
(3,4),
(3,5),
(3,6),
(4,1)
;

实例:多对多

服务和机器
一个服务可能被部署到多台机器上,一台机器上也可以部署多个服务学生和课程
一个学生可以选择多门课程,一门课程也可以被多个学生选择

多对多:其它关系实例  

  一对一:

#一对一
两张表:学生表和客户表一对一:一个学生是一个客户关联方式:foreign key+unique

create table customer(-> id int primary key auto_increment,-> name varchar(20) not null,-> qq varchar(10) not null,-> phone char(16) not null-> );create table student(-> id int primary key auto_increment,-> class_name varchar(20) not null,-> customer_id int unique, #该字段一定要是唯一的-> foreign key(customer_id) references customer(id) #外键的字段一定要保证unique-> on delete cascade-> on update cascade-> );#增加客户
mysql> insert into customer(name,qq,phone) values-> ('韩蕾','31811231',13811341220),-> ('杨澜','123123123',15213146809),-> ('翁惠天','283818181',1867141331),-> ('杨宗河','283818181',1851143312),-> ('袁承明','888818181',1861243314),-> ('袁清','112312312',18811431230)mysql> #增加学生
mysql> insert into student(class_name,customer_id) values-> ('脱产1班',3),-> ('周末1期',4),-> ('周末1期',5)-> ;

实例:一对一

例一:一个用户只有一个博客用户表:id  nameegonalexwupeiqi博客表   fk+uniqueid url name_idxxxx   1yyyy   3zzz    2例二:一个管理员唯一对应一个用户用户表:id user  passwordegon    xxxxalex    yyyy管理员表:fk+uniqueid user_id password1      xxxxx2      yyyyy

一对一:其它关系实例

转载于:https://www.cnblogs.com/open-yang/p/11411796.html

MySQL用户、库、表(单/多)操作相关推荐

  1. MySQL之库表设计篇:一到五范式、BC范式与反范式详解

    引言 MySQL的库表设计,在很多时候我们都是率性而为,往往在前期的设计中考虑并不全面,同时对于库表结构的划分也并不明确,所以很多时候在开发过程中,代码敲着敲着会去重构某张表结构,甚至大面积重构多张表 ...

  2. Java入力项目无法设定到form_html中关于form与表单提交操作的资料集合

    原标题:html中关于form与表单提交操作的资料集合 这里我们介绍一下form元素与表单提交方面的知识. form元素 form元素的DOM接口是HTMLFormElement,继承自HTMLEle ...

  3. mysql 列换行 表设计 设计_今天来讲一下怎么利用MySql进行库表设计

    今天来讲一下怎么利用MySql进行库表设计 1 引擎选择 在 mysql 5.1 中,引入了新的插件式存储引擎体系结构,允许将存储引擎加载到正在运新的 mysql 服务器中.使用 mysql 插件式存 ...

  4. 防止用户将表单重复提交的方法汇总

    表单重复提交是在多用户Web应用中最常见.带来很多麻烦的一个问题.有很多的应用场景都会遇到重复提交问题,比如: 点击提交按钮两次. 点击刷新按钮. 使用浏览器后退按钮重复之前的操作,导致重复提交表单. ...

  5. uniapp开发微信小程序-7.用户填写表单信息

    uniapp开发微信小程序-7.用户填写表单信息 本章让用户通过表单更新自己的个人信息,也是本系列文章最后一篇入门分享,之后碰到新颖.常用的功能会继续分享给大家,如果大家有任何问题欢迎留言,我会尽快线 ...

  6. selenium的常见表单元素操作

    selenium的表单相关操作 selenium是浏览器自动化测试框架,是一个用于Web应用程序测试的工具,可以直接运行在浏览器当中,并可以驱动浏览器执行指定的动作,如点击.下拉.填充数据.删除coo ...

  7. 关于表单的java的程序_JAVA BOT程序模拟人类用户填写表单 并 发送

    仿真表单:BOT程序模拟人类用户填写表单 并 发送 抓取表单: HTTPSocket http = new HTTPSocket(); SocketFactory.setProxyHost(" ...

  8. Spring Security默认的用户登录表单 页面源代码

    Spring Security默认的用户登录表单 页面源代码 <html><head><title>Login Page</title></hea ...

  9. PHP自学3——在html的table标签中显示用户提交表单

    为了更好地显示用户提交表单,本节将在上一节的基础上将读取的用户表单显示在html的<table>标签中,这一节将用到和数组有关的知识. 本节代码将从外部文件(.txt文件)中读取信息于指定 ...

  10. Vue编写添加用户的表单 ~ 不要错过哦

    在写项目的过程中遇到了添加用户信息的功能,今天写出来和大家分享. 希望可以帮助到有需要的小伙伴 文章目录 使用element-ui实现布局和样式 javascript中的数据.数据的校验规则 以及 方 ...

最新文章

  1. 只用jsp实现同样的Servlet功能
  2. [云炬创业基础笔记]第七章创业资源测试4
  3. mybatis plus使用雪花算法_MyBatis-Plus进阶
  4. C/C++ atoi函数 - C语言零基础入门教程
  5. java 接口案例
  6. POJ 2429 GCD LCM Inverse
  7. UML类图几种关系的总结,泛化 = 实现 组合 聚合 关联 依赖
  8. jQuery - 按回车键触发跳转
  9. 每日算法系列【LeetCode 556】下一个更大元素 III
  10. oracle for redhat 5.4 x64安装
  11. Objective-C中内存管理的一些特例
  12. 【学习笔记】《光纤传感器振动系统信号解调技术研究--华北电力--控制工程--张**》重点笔记
  13. 五笔字根表口诀的通俗易懂讲解
  14. 电脑或者手机的PIN码
  15. PDF to EPUB Converter Mac(PDF转EPUB转换器)
  16. Android 切割图片
  17. ubuntu20.04安装小鹤双拼输入法挂接音形改简体中文Rime
  18. windows server服务器查看操作记录
  19. Python光的干涉仿真
  20. 2013年中南大学复试-惠民工程

热门文章

  1. filter:grayscale(1) -- 使网页呈现哀悼模式
  2. OpenStack Pike 版本的 Mistral 安装
  3. php点击事件唤起微信聊天,vbot微信机器人微信聊天消息详解(15):点击消息
  4. 为什么wimax技术消失了?
  5. 利用Foxit Reader的PDF Printer实现提权
  6. 「电子行业」慧都大数据赋能某知名集成电路制造企业「质效双升」
  7. Linux : 捕获Kill的信号
  8. 子网划分例题-等长划分、不等长划分
  9. 推荐老狼的新歌《情人劫》
  10. 怎么提高学习或者工作效率