您的位置:首页 > 数据库 > MySQL

mysql语法

2016-04-18 00:02 741 查看
sql:
结构化查询语言(Structured Query Language),是一种数据库查询和程序设计语言,用于存取、查询、更新数据以及管理关系数据库系统.
分类:
DDL:数据库定义语言
操作对象:数据库和表
关键词:create alter drop
操作数据库:
创建(☆)
格式:create database 数据库名称;
例如:create database day05;
扩展:
格式:create database 数据库名称 character set 字符集 [collate 校对规则];
例如:create database day0501 character set gbk;
修改(了解)
就是修改数据库的字符集或者校对规则
格式:alter database 数据库名称 character set 新的字符集 [collate 新的校对规则]
例如:
alter database day0501 character set utf8;
删除(理解)
格式:
drop database 数据库名称;
例如:
drop database day0501;
常用命令:
查看所有的数据库:show databases;
查看建库语句:show create database 数据库名称;

-- 查看字符集和默认的校对规则:show character set;
操作表:
创建表:
格式:
create table 表名(
字段描述1,
字段描述2,
字段描述3,
..
字段描述n
);
字段描述的格式: 字段名 字段类型 [约束]
例如:
create table user(
id int,
name varchar(20)
);
修改表:
格式:alter table 表名 ...
修改表名:
格式:
alter table 老表名 rename to 新表名;
例如:
alter table user1 rename to user10;
添加字段:
格式:
alter table 表名 add [column] 新的字段描述;
例如:
alter table user add password varchar(20);
修改字段类型:
格式:
alter table 表名 modify [column] 新字段描述;
例如:
alter table user modify password int;
修改字段名称:
格式:
alter table 表名 change [column] 旧字段名 新字段描述;
例如:
alter table user change password pwd varchar(20);
删除字段:
格式:
alter table 表名 drop [column] 字段名;
例如:
alter table user drop pwd;
删除表:
格式:
drop table 表名;
例如:
drop table user10;
常用命令:
进入或者切换数据库:use 数据库名称;
查看当前数据库下所有表:show tables;
查看表结构:desc 表名;
查看建表语句:show create table 表名;
/////////////////////////////////////
DML:数据库操作语言 ☆
操作对象:记录
关键词:insert update delete 
插入记录:
格式1:
insert into 表名 values(值1[,值2...]);
例如:
insert into user values(1,'微哥');
insert into user values(1,'weige');
注意:
默认插入全部的字段
values里面的值要和建表语句的字段顺序和类型保持一致
int可以不使用引号
varchar必须使用引号
格式2:
insert into 表名(字段名1[,字段名2...]) values(值1[,值2...]);
例如:
insert into user(name,id) values ('威戈',2);
注意:
插入指定的字段
values里面的值要和前面的字段顺序和类型保持一致

更新记录:
格式:
update 表名 set 字段名称=值[,字段名称=值..] [where 条件];
例如:
update user set name='张三';-- 修改了所有
update user set name='李四' where id='2';-- 有条件的修改
update user set id=3,name='王五' where id=2;
删除记录:
格式:
delete from 表名 [where 条件];
例如:
delete from user; -- 清空表
delete from user where id=1;
扩展:
在开发中,添加一个字段用来标识该条记录是否被删除.例如 isdel
///////////////////////////////
DQL:数据库查询语言 ☆
关键词:select
查询语句:select * from 表名;
搭建环境
CREATE TABLE `products` (
 `id` INT PRIMARY KEY AUTO_INCREMENT,
 `name` VARCHAR(40) ,
 `price` DOUBLE ,
 `category` VARCHAR(40) ,
 `pnum` INT(11) ,
 `description` VARCHAR(255)
);
插入值:
INSERT INTO `products` VALUES(NULL,'感悟',100,'励志',100,'一次心灵的鸡汤');
INSERT INTO `products` VALUES(NULL,'java与模式',67,'计算机',200,'让你的编程,从些不一样');
INSERT INTO `products` VALUES(NULL,'java并发编程实战',190,'计算机',49,'实战大于理论');
INSERT INTO `products` VALUES(NULL,'设计模式解析',88,'计算机',86,'头脑风暴');
INSERT INTO `products` VALUES(NULL,'搭地铁游上海',28,'生活百科',120,'一次不一样的旅行');
INSERT INTO `products` VALUES(NULL,'时空穿行',65,'科技',87,'这是一本好书');
INSERT INTO `products` VALUES(NULL,'中国国家地理',45,'生活百科',100,'了解你生活的国家');
INSERT INTO `products` VALUES(NULL,'欧洲', NULL,'生活',200,'你梦中向往的地方');
INSERT INTO `products` VALUES(NULL,'网管员必备宝典',35,'计算机',120,'上网新手必备书籍');
基本查询:
1.查询出所有商品信息
-- 查询全部
select * from products;
select id,name,price,pnum,category from products;

