MySQL Learning Record 2
date type
create table student(
id int,
name char(6),
born_year year,
birth_date date,
class_time time,
reg_time datetime
);
insert into student values
(1,'egon',now(),now(),now(),now());
*If there is code that was previously typed incorrectly and cannot be deleted, you can use /c to end the current statement. (terminate operation)
The difference between datatime and timestamp
character type
Usage and difference between char and varchar
One is variable length and one is fixed length.
Char is simple and crude, and has fast access speed, but wastes space; varchar is more space-saving, accurate in access, but slow in access. Nowadays most use char type. It's best not to mix them.
Query: select xxx from xxx where xxx = 'x'; #The spaces after the data can be automatically ignored when used.
select xxx from xxx where xxx like = 'x'; #When used, the number of spaces, etc. must be the same
Enumeration types and collection types
It should be noted that if an option that is not included in the list type is selected during insert, it will not be displayed in the table.
Restrictions
null/key/default/extra is the constraint.
null: Whether to allow null values to be passed.
default: the value reported when an error occurs (default value can be set)
**unique key:** You can set certain data to not be repeated:
Single column unique:
Method 1: Add unique directly after the field
create table department(
id int,
name char(10) unique
);
Method 2: Add unique (name) at the end,
Union unique (MUL): unique(name, name),
primary key : not empty and unique not null unique
Storage engine (inodb (default)): For the innodb storage engine, a table must have a primary key.
Create a primary key:
#Single-column primary key
create table t1(
id int primary key,
name char(16),
);
#Compound primary key
primary(name,name),
auto_increment : self-increment
create table t20(
id int primary auto_increment,#表明id可以自己从1往上增加。
name char(16)
);
#If another id is set at the insert place, the next unset id will be added starting from this one.
#learn
show variables like ’auto_inc%‘;
#auto_increment_increment步长:默认为1
auto_increment_offset起始偏移量:默认为1`
#Set the step size (the same goes for the starting offset):
set session auto_increment_increment=5;#局部
set global auto_increment_increment=5;#全局,得退出重进
Emphasis: starting offset ≤ step size.
foreign key: establish relationships between tables
1. Establish table relationships:
eg.#先建被关联的表,并且保证被关联的字段唯一。
create table dep(
id int primary key,
name char(16),
comment char(50)
);
#再建关联的表
create table emp(
id int primary key,
name char(10),
sex enum('male','female'),
dep_id int,
foreign key (dep_id)references dep(id)
on delete cascade on update cascade #这里可实现自动更新自动删除
);
查看表结构:desc dep;(复习一下)
2. Insert data:
#First insert records into the related table
insert into dep values
(1,"IT","123"),
(2,"销售","456"),
(3,"财务","789");
#Insert records into the related table
insert into emp values
(1,'dumpling','male',1);
#Delete unwanted information:
1. Delete the related table first, then delete the related table.
delete from emp where dep_id=1;
delete from dep where id = 1;
2. Add when establishing the association table: on delete cascade on update cascade
You can directly delete the contents of the related table:
delete from dep where id = 1;
update dep set id = 202 where id = 2;
dep where id = 1;
2、建立关联表时加入:on delete cascade on update cascade
可直接删除被关联表中内容:
```mysql
delete from dep where id = 1;
update dep set id = 202 where id = 2;
Try not to use it like this, as it may cause problems when expanding.