您的位置:首页 > 数据库

常用的SQL语句大全-单表操作

2017-09-22 07:14 423 查看

Sql语言集数据定义语言,数据操纵语言,数据查询语言,数据控制语言于一体,可以完成数据库生命周期中的全部工作。

数据定义语言(DDL):完成创建,修改或删除数据库中的各种对象有create,drop,alter的命令。

数据查询语言(DQL):按各种条件查询数据库中的数据有select及其相关命令。

数据操纵语言(DML):对已经从在的数据库对其进行数据的插入,删除和修改有insert,update,delete命令。

数据库控制语言(DCL):用于授权或收回访问数据库的某种特权,控制数据操纵事物的发生时间及效果,对数据库进行监视。有grant,revoke,commit,rollback等命令。

一、数据库操作命令

启动数据库: net start mysql

关闭数据库: net stop mysql

打开数据库: mysql -uroot -p123

显示所有数据库:show databases;

创建数据库: create database mydb1;

删除数据库: drop database mydb1;

单表操作:

1、选择并使用数据库:

use mydb1

2、创建表:(宠物表)

宠物表:id、名字、主人、种类,性别、出生和死亡日期。

create table pet(

id int primary key,

name varchar(20),

owner varchar(20),

species varchar(20),

sex char(1),

birth date,

death date

);

3、插入数据:

insert into pet values(1,’ergou’,’zx’,’dog’,’f’,’2013-09-06’,null);

查看当前数据库下的所有表:show tables;

查看表结构:desc 表名;

查看表中数据:show create table pet;

4、查询所有内容

select * from pet;

5、删除一条数据:

delete from pet where id=1;

6、修改数据中的内容:

update pet set birth=’2015-09-09’ where name=’huahua’;

7、选择特殊的行

select * from pet where species=’dog’;

select * from pet where species=’dog’ and sex=’f’;

select * from pet where species=’dog’ or ‘cat’;

select * from pet where birth>’2016-09-01’ and birth<’2014-09-09’;

select * from pet where (species=’dog’ and sex=’f’)or(species=’cat’ and sex=’m’);

select * from pet where sex<>’f’;

select * from pet where sex!=’f’;

8、选择特殊列

select name,species,birth from pet where species=’dog’ or species= ’cat’;

9、排序查询:order by

select name,birth from pet order by birth;

select name,birth from pet order by birth desc;(降序)

10、分组查询

select speices,sex from pet group by species,sex;

11、模糊查询

%:表示零个或多个字符。

select * from pet where owner like ’z%’;(以z开头)

select * from pet where name like ’%b’;(以b结尾)

select * from pet where name like ’%b%’;(包含b)

_:表示任意单个字符。匹配单个任意字符,它常用来限制表达式的字符长度:

select * from flow_user where username like ‘英‘;

select * from flow_user where username like ‘英__’;

[]:表示括号内所列字符中的一个(类似正则表达式)。指定一个字符、字符串或范围,要求所匹配的对象为他们中的任一个。

select * from flow_user where username LIKE’[王李张]飞’;

[^]:表示不在括号所列之内的单个字符。其取之和[]相同,但它所要求匹配对象为指定字符以外的任一个字符。

select * from flow_user where username LIKE’[^王李张]飞’;