2.查询出所有商品的名称,价格,类别及数量信息
-- 查询部分字段
-- 格式 :select 字段名1[,字段名2...] from 表名;
select name,price,category,pnum from products;
3.查询出所有的商品类别
-- 去重操作  
-- 格式 :select distinct 字段名1[,字段名2...] from 表名;
select category from products;
select distinct category from products;

4.查询出所有商品的名称及价格,将所有商品价格加10
-- 可以在查询的结果之上进行运算.
select name,price+10 from products;
-- 注意 :null和任意数据进行运算结果都是null
-- 可以通过 ifnull(字段名,默认值)  例如:ifnull(price,0) 若价格为null则按0处理
select name,ifnull(price,0)+10 from products;
5.查询出每一个商品的总价及名称
select name,pnum*price from products;
-- 字段起别名 
-- 格式: 字段 [as] 别名
select name,pnum*price as 总价 from products;
select name,pnum*price  `总 价` from products;
select name,pnum*price  '总 价' from products;
/////////////////////////
条件查询
格式:
select * from 表名 where 条件;
select 字段1[,字段2...] from 表名 where 条件;
1.查询所有计算机类商品信息
-- 条件写法1: 支持关系运算符  > >=  < <=  = !=(<>)
-- 格式:字段名 运算符 值;
select * from products where category='计算机';
select * from products where category!='计算机';
select * from products where category<>'计算机';
2.查询出商品价格大于90的商品信息
select * from products where price > 90;
3.查询出商品总价大于10000的商品信息
select * from products where price*pnum > 10000;
4.查询出价格在100-200之间的商品信息
-- 条件写法2:支持逻辑操作符 and  or  not
-- 格式: 表达式1 and|or 表达式2
-- 格式: not 表达式
select * from products where price>=100 and price<=200;
-- 条件写法3:支持 between and 操作
-- 格式: 字段 between 较小值 and 较大值
select * from products where price between 100 and 200;
5.查询出商品价格是65,100或190的商品信息
-- 条件写法4 :支持in操作 
-- 格式: 字段 in (值1[,值2...])
select * from products where price = 65 or price=100 or price=190;
select * from products where price in (65,100,190);

6.查询出商品的名称中包含java的商品信息。
-- 条件写法5:匹配操作 like
-- 格式 : 字段 like 匹配规则;
-- 匹配内容
like '龙'   值为龙
like '%龙'  值以龙结尾
like '龙%'  值以龙开头
like '%龙%' 值包含龙

-- 匹配个数
like '_'  值为一个字符
like '__' 值为两个字符

select * from products where name like '%java%';

select * from products where name like '%JAVA%';

