mysql command statement Overview

MySQL command

Mysql login account

dos command window:

-- mysql(或者是mysqlsh) -u账号名 -p密码
mysql -uroot -p123

use

-- 使用数据库
use db_name;

select

-- 查看当前使用的数据库
select database();

-- 查看当前数据库版本
select version();

show

-- 查看数据库目录
show databases;

-- 查看数据库中的表目录
show tables;

-- 查看其他库中的表
show tables from db_name;

-- 查看表的创建语句
show create table tb_name;

desc

-- 查看表结构
desc tb_name;

explain

-- 查看语句的执行状态(执行计划)
explain select ename,sal from emp where sal = 5000;

create

-- 创建一个数据库
create database db_name;

drop

-- 删除一个数据库
drop database db_name;

source

-- 加载 sql 文件,拖拽文件到dos窗口会自动生成路径
-- \source
source D:\AllCode\SQLCode\test.sql

limit

Description: paging query limit <startIndex>, <lenght>

startInsdex indicates a start position, from zero.

It represents taking some lenght.

A fixed formula: Showing pageSize records:

limit (pageNo - 1) * pageSize, pageSize

# 例子,显示前五页的数据
-- 从 5 开始,取 5 个,将显示第 5~10 条的数据。
select ename,sal from emp order by sal desc limit 5,5;

Other commands

-- 终止一条语句
\c 或者按ctrl+c键

-- 退出 mysql
\q 或者 quit 或者 exit

DDL statements Data Definition Language

create

Description: Create a table

-- 建表语句的语法格式:
create table 表名(
    字段名1 数据类型,
    字段名1 数据类型,
    字段名1 数据类型,
    ......
);

-- 将查询结果做一张表:create table 表名 as select语句;
create table t_test 
as
select * from emp;

constraint (constraint)

constraint constraint Introduction
not null Non-empty constraint Constraints can not be null
unique The only constraint Constraint field can not be repeated, but may be null
primary key The primary key constraint Neither bound field is null, can not be repeated, a table can only save one
foreign key Foreign key constraint Foreign key value can be null, the referenced field is not necessarily the primary key, but at least having unique constraints
-- 列级约束
-- not null 只有列级约束
drop table if exists t_class;
create table t_class(
    cid int primary key, -- 主键约束
    cname varchar(255) unique, -- 唯一约束
    cpassword varchar(255) not null, -- 非空约束
)
-- 表级约束
drop table if exists t_student;
create table t_student(
    sid int auto_increment, -- 自增
    classid int,
    sname varchar(255),
    scode varchar(255),
    spassword varchar(255),
    -- 联合约束,两者其一俱备唯一性即可
    unique(sname,scode),
    -- 复合主键
    primary key(sid,spassword)
    -- 外键引用
    foreign key(classid) references t_class(cid)
)

drop

-- 删除一张表
drop table 表名;
-- 如果表存在则删除,mysql特有语句
drop table if exists 表名;

alter

Data Manipulation Language DML statements

insert

Introduction: add data to the table.

-- 添加一行记录
insert into 
    表名(字段名1, 字段名2, ...)
values (数据1, 数据2, ...);

-- 添加一行记录,省略字段
insert into 表名
values (数据1, 数据2, ...);

-- 添加两行记录
insert into 
    表名(字段名1, 字段名2, ...)
values 
    (数据1, 数据2, ...),
    (数据1, 数据2, ...);
    
-- 将查询结果插入到一张表中:insert into 表名 select语句;
insert into t_test select * from emp;

update

Modify data in the table

-- 语法格式:update 表名 set 字段名1=值1,字段名2=值2...where条件;
update 
    emp 
set 
    ename = 'black', sal = 1234
where 
    deptno = 10;

delete

Delete data in the table

-- 语法格式:delete from 表名 where条件
delete from emp where deptno = 10; 

truncate

Description: permanently cut (delete) data in the table can not be rolled back.

truncate table emp;

index (index)

Introduction: primary key field and having a unique constraint indexes are automatically added.

# create index 索引名称 on 表名(字段名);
# drop index 索引名称 on 表名;
-- 给sal字段添加索引
create index emp_sal_index on emp(sal);
-- 删除索引
drop index emp_sal_index;

view (View)

View objects can only be created through DQL statements.

CRUD operations may be performed on a view.

A view is CRUD, it will affect the original data table.

-- 创建视图
create view myview as select empno,ename from emp;

-- 删除视图
drop view myview;

DQL statement data query language

select

select      -- execute order 5
...
from        -- execute order 1

where       -- execute order 2
...
group by    -- execute order 3
...
having      -- execute order 4
...
order by    -- execute order 6
...
limit       -- execute order 7
...

Conditions Query Operators

