Database mysql basic CRUD

Character set and storage engine

  • Modify the character set
    In order to display Chinese normally, the character set of the database must be set to utf8.
 show variables like 'character%';#查看字符集
修改mysql的配置文件
cd /etc/mysql/mysql.conf.d
sudo cp mysql.cnf mysql.cnf.bak
sudo vim mysql.cnf
在[mysqld]下增加语句:
character_set_server = utf8
保存并重启服务
sudo systemctl restart mysql.service #重启服务
  • Database engine
    commonly used database engine: myisam, innodb, Archive, the NDB, Memory
    myisam and innodb difference
    myisam query speed, does not support transactions do not support foreign keys, to support table lock
    innodb additions and deletions quick change efficiency, support services, support for foreign keys , Support table lock

SQL

Basically can be divided into:
data definition language DDL (create, drop)
data operation language DML (insert, delete, update)
data query language DQL (select, where, group by, order by, limit)
data control language DCL (grant, revoke )
Transaction processing language TPL (commit, rollback)

mysql basic commands

Command to connect to mysql database

mysql -h服务器地址 -u用户名 -p

# 1. View the library
show databases;
# 2. Create a library
create database database name default charset = utf8; # Database name should not be pure numbers, no Chinese characters
# 3. Delete library
drop database database name;
# 4. Select the library
use database name;
# 5. View the table
show tables;
# 6. View the database creation statement
show create database database name
# 7. View the selected database
select database ()
# 8 Modify the database character set
alter database student default charset = utf8;

C in mysql

create table [if not exists] 表名(
 列名1 类型 [限制],
 列名2 类型 [限制],
 ...
 列名n 类型 [限制] #最后一列没有逗号
) [engine=myisam | innodb][ default charset=utf8];

The primary key does not allow duplicate values, and it is not allowed to be empty.
auto_increment self-increment, only works for int type primary keys.
primary key does not allow null values, unique
not null non-empty
unique unique
default default, default value

写法一:insert into 表名(字段1,字段2...) values(1,2...);
省略了字段列表,则按照建表时的字段顺序进行插入,每一列都要给值
写法二:insert into 表名 values(1,2...);
写法三:插入多个记录
 insert into 表名(字段1,字段2...)
 values(1,2...),
 (1,2...),
 (1,2...)....

R in mysql

View table-building statement

show create table 表名;

View table structure

desc 表名;

data query

  • Basic query
select username,password from user;
select usernname as ⽤户名, password as 密码 from user; #可以给字段起
别名
select * from user; #查询所有字段,慎用,一般不建议使用,会导致无法优化
sql语句
select 2018,username,password from user; #可以有常量,表达式
select sname,2018-year(sbirthday) from student; #year是mysql的内置函数
select distinct username from user; #去除重复记录 distinct 针对查询结果去除重复记录,不针对字段
  • Conditional query
    Relational operators:>,> =, <, <=, =,! =, <>, Between and
select username,password from user where uid <10
select username,password from user where uid != 10
select username,password from user where uid between 10 and 20

Logical operators: and, or, not

select username,password from user where uid < 100 and uid > 20;
select username,password from user where uid > 100 or uid < 20;

Set operators: in, not in

select username,password form user where uid in (2,3,4)
select username,password form user where uid not in (2,3,4)

Judgment operation: is null, is not null

select username,password from user where username is null

Wildcard _ represents a character,% represents a string of any length

select * from user where username like '王_';
select * from user where username like '王%';
  • Sort
select * from user order by age asc;
select * from user order by age desc;
多字段排序
 select name,age from php_user_history order by age desc,name;
#如果在第一列上有相同的值,在具有相同的age的记录上再按name升序排列
  • Limit result set (limit)
    limit n # Take the first n records
    limit offset, n # Take the offset from the beginning, take n
select * from php_user_history limit 3;
select * from php_user_history limit 4,2;
注意结果集中记录从0开始数数,offset相对于0开始
实现分页必须的技术点
limit (page-1)*num,num
  • Aggregate function
    • count the number of records in the statistical result set
    • max
    • min minimum
    • Avg average value, only for numeric type statistics
    • sum Sum, only for numeric type statistics
    • Note that aggregate functions cannot be used directly in the condition behind where, but can be used in subqueries
select count(*) num from user;
select count(distinct age) num from user; //去除重复记录
select * from student where sno = max(sno);//错误
  • Grouping
    • The query statement of groub by appears, the fields after select can only be the fields behind the aggregate function and group by
      , do not follow other fields
    • To filter packets, you can use having
select uid, count(*) num from php_forum group by uid;
select uid,title, count(*) num from forum group by uid having
count(*) >=2;
havingwhere的区别:
 where针对原始表进行过滤
 having 是针对分组进行过滤

select field from table name [where condition] [group by] [having] [order by] [limit]

U in mysql

  • Modify field type
 alter table 表名 modify 字段名 类型 [限制] #增加字段
 alter table 表名 add [column] 字段名 类型 [限制];
  • Delete field
 alter table 表名 drop [column] 字段名;
  • Modify the field name and type
alter table 表名 change [column] 旧字段名 新字段名 类型 [限制];
  • Modify the table name
alter table 表名 rename 新表名
 alter table 表名 [engine=myisam] [default charset=utf8];
  • The insertion position can be specified by first and after
 alter table student add sno varchar(3) not null after sid; //在sid列后插入
 alter table student add sid int primary key auto_increment first;//在第一列插入

 update 表名 set 字段1=1,字段2=2... where 条件 #不加where修改的是所有的记录

D in mysql

Delete the data in the table, the value of the self-incrementing primary key will not restart

delete from 表名 where 条件;#如果不加条件,会删除表中所有数据,慎重使用

Empty the table, and increment the value of the primary key to restart
truncate

truncate table 表名,清空表中所有记录,等价于delete from 表名;

The difference between delete and truncate. After truncate, the incremented primary key value in the table starts from 1.

Published 44 original articles · liked 0 · visits 1226

Guess you like

Origin blog.csdn.net/weixin520520/article/details/104516850