MySQL常用语句总结
2016-03-14 11:44
696 查看
SQL语句主要划分为以下三个类别:
DDL(Data Definition Languages):数据定义语句,这些语句定义了不同的数据段、数据库、表、列、索引等数据库对象(create、drop、alter等)
DML(Data Manipulation Languages):数据操纵语句,用于添加、删除、更新、查询数据库记录,并检查数据完整性(insert、delete、update、select等)
DCL(Data Control Languages):数据控制语句,这些语句定义了数据库、表、字段、用户的访问权限和安全级别(grant、revoke等)
下面以MySQL为例,分别介绍这三种类别语句常用的使用方法:
1. DDL:
创建数据库:create database dbname
显示当前系统中数据库:show databases
选择要操作的数据库:use dbname
查看数据库中所有数据表:show tables
删除数据库:drop database dbname
创建数据库表:create table tablename(
column_name column_type constraints(约束条件)
…
)
查看表基本定义:desc tablename
查看表详细定义:show create table tablename
删除表:drop table tablename
修改表:注:’[]’为可选项
(1)修改表字段类型:
alter table tablename modify[column] column_definition [first | after column_name]
(2)增加表字段:
alter table tablename add[column] column_definition [first | after column_name]
(3)删除表字段:
alter table tablename drop[column] col_name
(4)修改表字段名:
alter table tablename change[column] old_column_name column_defition [first | after column_name]
(5)修改表字段顺序:
add增加的新字段默认加在表的最后,change和modify默认字段位置不变,
如需修改位置,可在结尾加first(最前面)或after column_name(某一字段后)
(6)更改表名:
alter table tablename(表名) rename[to] new_tablaname(新表名)
2.DML:
插入记录:insert into tablename(col1,col2,…coln) values(v1,v2,…vn)[,values(vv1,vv2…vvn)]
更新记录:
单表更新:update tablename set col1=v1,col2=v2,…col2=vn [where condition]
多表更新:update tablename t1, tablename t2 set t1.col = v1, t2.col = v2 [where condition]
删除记录:
单表删除:delete from tablename [where condition]
多表删除:delete t1, t2,…tn from tablename t1, tablename t2…tablename tn [where condittion]
查询记录:select col1,…coln from tablename [where condition]
如查询所有字段可用*代替,即select * from tablename [where condition]
(1) 查询不重复记录
从图中可看出distinct 必须放在查询字段的开头,如果查询字段有多个,他会作用于多个字段,也就是要求所有字段都相同才能被排除,即如下图:
(2)条件查询:查询条件除=之外,还可以用>,<,<=,>=,!=等比较运算符,多个条件还可以用or,and等逻辑运算符
(3)排序和限制 select col1,…coln from tablename [where condition] [order by field [desc\asc] , field2[desc\asc]…fieldn[desc\asc]] [limit offset_start, row_count]
desc\asc是排序顺序的关键字,desc是降序,asc是升序(默认);offset_start表示记录的其实偏移量(起始为0),row_count表示显示的行数。
如果排序字段的值一样,则值相同的字段按照第二个排序字段进行排序,依次类推,如果只有一个排序字段,则这些字段相同的记录将会无序排列。
(4) 聚合:select [field1,field2,…fieldn]fun_name from tablename [where condition] [group by field1,field2,…fieldn [with rollup]] [having condition]
fun_name 表示要做的聚合操作,即聚合函数(sum,count(*),max,min等),count(1)与count(*)的区别在于:在数据记录都不为空的时候查询出来结果上没有差别的. 但当count(1)查询的那列有空的时候空的是要被去掉的不记入统计。.
group by表示要进行分类聚合的字段,with rollup 是可选语法,表明是否对分类聚合后的结果进行再汇总。
having关键字表示对分类后的结果在进行条件过滤;having和where的区别在于,having是对聚合后的结果进行条件的过滤,而where是在聚合前就对记录进行过滤。
(5) 表连接:当需要同时显示多个表中的字段时就需要用到表连接,表连接又分为内连接和外链连接,内连接是指选出两张表中互相匹配的记录,外连接则会选出其他不匹配的记录。
内连接:
外连接分为左连接和右连接:
左连接:包含所有的左边表中的记录甚至是右边表没有和它匹配的记录
右连接:包含所有的右边表中的记录甚至是左边表没有和它匹配的记录
(6)子查询:在进行查询时,需要的条件是另外一个select语句的结果,就需要用到子查询(in,not in,=,!=,exists,not exists)
(7)记录联合:将两个表的数据按照一定的查询条件查出后,将结果合并到一起显示,这时候需要union和union all
3.DCL:
创建用户:create user ‘username’@’host’ identified by ‘password’
授予权限:grant privileges on databasename.tablename to ‘username’@’host’
设置更改用户密码:set password [for ‘username’@’host’] = password(‘newpassword’]
撤销权限:revoke privileges on databasename.tablenames from’username’@’host’
删除用户:drop user ‘username’@’host’
附表:在MySQL中的操作权限
ALTER Allows use of ALTER TABLE.
ALTER ROUTINE Alters or drops stored routines.
CREATE Allows use of CREATE TABLE.
CREATE ROUTINE Creates stored routines.
CREATE TEMPORARY TABLE Allows use of CREATE TEMPORARY TABLE.
CREATE USER Allows use of CREATE USER, DROP USER, RENAME USER, and REVOKE ALL PRIVILEGES.
CREATE VIEW Allows use of CREATE VIEW.
DELETE Allows use of DELETE.
DROP Allows use of DROP TABLE.
EXECUTE Allows the user to run stored routines.
FILE Allows use of SELECT… INTO OUTFILE and LOAD DATA INFILE.
INDEX Allows use of CREATE INDEX and DROP INDEX.
INSERT Allows use of INSERT.
LOCK TABLES Allows use of LOCK TABLES on tables for which the user also has SELECT privileges.
PROCESS Allows use of SHOW FULL PROCESSLIST.
RELOAD Allows use of FLUSH.
REPLICATION Allows the user to ask where slave or master
CLIENT servers are.
REPLICATION SLAVE Needed for replication slaves.
SELECT Allows use of SELECT.
SHOW DATABASES Allows use of SHOW DATABASES.
SHOW VIEW Allows use of SHOW CREATE VIEW.
SHUTDOWN Allows use of mysqladmin shutdown.
SUPER Allows use of CHANGE MASTER, KILL, PURGE MASTER LOGS, and SET GLOBAL SQL statements. Allows mysqladmin debug command. Allows one extra connection to be made if maximum connections are reached.
UPDATE Allows use of UPDATE.
USAGE Allows connection without any specific privileges.
DDL(Data Definition Languages):数据定义语句,这些语句定义了不同的数据段、数据库、表、列、索引等数据库对象(create、drop、alter等)
DML(Data Manipulation Languages):数据操纵语句,用于添加、删除、更新、查询数据库记录,并检查数据完整性(insert、delete、update、select等)
DCL(Data Control Languages):数据控制语句,这些语句定义了数据库、表、字段、用户的访问权限和安全级别(grant、revoke等)
下面以MySQL为例,分别介绍这三种类别语句常用的使用方法:
1. DDL:
创建数据库:create database dbname
显示当前系统中数据库:show databases
选择要操作的数据库:use dbname
查看数据库中所有数据表:show tables
删除数据库:drop database dbname
创建数据库表:create table tablename(
column_name column_type constraints(约束条件)
…
)
查看表基本定义:desc tablename
查看表详细定义:show create table tablename
删除表:drop table tablename
修改表:注:’[]’为可选项
(1)修改表字段类型:
alter table tablename modify[column] column_definition [first | after column_name]
alter table user modify column id int(20)//column可有可无
(2)增加表字段:
alter table tablename add[column] column_definition [first | after column_name]
alter table user add column score float(5,2)//column可有可无
(3)删除表字段:
alter table tablename drop[column] col_name
alter table user drop column score//column可有可无
(4)修改表字段名:
alter table tablename change[column] old_column_name column_defition [first | after column_name]
alter table user change column score goal float//column可有可无
(5)修改表字段顺序:
add增加的新字段默认加在表的最后,change和modify默认字段位置不变,
如需修改位置,可在结尾加first(最前面)或after column_name(某一字段后)
alter table user add address varchar(20) first; alter table user modify mark int after id;
(6)更改表名:
alter table tablename(表名) rename[to] new_tablaname(新表名)
alter table user rename to userinfo//to可有可无
2.DML:
插入记录:insert into tablename(col1,col2,…coln) values(v1,v2,…vn)[,values(vv1,vv2…vvn)]
insert into user(name,age) values('sbw',20),values('gy',20)//多表插入
更新记录:
单表更新:update tablename set col1=v1,col2=v2,…col2=vn [where condition]
多表更新:update tablename t1, tablename t2 set t1.col = v1, t2.col = v2 [where condition]
update A a, B b set a.name = 'a',b.name = 'b' where a.id = b.id//多表更新
删除记录:
单表删除:delete from tablename [where condition]
多表删除:delete t1, t2,…tn from tablename t1, tablename t2…tablename tn [where condittion]
delete t1,t2 from T1 t1,T2 t2//多表删除
查询记录:select col1,…coln from tablename [where condition]
如查询所有字段可用*代替,即select * from tablename [where condition]
(1) 查询不重复记录
从图中可看出distinct 必须放在查询字段的开头,如果查询字段有多个,他会作用于多个字段,也就是要求所有字段都相同才能被排除,即如下图:
(2)条件查询:查询条件除=之外,还可以用>,<,<=,>=,!=等比较运算符,多个条件还可以用or,and等逻辑运算符
(3)排序和限制 select col1,…coln from tablename [where condition] [order by field [desc\asc] , field2[desc\asc]…fieldn[desc\asc]] [limit offset_start, row_count]
desc\asc是排序顺序的关键字,desc是降序,asc是升序(默认);offset_start表示记录的其实偏移量(起始为0),row_count表示显示的行数。
如果排序字段的值一样,则值相同的字段按照第二个排序字段进行排序,依次类推,如果只有一个排序字段,则这些字段相同的记录将会无序排列。
(4) 聚合:select [field1,field2,…fieldn]fun_name from tablename [where condition] [group by field1,field2,…fieldn [with rollup]] [having condition]
fun_name 表示要做的聚合操作,即聚合函数(sum,count(*),max,min等),count(1)与count(*)的区别在于:在数据记录都不为空的时候查询出来结果上没有差别的. 但当count(1)查询的那列有空的时候空的是要被去掉的不记入统计。.
group by表示要进行分类聚合的字段,with rollup 是可选语法,表明是否对分类聚合后的结果进行再汇总。
having关键字表示对分类后的结果在进行条件过滤;having和where的区别在于,having是对聚合后的结果进行条件的过滤,而where是在聚合前就对记录进行过滤。
(5) 表连接:当需要同时显示多个表中的字段时就需要用到表连接,表连接又分为内连接和外链连接,内连接是指选出两张表中互相匹配的记录,外连接则会选出其他不匹配的记录。
内连接:
外连接分为左连接和右连接:
左连接:包含所有的左边表中的记录甚至是右边表没有和它匹配的记录
右连接:包含所有的右边表中的记录甚至是左边表没有和它匹配的记录
(6)子查询:在进行查询时,需要的条件是另外一个select语句的结果,就需要用到子查询(in,not in,=,!=,exists,not exists)
(7)记录联合:将两个表的数据按照一定的查询条件查出后,将结果合并到一起显示,这时候需要union和union all
3.DCL:
创建用户:create user ‘username’@’host’ identified by ‘password’
create user 'sbw'@'localhost' identified by '123'
授予权限:grant privileges on databasename.tablename to ‘username’@’host’
grant select,insert on test.user to 'sbw'@'localhost'
设置更改用户密码:set password [for ‘username’@’host’] = password(‘newpassword’]
set password for 'sbw'@'lcoalhost' = password('456')//如修改当前用户则去掉for 'sbw'@'localhost'
撤销权限:revoke privileges on databasename.tablenames from’username’@’host’
revoke insert on test.user from 'sbw'@'localhost'
删除用户:drop user ‘username’@’host’
drop user 'sbw'@'host'
附表:在MySQL中的操作权限
ALTER Allows use of ALTER TABLE.
ALTER ROUTINE Alters or drops stored routines.
CREATE Allows use of CREATE TABLE.
CREATE ROUTINE Creates stored routines.
CREATE TEMPORARY TABLE Allows use of CREATE TEMPORARY TABLE.
CREATE USER Allows use of CREATE USER, DROP USER, RENAME USER, and REVOKE ALL PRIVILEGES.
CREATE VIEW Allows use of CREATE VIEW.
DELETE Allows use of DELETE.
DROP Allows use of DROP TABLE.
EXECUTE Allows the user to run stored routines.
FILE Allows use of SELECT… INTO OUTFILE and LOAD DATA INFILE.
INDEX Allows use of CREATE INDEX and DROP INDEX.
INSERT Allows use of INSERT.
LOCK TABLES Allows use of LOCK TABLES on tables for which the user also has SELECT privileges.
PROCESS Allows use of SHOW FULL PROCESSLIST.
RELOAD Allows use of FLUSH.
REPLICATION Allows the user to ask where slave or master
CLIENT servers are.
REPLICATION SLAVE Needed for replication slaves.
SELECT Allows use of SELECT.
SHOW DATABASES Allows use of SHOW DATABASES.
SHOW VIEW Allows use of SHOW CREATE VIEW.
SHUTDOWN Allows use of mysqladmin shutdown.
SUPER Allows use of CHANGE MASTER, KILL, PURGE MASTER LOGS, and SET GLOBAL SQL statements. Allows mysqladmin debug command. Allows one extra connection to be made if maximum connections are reached.
UPDATE Allows use of UPDATE.
USAGE Allows connection without any specific privileges.
相关文章推荐
- windows mysql忘记root密码的解决方法
- mysql导入sql文件出现1050和1813的解决办法
- Change MySQL default character set to UTF-8 in my.cnf?
- MySQL——启动后停止——错误编码3534
- 1、利用jdbc,连接mysql数据库
- mysql slave从库负载均衡方案选择
- mysql被sql注入的一次打脸经历
- MySQL5.7 Replication主从复制配置教程
- mysql 5.6搭建总结
- MySql 基本操作语法
- mysql 数据库连接(远程和本地原理同样)
- mysql计算两点经纬度之间的距离
- mysql查询字段名 跟注释
- mysql的数据导入导出的几种方法
- 在Ubuntu上源码安装MySQL+安装问题解决+安全优化
- mysql分页(ajax)
- mysql的环形主从配置
- MySQL用户管理、授权
- Navicat for MySQL 函数高级属性讲解
- 常用的mysql数据库命令