7.查询出书名是两个字的商品信息
select * from products where name like '__';
-- length(字段名) = 值
select * from products where length(name)=6;
8.查询出商品价格不为null商品信息
错误的:select * from products where price != null;
-- 条件写法6: null操作   
-- 格式1:字段 is null
select * from products where price is null;
-- 格式2:字段 is not null
select * from products where price is  not null;
-- select * from products where not price is null;
排序:order by
格式1:
select * from 表名 [where 条件] order by 排序字段 排序方式;
格式2:
select * from 表名 [where 条件] order by 排序字段1 排序方式,排序字段2 排序方式;
排序方式:
升序:asc(默认)
降序:desc
1.查询出所有商品,并根据价格进行升序排序
select * from products order by price;
2.查询出所有商品,根据数量进行升序排列,如果数量相同,根据价格进行降序排列
select * from products order by pnum asc,price desc;
聚集函数:
它是对一列的值进行计算,然后返回一个单一的值;另外聚合函数会忽略空值
sum(字段):求和
count(字段):计数
max(字段):最大值
min(字段):最小值
avg(字段):平均数
格式:
select 聚集函数 from 表名 [where 条件];
1.统计商品表中共有多少条记录
select count(id) from products;
select count(price) from products;
开发中
select count(*) from products;
2.统计商品表中价格大于50的有多少条记录
select count(*) from products where price>50;
3.统计有多少商品
select sum(pnum) from products;
4.统计所有商品的总价值
select sum(pnum*price) from products;
5.统计所有商品的平均价格
select sum(pnum*price)/sum(pnum) from products;
-- round(值,保留几位小数)
select round(sum(pnum*price)/sum(pnum),2) '平均价格' from products;
6.统计出记录中pnum的平均值
select avg(pnum) from products;
7.统计出商品表中price最大值
select max(price) from products;
8.统计出商品表中price最小值
select min(price) from products;
9.统计出生活百科类图书的总数量
select sum(pnum) from products where category='生活百科';
分组:group by
格式1:
select 分组字段[,集合函数] from 表名 [where 条件] group by 分组字段;
格式2:
select 分组字段[,集合函数] from 表名 [where 条件] group by 分组字段 having 条件;
1.对商品分类别统计,求出每一种类商品的总数量
select category,sum(pnum) from products group by category;
2.对商品分类别统计,求出每一种类商品的总数量,数量要大于100
错误的:select category,sum(pnum) from products where sum(pnum)>100 group by category;
select category,sum(pnum) from products  group by category having sum(pnum)>100;
where和having的区别:
1.where后面不能使用聚集函数,having可以
2.where是对分组之前的数据进行筛选,having是对分组之后的数据进行筛选.
DQL的使用小结:
格式:

select 分组字段[,聚集函数] from 表名 where 条件 group by 分组字段 having 条件 order by 排序字段 asc|desc;
注意:
order by 永远放在语句的最后.
执行顺序:
1.先确定那张表. from
2.确实是否有条件 where
3.若需要分组,使用group by 
4.若需要对分组之后的数据进行筛选,执行having
5.确定要显示那些字段,执行select
6.确定字段的显示方法,执行order by

///////////////////
DTL:数据库事务语言
DCL:数据库控制语言

//////////////////////////////

约束:
保证数据的有效性和完整性.
在mysql中有主键约束(primary key),唯一约束(unique),非空约束(not null),外键约束(foreign key)
主键约束(primary key)
作用:确定字段是该条记录唯一标识.
注意:
主键可以是一个字段,也可以是多个字段.
一张表只能有一个主键.
特点:
被主键约束修饰过的字段唯一 非空.
使用:
格式1:在声明字段的同时,添加主键约束
create table pk01(
id int primary key,
name varchar(20)
);

insert into pk01 values(1,'tom');-- 成功
insert into pk01 values(1,'tom');-- 错误 Duplicate entry '1' for key 'PRIMARY'
insert into pk01 values(null,'tom');-- 错误的  Column 'id' cannot be null

扩展:
一般情况下 使用id作为主键,id没有任何实际意义.
格式2:在声明字段之后,在约束区域添加主键约束 : [constraint] primary key(字段1[,字段2...])
create table pk02(
id int,
name varchar(20),
primary key (id,name)
);

insert into pk02 values(1,'tom');
insert into pk02 values(1,'jack');
insert into pk02 values(1,null);--  错误
insert into pk02 values(null,'rose');

