Operating tables added:
Storage Engine:
innodb: mysql default engine
It can support transactions, foreign keys, and row lock
blackhole: black hole, it flew away into the data
The loss of cache, power on the effective breakpoint: memory
myisam: 5.5 version previously used the default engine
Field Type:
Integer:
tinyint: range -128, 127
int: range -2147483648, 2147483647
bigint: Range: great
width:
Refers to the width of the display, insert the data if the width is less than width definition, will complement a space
Float:
float: 255 range (255 total), 30 (number of decimal places)
double: 255 range (255 total), 30 (number of decimal places)
decimal: the range of 65 (total 65), 30 (number of decimal places)
The biggest problem is the accuracy of the difference between the three, but we use float
Date Type:
date: Date
datetime: year, month, day, hour
time: minutes and seconds
year: Year
timestamp: When timestamp 5464842416 ... insert data or create, it will automatically update the time
Enumeration and collection
enum () which is a string
set () which is a string
Restrictions:
not null: the constraint can not be empty
unique: the constraint value must be unique
primary key: 主键 not null +unique
Interior creates an index, which is used to quickly find a particular record
Index: improve query efficiency
default: The default value of the constraint when the data is inserted
auto_increment: # default increment from 0
zerofill: fill all spaces with 0
unsigned: Unsigned means that there is no positive or negative sign
The relationship between the table and the table
Foreign key: to establish a relationship between two tables
Many
Many to many
One to One
Modify the operating table
Operation copies table
The relationship between the table and the table:
a table:
Department staff table fields: id name gender dep_name dep_desc
All data will be stored in a table of drawbacks:
1. The structure is not clear: fatal
2. waste of space: fatal
3. Scalability poor: fatal drawbacks
To solve the problem:
Two tables to build strong relationships, foreign key
How to determine the relationship between the table and the table?
Employees and departments: Many
Department staff: Not many to
Employees form to: Many departments table
Summary: Any way many-table relationships, referred to many foreign key relationships
Foreign key syntax:
Table foreign key (current relationship table foreign key field) references the associated (id)
When you create two tables:
Must be the establishment of an association table, then association table
# 被关联表:
# dep:
create table dep(
id int primary key auto_increment,
dep_name varchar(16),
dep_desc varchar(255)
);
# 关联表:
# emp:
create table emp(
id int primary key auto_increment,
name varchar(16),
age int,
gender enum('male', 'female', 'others') default 'male',
dep_id int not null,
foreign key(dep_id) references dep(id)
);
When insert data:
Must be inserted in the data relation table, then insert the association table data
# dep
insert into dep(dep_name, dep_desc)values ('计划生育部','管理全国各地的生育情况'),('税收统筹部','管理全国各地的税收情况'),('国家供电局','管理全国各地的供电所');
+----+-----------------+-----------------------------------+
| id | dep_name | dep_desc |
+----+-----------------+-----------------------------------+
| 1 | 计划生育部 | 管理全国各地的生育情况 |
| 2 | 税收统筹部 | 管理全国各地的税收情况 |
| 3 | 国家供电局 | 管理全国各地的供电所 |
+----+-----------------+-----------------------------------+
# emp;
insert into emp(name, age, gender, dep_id) values('tank',17,'male',1),
('yyx',21,'male',2),
('sean',55,'female',2),
('egon',88,'female',3);
+----+------+------+--------+--------+
| id | name | age | gender | dep_id |
+----+------+------+--------+--------+
| 1 | tank | 17 | male | 1 |
| 2 | yyx | 21 | male | 2 |
| 3 | sean | 55 | female | 2 |
| 4 | egon | 88 | female | 3 |
+----+------+------+--------+--------+
When updating data:
You must first remove dep_id field is associated table in order to modify the association id field dep table
Note here:
Data relation table can be changed
However, the association table id field can not be easily modified, but the other fields may be modified
When you delete data:
delete the associated records in the table, then delete the associated records are in the table
Note here that can only be associated with a delete delete table records, truncate can not be deleted
Both records associated table can be deleted
Simultaneously with the drop drop the table, but also must first delete the associated table, you go to delete the associated table
Cascading update and cascade delete
on update cascade
on delete cascade
Foreign key on the back, not a comma.
# 被关联表:
# dep:
create table dep(
id int primary key auto_increment,
dep_name varchar(16),
dep_desc varchar(255)
);
# 关联表:
# emp:
create table emp(
id int primary key auto_increment,
name varchar(16),
age int,
gender enum('male', 'female', 'others') default 'male',
dep_id int not null,
foreign key(dep_id) references dep(id)
on update cascade
on delete cascade
);
这里要注意:
就算是设置了级联更新与级联删除,也不可以直接先drop被关联表,但是可以先删除被关联表中的记录。
mysql many to say no, the only one to many
Many to many:
Also stand to think about the position of two tables
So when many relationships, we must take advantage of the third table, the establishment of "many to many foreign key relationships" for the two tables.
# book表
create table book(
id int primary key auto_increment,
title varchar(24),
price int,
book_content varchar(255)
);
# 作者表
create table author(
id int primary key auto_increment,
name varchar(16),
age int
);
# 关联表
create table book_author(
id int primary key auto_increment,
book_id int,
author_id int,
foreign key(book_id) references book(id)
on update cascade
on delete cascade,
foreign key(author_id) references author(id)
on update cascade
on delete cascade
);
Insert data:
book
insert into book(
title, price,book_content
)values('茶花女',200,'讲述一个茶花女的一生!'),
('python从入门到精通', 2000, '学习如何一夜秃头'),
('三体', 45, '带你探究奇幻的宇宙世界!');
author
insert into author(name, age)values
('yyx',68),
('yyh',68);
book_author:
insert into book_author(
book_id, author_id) values
(1,1),
(
(2,2),
(3,1);
+----+--------------------------+-------+--------------------------------------+
| id | title | price | book_content |
+----+--------------------------+-------+--------------------------------------+
| 1 | 茶花女 | 200 | 讲述一个茶花女的一生! |
| 2 | python从入门到精通 | 2000 | 学习如何一夜秃头 |
| 3 | 三体 | 45 | 带你探究奇幻的宇宙世界! |
+----+--------------------------+-------+--------------------------------------+
+----+------+------+
| id | name | age |
+----+------+------+
| 1 | yyx | 68 |
| 2 | yyh | 68 |
+----+------+------+
+----+---------+-----------+
| id | book_id | author_id |
+----+---------+-----------+
| 1 | 1 | 1 |
| 2 | 1 | 2 |
| 3 | 2 | 2 |
| 4 | 3 | 1 |
+----+---------+-----------+
Update, or delete all unconstrained;
One relationship:
The relationship between the two tables is one to one relationship, and a large amount of data tables, split into two tables
user_info:
id,name,age,gender,hobby,id_card
user:
id, name, age, detial_id(外键)
detial:
id, gender, hobby, id_card
user与detial表之间建立一一对应的关系,foreign key 应该建立在使用频率比较高的一方
# 被关联表
create table customer(
id int primary key auto_increment,
name varchar(16),
media varchar(32)
);
# 关联表
# 外键必须设置为唯一
create table student(
id int primary key auto_increment,
addr varchar(255),
phone char(11),
id_card char(18),
customer_id int unique,
foreign key(customer_id) references customer(id)
on update cascade
on delete cascade
);
# 插入
insert into customer(name, media) values
('hcy', 'facebook'),
('zsb1', 'ig'),
('zsb2', 'vk'),
('hb', '探探');
+----+------+----------+
| id | name | media |
+----+------+----------+
| 1 | hcy | facebook |
| 2 | zsb1 | ig |
| 3 | zsb2 | vk |
| 4 | hb | 探探 |
+----+------+----------+
insert into student(addr, phone, id_card, customer_id) values
('上海', '15214546711', '440888888888888888', 1),
('北京', '18888888888', '440777777777777777', 2);
+----+--------+-------------+--------------------+-------------+
| id | addr | phone | id_card | customer_id |
+----+--------+-------------+--------------------+-------------+
| 1 | 上海 | 15214546711 | 440888888888888888 | 1 |
| 2 | 北京 | 18888888888 | 440777777777777777 | 2 |
+----+--------+-------------+--------------------+-------------+
修改表的操作
- 语法: 注意: mysql 关键字不区分大小写
1. 修改表名
ALTER TABLE 表名
RENAME 新表名;
2. 增加字段
ALTER TABLE 表名
ADD 字段名 数据类型 [完整性约束条件…],
ADD 字段名 数据类型 [完整性约束条件…]; # 添加到最后一列
ALTER TABLE 表名
ADD 字段名 数据类型 [完整性约束条件…] FIRST; # 添加到第一列
ALTER TABLE 表名
ADD 字段名 数据类型 [完整性约束条件…] AFTER 字段名; # 添加到某一列之后
3. 删除字段
ALTER TABLE 表名
DROP 字段名;
4. 修改字段
ALTER TABLE 表名
MODIFY 字段名 数据类型 [完整性约束条件…]; # 修改数据类型
ALTER TABLE 表名
CHANGE 旧字段名 新字段名 旧数据类型 [完整性约束条件…]; # 修改字段名,保留字段类型
ALTER TABLE 表名
CHANGE 旧字段名 新字段名 新数据类型 [完整性约束条件…]; # 修改字段名与字段类型
- 复制表的操作:
复制表结构+记录 (key不会复制: 主键、外键和索引)
mysql> create table new_service select * from service;
只复制表结构
# 将select * from service where 1=2; ---> 不要真实数据,需要表结构
mysql> create table new_customer select * from customer where 1=2;