Operators Explanation
= equal
<> Or! = not equal to
< Less than
<= Less than or equal
> more than the
>= greater or equal to
between ... and ... Between two values
is null Empty (is not null is not empty)
and versus
or or
in( , ) Include (not in (,) is not included)
not Negated
like Fuzzy query (using the% and _ match)

order by

-- asc表示升序,desc表示降序,默认是升序
select 字段... from 表名 order by 字段 desc;

-- 多字段排序,越靠前的字段越起主导作用,优先满足前排字段的条件
# 例子,emp是员工表,ename是员工名,sal是员工工资
select ename,sal from emp order by sal desc, ename asc;

Multi-line handler

function Explanation
count() Get the number of records
sum() Summing
avg() take the average
max() Get the maximum value
(I) Get the minimum

A Note: All multi-line operation of the processing functions are performed on a set of data

Note II: multi-line processing function automatically ignore null

Note III: multi-line processing functions can not appear in the where clause

Note Four: multi-line processing function is executed after the group by statement

# emp是员工表,sal 是员工工资
select sum(sal) from emp; -- 找出工资总和
select avg(sal) from emp; -- 算出平均工资
select max(sal) from emp; -- 找出最高工资
select min(sal) from emp; -- 找出最低工资

select count(*) from emp; -- 找出总条数
select count(sal) from emp; -- 找出不为null的条数

ifnull()

# emp是员工表,comm 是员工津贴
-- ifnull(可能为null的数据,被当做什么处理)
select ename,ifnull(comm,0) from emp;

distinct

-- 去除重复记录
select distinct 字段 from 表名;
-- distinct 修饰多个字段时,所有字段联合去重
select distinct 字段1,字段2... from 表名;

Note a: distinct only appear in the forefront of all fields

join...on...

Syntax Explanation
inner join...on... The connection, the connection will only participate in Table A and Table B data query matches it. inner keyword can be omitted.
left outer join...on... An outer connector, first search the primary table, the sub-table when no match to null fill. leftModified table on the left main table, outerthe keywords can be omitted.
right outer join...on... An outer connector, first search the primary table, the sub-table when no match to null fill. rightModified table to the right of the main table, outerthe keywords can be omitted.

union

Introduction: The results of the two queries can be spliced ​​into a table, usually used to splice two unrelated tables.

Note: The number of table columns involved in splicing must be the same

-- 语法
select 
...
from 
...
union
select
...
from
...

TCL statement transaction control language

Description: Transaction (Transaction)

And statements related matters only DML statements ( insert, delete, update)

-- 开启事务
start transaction;
-- 回滚
rollback;
-- 提交
commit;
-- 设置回滚点, 回滚:rollback name;
savepoint name;

Data Control Language DCL statement

Additional knowledge

In sql, the result must be involved in computing null is null.

Sample data code table

-- 部门表
create table dept(
    deptno int primary key auto_increment, -- 部门编号
    dname varchar(14) ,   -- 部门名字
    loc varchar(13)   -- 地址
) ;
-- 员工表
create table emp(
    empno int primary key auto_increment,-- 员工编号
    ename varchar(10), -- 员工姓名                                      -
    job varchar(9), -- 岗位
    mgr int,     -- 直接领导编号
    hiredate date, -- 雇佣日期,入职日期
    sal int, -- 薪水
    comm int,  -- 提成
    deptno int not null, -- 部门编号
    foreign key (deptno) references dept(deptno)
);
insert into dept values(10,'财务部','北京');
insert into dept values(20,'研发部','上海');
insert into dept values(30,'销售部','广州');
insert into dept values(40,'行政部','深圳');
insert into emp values(7369,'刘一','职员',7902,'1980-12-17',800,null,20);
insert into emp values(7499,'陈二','推销员',7698,'1981-02-20',1600,300,30);
insert into emp values(7521,'张三','推销员',7698,'1981-02-22',1250,500,30);
insert into emp values(7566,'李四','经理',7839,'1981-04-02',2975,null,20);
insert into emp values(7654,'王五','推销员',7698,'1981-09-28',1250,1400,30);
insert into emp values(7698,'赵六','经理',7839,'1981-05-01',2850,null,30);
insert into emp values(7782,'孙七','经理',7839,'1981-06-09',2450,null,10);
insert into emp values(7788,'周八','分析师',7566,'1987-06-13',3000,null,20);
insert into emp values(7839,'吴九','总裁',null,'1981-11-17',5000,null,10);
insert into emp values(7844,'郑十','推销员',7698,'1981-09-08',1500,0,30);
insert into emp values(7876,'郭十一','职员',7788,'1987-06-13',1100,null,20);
insert into emp values(7900,'钱多多','职员',7698,'1981-12-03',950,null,30);
insert into emp values(7902,'大锦鲤','分析师',7566,'1981-12-03',3000,null,20);
insert into emp values(7934,'木有钱','职员',7782,'1983-01-23',1300,null,10);

Guess you like

Origin www.cnblogs.com/learningmakesmehappy/p/11847329.html