格式3:创建完表之后,通过修改表结构添加主键约束. alter table 表名 add primary key(字段1[,字段2...])
create table pk03(
id int,
name varchar(20)
);
alter table pk03 add primary key(name);
使用小结:
开发中最常使用的是第一种.只能在一个字段上添加主键约束.
若需要在多个字段上添加主键约束(联合主键),请使用第二种或第三种.

create table pk04(
id int primary key,
name varchar(20) primary key
);-- 错误的  Multiple primary key defined
/////////////////
唯一约束(unique)
特点:被修饰的字段唯一.对null值不起作用
格式1:在声明字段的同时,添加唯一约束    写法 : 字段名  字段类型 unique
create table un01(
id int unique,
name varchar(20)
);
insert into un01 values(1,'tom');-- 成功
insert into un01 values(1,'tom');-- 错误 Duplicate entry '1' for key 'id'
insert into un01 values(null,'tom');-- 成功
格式2:在声明字段之后,在约束区域添加唯一约束 写法:[constraint] unique(字段1[,字段2..])
create table un02(
id int,
name varchar(20),
unique(id,name)
);-- 添加联合的唯一约束
insert into un02 values(1,'tom');
insert into un02 values(1,'tom');-- Duplicate entry '1-tom' for key 'id'
insert into un02 values(1,'java');

create table un04(
id int unique,
name varchar(20) unique
);

格式3:在创建完表之后,通过修改表结构添加唯一约束 alter table 表名 add unique(字段1[,字段2])
create table un03(
id int,
name varchar(20)
);
alter table un03 add unique(id);
使用小结:
在开发中一般不添加.
在项目最后上线的时候通过第三种方式添加唯一约束.
///////////////////
非空约束(not null)
特点:被修饰的字段非空
格式:在声明字段的同时添加非空约束.
create table nn(
id int not null,
name varchar(20) not null
);
insert into nn values(null,'tom');-- 错误的
insert into nn values(1,null);-- 错误的

/////////

插入记录乱码问题:
方法1:临时修改
set names gbk;
方法2:永久修改
安装目录下/my.ini文件 
大概50行左右
[client]

port=3306

[mysql]

default-character-set=utf8

把utf8改成gbk,然后重启服务

///////////////////////////////////////

字段类型:
java mysql
byte tinyint
short smallint
int int☆
long bigint

char
String
char(n) char(5):长度不可变 例如:存入abc  在数据库中"abc  "
varchar(n)☆
varchar(5):长度可变 例如:存入abc  在数据库中"abc"

boolean

float float
double(m,d) m代表的数字的长度,d代表小数位个数
double(5,2)  存放的最大值为 999.99
double
double

java.sql.Date
date:日期
java.sql.Time
time:时间
java.sql.Timestamp timestamp时间戳
dateTime:日期和时间.

java.sql.Blob
blob
java.sql.Clob   text

///////////////////////////////////////////

回顾:
sql:
DDL:数据定义语言
操作的对象:数据库和表
关键字:create alter drop
create database 数据库名 [character set 字符集 [collate 校对规则]];
alter database 数据库名 [character set 字符集 [collate 校对规则]];
drop database 数据库名;

show databases;

create table  表名(字段描述[,字段描述...]);
字段名称 字段类型 [约束]
修改表名:alter table 表名 rename to 新表名;
添加字段:alter table 表名 add 新的字段描述;
修改字段类型:alter table 表名 modify 新的字段描述;
修改字段名称:alter table 表名 change 字段名 新的字段描述;
删除字段:alter table 表名drop 字段名称;

查看表结构:desc 表名;
进入或者切换数据库:use 数据库名称;

drop table 表名;-- 删除表
DML:数据操作语言
插入记录:
insert into 表名 values(值1[,值2..]);
insert into 表名(字段名1[,字段名2..]) values(值1[,值2..]);
DQL:数据查询语言(非官方)
DTL:数据事务语言
DCL:数据控制语言 
DBA
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: