table of Contents
First, the operation of the library
(A) by create
create database 数据库名称 charset utf8;
# 命名规范
# 1. 可以有字母、数字、下划线、@、#、$
# 2. 区分大小写
# 3. 唯一性
# 4. 不能使用关键字
# 5. 不能单独使用数字
# 6. 最长128位
(B) delete drop
drop database 数据库名称;
(Iii) changes alter charset
# 删除在增加
# 如果数据库中有数据的话,直接drop会导致数据库的数据丢失
# 线上环境,不能直接删除数据,在删除之前,需要进行备份
alter database 数据库名称 charset utf8 # 修改库的编码
(D) the investigation show
show databases; #查看当前所有库
show create database 数据库名; # 展示单个库
select database(); # 查看当前所在库
(5) the database use
use 数据库名; # 切换到库目录
Second, the operating table
(A) integrity constraint
- auto_increment: Constraints field is the automatic growth 1
- primary key: a unique identifier that is bound, column values can not be repeated, is equivalent to the primary key index, speed up queries
- unsigned: Default Type range of signed numbers (with negative numbers), unsigned its constraint (not negative)
- not null: identify the field can not be empty
- default: specify default values, when the insert data, if not set, the default value is automatically added
- zerofill: filled with 0s
- foreign key: foreign key, you use the multi-table creation
- unique: a unique identifier for multi-table created in one
# 例子1:
create table t2 (
id int auto_increment primary key,
name char(10)
)charset=utf8;
# 例子2:
create table t3 (
id int unsigned auto_increment primary key,
name char(10) not null default 'xxxx',
age int not null default 0
)charset-utf8;
(Ii) Data Types
Digital Type
Integer type
tinyint, smallint, mediumint, int, bigint is an integer type, commonly used int.
Float
float[m,d]
: M represents a number of the total number, the maximum value of 255, d is the number of decimal places, maximum 30, with the increase of the decimal precision becomes inaccurate,double[m,d]
: M represents a number of the total number, the maximum value of 255, d is the number of decimal places, the maximum value of 30, the accuracy is higher than the float, but it is not accuratedecimal[m,d]
: M represents the total number of digits, the maximum value of 65, d is the number of decimal places, maximum 30, with the increase of the decimal precision always accurate
# 例子1 : create table t5( id int auto_increment primary key, salary decimal(16,10), num float )charset=utf8; # 例子2(刚好10位) : insert into t5 (salary, num) values(500023.2312345678, 5000.2374837284783274832); # 例子3 (少于10位) insert into t5(salary,num) values (500023.231234567, 5000.2374837284783274832); # 例子4 (多于10位) insert into t5 (salary,num) values (500023.23123456789, 5000.2374837284783274832);
String
char (length): length, in the range of (0-255)
No matter how many characters are inserted, forever fixed duty prescribed length (will be filled with spaces), trailing spaces will be deleted when retrieving
Scenario: ID card, phone number, MD5 encrypted value
varchar (length): variable length, range (0-65535)
Is calculated according to the number of bytes occupied by the inserted length of the string will increase to hold a prefix byte size of the string
Note: If you can not determine the insert size of the data is generally recommended to use varchar (255)
create table t6( id int unsigned auto_increment primary key, name char(10) not null default 'xxx', )charset=utf8; insert into t6 (name) values ('hello') # 只能输入10个字节以内 insert into t6(name) values ('sadfasdfsafafdsadfdasf') # 报错,超出范围
The date and time type
- year
- date
- time
- datetime
- timestamp
General use datetime
create table t8( d date, t time, dt datetime ); select * from t8;
Enumeration and collection
- enum: radio, only a selected value within a given range, such as gender
- set: multiple choice, you can select one or more values within a given range (hobby)
create table t9( id int auto_increment primary key, gender enum('male','female') )charset=utf8; insert into t9 (gender) values('male'); inser into t9 (gender) values ('female')l insert into t9(gender) values ('sdfasf0') # 报错
(C) increase
(1) increasing the table create
# []内不是必填内容
create table 表名(
字段名 列类型 [约束条件], # 记住加逗号
····
字段名 列类型 [约束条件] #最后一行不加逗号
)charset=utf8; # 加分号
# 例子
#创建编码为utf8,字段为id、name的表t1
create table t1(
id int,
name char(5)
)charset=utf8;
# 增加数据
insert into t1 (id,name) values (1,'wick');
# 查询数据
select * from t1; # *代表查询所有的列
(2) increase the field alter add
The last one is added by default
alter table 表名 add 字段名 列类型 [约束条件], add 字段名 列类型 [约束条件]; # 默认添加在最后一列之后 alter table t88 add name varchar(32) not null default '';
Add in the first column (first)
alter table 表名 add 字段名 列类型 [约束条件] first; # 添加在首行 alter table t88 add name3 varchar(32) not null default '' first;
Add in the specified location (after)
alter table 表名 add 字段名 列类型 [约束条件] after 字段名; # 添加在首行 alter table t88 add name3 varchar(32) not null default '' after d;
(Iv) change
(1) modify the table name alter rename
alter table 旧表名 rename 新表名;
# 指令
alter table t8 rename t88;
(2) modify the field
1. modify
# 只可以修改字段的类型和约束条件
alter table 表名 modify 字段名 类型 [约束条件]
# 例子
alter table t88 modify name2 char(20);
2. change
# 可以修改字段、类型、约束条件
alter table 表名 change 旧字段名 新字段名 新数据类型 [约束条件]
# 例子
alter table t88 change name2 name22 varchar(32) not null default '';
alter table t88 change name22 name23;
(E) delete
Disable online
(1) Delete table drop
drop table 表名
# 例子:
drop table t9;
(2) remove fields alter drop
alter table 表名 drop 字段名;
# 例子
alter table t88 drop name4;
(Vi) investigation show
show tables;
: View all the current library table
show create table 表名;
: View the table creation statements
(Vii) to copy the table structure create like
View the table creation statement, copy, run
show create table t88;
like to achieve
create table 新表名 like 旧表名;
create table t89 like t88;
Third, the operation record table
(A) by insert into
insert into 表名(列1,列2) values(值1,值2);
# 例子
insert into t1(id,name) values(1,'nick');
insert into t1(id,name) values(1,'tank'),(3,'zekai');
(B) check select from
(1) where conditions
- Comparison operators:> <> = <= <> =!
- between 80 and 100: 80 and 100 between, closed interval, comprising 80 and 100
- in (23,45,23): the value is 23 or 45 or 23
- like'wick% ': at the beginning of the wick,% represents any number of characters, a character logo _
- Logical operators: and or not
select 列1 , 列2 from 表名;
select * from 表名; # 代表所有的列
# 例子
select * from t66 where id>30 and id<40;
select * from t66 where id between 31 and 33;
select * from t66 where id in (23,34,11);
select * from t66 where name like 'x%'; # x开头的
select * from t66 where name like '%x'; # x结尾的
select * from t66 where name like '%x%'; # 包含x的,不要用
# distinct避免重复
select distinct name from t66;
# 四则运算,不要用
select name, age*10 from t3;
(C) delete delete, truncate
delete from 表名; #删除表中所有数据
delete from 表名 where 条件;
truncate 表名; # 没有where文件的,
# 例子
delete from t5 where id=1;
delete from t5 where id>=1;
delete from t5 where id>=1 and id<10;
truncate and delete differences:
- After deleting delete, insert data from a primary key plus 1., starting from 1 TRUNCATE
- delete to delete the equivalent line by line, truncate equivalent to select all to delete
(Iv) change update set
update 表名 set 列名1= 新值1,列名2=新值2 where 条件;
# 例子
uodate t66 set name = 'xxxx' where id = 30;
update t66 set name='xxxx' where id>20 or name='zekai';
supplement
- sql command must be added
;
tee D:/mylog.log
: Automatically to the next all the sql statements and results into file