mysql -- 基础语句

  关于版本选取,安装等都略过,网上的教程很多。目前用的比较多的是可视化的navicat for MySQL。本篇简单陈列一些常用的命令和语句,方便在需要的时候进行查询。

========================================================================>

  ---->SQL语句的分类:

DDL,Data Definition Language,数据定义语言【结构】
管理数据库和表的结构和索引的结构
保留字:CREATE create 创建,ALTER alter 修改,DROP drop 删除

DML,Data Manipulation Language,数据操作(操纵)语言【数据】
用于添加,修改和删除表中的行
保留字:INSERT insert 插入,UPDATE update 更新,DELETE delete 删除 和TRUNCATE truncate 清空

DQL,Data Queries Language,数据查询语言【查询】 --非官方
用以从表中获得数据
保留字:SELECT select 查询 ,WHERE,GROUP BY,HAVING和ORDER BY
-·-·-·-·-·-·-·-·-·-·-·-
DTL,Data Transaction Language,事务处理语言
确保被DML语句影响的表的所有行及时得以更新
保留字:BEGIN TRANSACTION begin transaction 开启事务,COMMIT commit 提交事务 和ROLLBACK rollback回滚事务

DCL,Data Control Language,数据控制语言
授权用户或用户组操作和访问数据的权限
保留字:GRANT grant 授权 或REVOKE revoke 取消权限

========================================================================>

  ---->常用查询:

* 没有条件查询
* 查询所有
mysql> select * from users;
mysql> select id,firstname,secondname,age,count from users; #建议
* 查询部分信息
mysql> select id,firstname,secondname,count from users;
* 查询用户编号、姓名,及格(与60分相对值)
mysql> select id,concat(firstname,secondname) ,count from users;
mysql> select id,concat(firstname,secondname) ,count -60 from users;
* 修改上面查询显示字段名称,用"姓名"表示姓名,用"及格"表示及格
### 字段的别名
格式:字段 [AS] 别名
mysql> select id,concat(firstname,secondname) as 姓名 ,count -60 及格 from users;
mysql> select id,concat(firstname,secondname) as `姓 名` ,count -60 及格 from users;
### ` 重音符可以解决特殊符号,关键字等。


* 带有条件查询
* 查询分数等于60的学生
mysql> select * from users where count = 60;
mysql> select * from users where count = '60'; #数字可以使用单引号
* 查询姓"张"学生
mysql> select * from users where firstname = 张; #异常
mysql> select * from users where firstname = '张';
* 查询年龄大于18的学生
mysql> select * from users where age > 18;
* 显示分数在60-80的学生
mysql> select * from users where count >=60 and count <= 80;
mysql> select * from users where count between 60 and 80;
* 查询编号为u001和u002的学生
mysql> select * from users where id = 'u001' or id='u002';
* 查询年龄是18或20的学生
mysql> select * from users where age = 18 or age = 20;
* 查询名中含有"云"的学生
## 模糊查询,不完全匹配、like语句
## 格式: 字段 like 值
## 值只有使用符号才可以进行模糊查询。
% 匹配多个数据
'云' 只能匹配一个云
'%云' 以云结尾
'云%' 以云开头
'%云%'包含云 【】
_ 匹配一个数据
'_云'
'__云'

mysql> select * from users where secondname like '%云%';

* 查询名中第二字还有"云"的学生
mysql> select * from users where secondname like '_云%';
* 查询分数小于60 或 大于90分的学生
mysql> select * from users where count < 60 or count > 90;
* 查询分数等于60 或者 分数大于90并且年龄大于23
mysql> select * from users where count = 60 or count > 90 and age > 23;
mysql> select * from users where count = 60 or (count > 90 and age > 23);
### 运算符优先级 and 优先 or
* 查询没有考试的学生
mysql> select * from users where count is null;
* 查询所有考试的学生
mysql> select * from users where count is not null;

* 运算符 不相等 != <>


#聚合函数的使用
#聚合函数:对表中的数据进行统计,显示一个数据(一行一列的数据)
### 聚合函数不统计 null值。
#有多少条记录 count(* | 字段)
select count(*) from users;
select count(id) from users; #7
select count(count) from users; #6
#平均成绩 avg
select avg(count) from users; #不精准(没有null)
select sum(count)/count(id) from users; #精准(计算null)
#最高成绩 max
select max(count) from users;
#最小年龄 min
select min(age) from users;
#班级总成绩 sum
select sum(count) from users;
#查询所有的年龄数(排序)
### 去重复 distinct
### 排序 select..... order by 字段1 关键字, 字段2 关键字,....
### 关键字 asc 升序 , desc 降序
select distinct age from users order by age desc; # age asc 等效 age [asc]

#分组
# 添加班级字段(classes)
alter table users add column classes varchar(3);
update users set classes = 1;
update users set classes = 2 where id='u005' or id ='u006' or id ='u007';
update users set classes = 2 where id in ('u005','u006','u007');
# 查询1班和2班的平均成绩
### 分组 select ... group by 分组字段;
select classes,avg(count) from users group by classes;
select classes,sum(count)/count(id) from users group by classes;


#查询班级,平均成绩不及格的,班级成员信息
### 查询2班,成员信息
select * from users where classes = 2;

### 多表操作
select * from A,B where A.classes = B.classes and avg < 60;
#### 表的别名
select ... from 表名 [as] 别名
### 子查询,一条select语句,作为另一个select一部分。
select * from users,(
select classes,sum(count)/count(id) as cavg from users group by classes) as B
where users.classes = B.classes and cavg < 60;

#### 子查询特点
#查询结果一行一列,可以使用
select id,(xxx) from
select ... from ... where id= (xxxx)
#查询结果一行多列(查询多个值),使用关键字 in ,all 等
#查询结果多行多列,可以当另一个表使用

========================================================================>

  DML作为SQL语句的四种基本语言之一,也是前台与RDBMS通讯的SQL语言之一,通用于所有RDBMS,当然,在MYSQL中某些语句可能因为“方言”而与其它RDBMS不同。
  在DML中所包含的SQL语句包括以下几种:

  1、 INSERT语句:将数据添加到表中
    拿插入单行记录举例——通常情况下,在MYSQL中都是通过INSERT INTO VALUES语句往表中添加数据,语法为:
    INTSERT INTO 表名(列名1,列名2, 列名3,···) VALUES(‘字符串常量值’,‘日期常量值’,整型值,浮点型值);
    需要注意的是,以下这几种情况的列可以忽略:
    (1)、由RDBMS自动创建的列(如主键列)
    (2)、列被设置为NULL,不输入值不会影响数据库的完整性
    (3)、列设置了默认值
    (4)、列是TIMESTAMP类型,某些RDBMS会自动将当前时间日期作为此列的值

    2、 UPDATE语句:更新表中的数据(当数据库中某些数据过期,可以用此语句进行修改)
    语法:UPDATE 表名 SET 列1=新值1,列2=新值2,列3=新值3 WHERE 过滤条件
    注:如果忽略了WHERE将会改变所有行。

    3、 DELETE语句:删除表中数据
    删除行可以使用DELETE和TRUNCATE这两种语句,它们不同之处在于:
    DELETE语法:DELETE FROM 表名 WHERE 过滤条件
    注:过滤条件通常都是改行主键
    TRUNCATE语法:TRUNCATE TABLE 表名

    DELETE和TRUNCATE同样作为删除表的行的语句,它们有什么不同呢?:
     TRUNCATE是删除一个表里面的所有行,并且不会写入日志文件,也就是如果使用TRUNCATE,数据将会永久删除。而DELETE是一行一行的删除表中记录,另外,TRUNCATE TABLE被优化,执行起来比DELETE更快。TRUNCATE,DELETE,DROP放在一起比较:TRUNCATE TABLE:删除内容、释放空间但不删除定义。DELETE TABLE:删除内容不删除定义,不释放空间。DROP TABLE:删除内容和定义,释放空间。TRUNCATE TABLE不可以回滚,但DELETE TABLE可以。

========================================================================>

  DDL

create database dbname;  创建一个数据库
show databases;  显示系统中已有的数据库
use dbname;  切换数据库
show tables;  查看dabname中的内容
drop database dbname;  删除数据库
create table tablename(column_name_1 column_type_1 constraints,column_name_2 column_type_2 constraints,......column_name_n column_type_n constraints);  创建表
desc tablename;  查看表
drop table tablename;  删除表
ALTER TABLE newtest CHANGE old_name new_name VARCHAR(100);  字段改名
ALTER TABLE old_name RENAME new_name;  更改表名
ALTER TABLE newtest ADD COLUMN `newname` VARCHAR(100) NOT NULL; 添加字段
ALTER TABLE newtest DROP COLUMN newname;  删除表字段

原文地址:https://www.cnblogs.com/bruceChan0018/p/5952583.html