查询内容包含通配符时:由于通配符的缘故,导致查询特殊字符“%”、“_”、“[”的语句无法正常实现,把特殊字符用“[]”括起来便可以正常查询。

12、计数行

select count(*) from pet;

select species,sex,count(*)from pet group by species,sex;

13、像表中添加一列:

alter table event add sex varchar(20);

14、删除表中一列:

alter table event drop sex;

15、修改表名:

rename table A to B;

16、用select查询两个条件

SELECT * FROM table_name

WHERE (条件1 and 条件2) 查询两个条件都符合的结果,

或者

SELECT * FROM table_name

WHERE (条件1 or 条件2) 查询符合两个条件中满足任一条件的结果。

17、去重操作 distinct

格式: select distinct 字段名,字段名2 from 表名

select price from products;

select distinct price from products;

18、自动增长设置起始值

alter table student auto_increment=100;

in的使用

查询价格为38,68,98的商品

select * from products where price = 38 or price = 68 or price=98;

select * from products where price in(38,68,98);

where后的条件写法:

> ,<,=,>=,<=,<>,!=

like 使用占位符 _ 和 % _代表一个字符 %代表任意个字符.

select * from product where pname like ‘%新%’;

in在某个范围中获得值.

select * from product where pid in (2,5,8);

between 较小值 and 较大值

select * from products where price between 50 and 70;

将所有商品的价格+10元进行显示.(别名)

可以在查询的结果之上进行运算,不影响数据库中的值

给列起别名 格式: 字段名 [as] 别名

select price+10 from products;

select price+10 新价格 from products;

select price+10 ‘新价格’ from products;

select price+10 新 价 格 from products;– 错误

select price+10 ‘新 价 格’ from products;

select price+10
新 价 格
from products;

排序查询:

1.查询所有的商品,按价格进行排序.(asc-升序,desc-降序)

select * from products order by price desc;

2.查询名称有新的商品的信息并且按价格降序排序.

select * from products where pname like ‘%新%’ order by price desc;

聚合函数:

对一列进行计算 返回值是一个,忽略null值

sum(),avg(),max(),min(),count();

1.获得所有商品的价格的总和:

select sum(price) from products;

2.获得商品表中价格的平均数:

select avg(price) from products;

– round(值,保留小数位)

select round(avg(price),2) from products;

3.获得商品表中有多少条记录:

select
4000
count(*) from products;

分组:使用group by

1.根据cno字段分组,分组后统计商品的个数.

select cno,count(*) from products group by cno;

2.根据cno分组,分组统计每组商品的总数量,并且总数量> 200;

select cno,sum(pnum) from products group by cno;

select cno,sum(pnum) from products group by cno having sum(pnum)>200;

注意:

where和having区别:

1.where 是对分组前的数据进行过滤 ;having 是对分组后的数据进行过滤

2.where 后面不能使用聚合函数,having可以

数据类型:(了解)

java            mysql
byte            tinyint
short           smallint
int             int(★)
long            bigint
char/String     varchar(★)|char
varchar:可变长度 mysql的方言  varchar(20):  存放abc 只会占用三个
char:固定长度 char(20) 存放abc 占用20个
boolean         tinyint|int 代替
float|double    float|double
注意:
double(5,2):该小数长度为5个,小数占2个  最大值:999.99

java.sql.Date       date 日期
java.sql.Time       time 时间
java.sql.Timestamp  timestamp(★) 时间戳 若给定值为null,数据库会把当前的系统时间存放到数据库中
datetime(★) 日期+时间

java.sql.Clob(长文本)  mysql的方言(text)
java.sql.Blob(二进制)  blob


约束:

作用:
为了保证数据的有效性和完整性
mysql中常用的约束:主键约束(primary key)  唯一约束(unique) 非空约束(not null) 外键约束(foreign key)
主键约束:被修饰过的字段唯一非空
注意:一张表只能有一个主键,这个主键可以包含多个字段
方式1:建表的同时添加约束 格式: 字段名称 字段类型 primary key
方式2:建表的同时在约束区域添加约束
所有的字段声明完成之后,就是约束区域了
格式: primary key(字段1,字段2)

create table pk01(
id int,
username varchar(20),
primary key (id)
);

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

create table pk01(
id int primary key,
username varchar(20),
primary key (id)
);-- 错误的 一张表只能有一个主键

方式3:建表之后,通过修改表结构添加约束
create table pk02(
id int,
username varchar(20)
);

alter table pk02 add primary key(字段名1,字段名2..);
alter table pk02 add primary key(id,username);

insert into pk02 values(1,'tom');-- 成功
insert into pk02 values(1,'tomcat');-- 成功
insert into pk02 values(1,'tomcat');-- 失败

唯一约束:(了解)
被修饰过的字段唯一,对null不起作用
方式1:建表的同时添加约束 格式: 字段名称 字段类型 unique
create table un(
id int unique,
username varchar(20) unique
);

insert into un value(10,'tom');-- 成功
insert into un value(10,'jack');-- 错误 Duplicate entry '10' for key 'id'
insert into un value(null,'jack');-- 成功
insert into un value(null,'rose');-- 成功

方式2:建表的同时在约束区域添加约束
所有的字段声明完成之后,就是约束区域了
unique(字段1,字段值2...)
方式3:建表之后,通过修改表结构添加约束
alter table 表名 add unique(字段1,字段2);-- 添加的联合唯一
alter table 表名 add unique(字段1);-- 给一个添加唯一
alter table 表名 add unique(字段2);-- 给另一个添加唯一

////////////////
create table un01(
id int,
username varchar(20)
);
alter table un01 add unique(id,username);
insert into un01 values(1,'tom');-- 成功
insert into un01 values(1,'jack');-- 成功
insert into un01 values(1,'tom');-- 失败  Duplicate entry '1-tom' for key 'id'
/////////////////////
非空约束(了解)
特点:被修饰过的字段非空
方式:
create table nn(
id int not null,
username varchar(20) not null
);

insert into nn values(null,'tom');--  错误的 Column 'id' cannot be null


truncate 清空表 ★
格式:
truncate 表名; 干掉表,重新创建一张空表
和delete from 区别:
delete属于DML语句  truncate属于DDL语句
delete逐条删除  truncate干掉表,重新创建一张空表

auto_increment 自增
要求:
1.被修饰的字段类型支持自增. 一般int
2.被修饰的字段必须是一个key 一般是primary key

create table ai01(
id varchar(10) auto_increment
);-- 错误 Incorrect column specifier for column 'id'

create table ai01(
id int auto_increment
);-- 错误 Incorrect table definition; there can be only one auto column and it must be defined as a key
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  数据库 sql