sql常用操作(一)

sql(structured query language,结构化查询语言)语言:和数据库交互的语言,进行数据库管理的语言。

1.1 sql语句的作用:说白了就是增删改查

管理数据库

管理表

管理数据

2数据库的操作

2.1创建数据库

create database db_name   

    [default] character set charset_name  -- character set:指定数据库采用的字符集

[default] collate collation_name  --collate:指定数据库字符集的比较方式(校对规则)

例:

create database mydb1;

create database java0929 default character set utf8;

 

2.2查看数据库

1)查询所有数据库

 show databases;

2)显示数据库创建语句:

show create database db_name;

 

2.3修改数据库

alter database db_name   

    [default] character set charset_name 

|   [default] collate collation_name

例:alter database mydb2 default character set gbk;

12.4删除数据库

drop database db_name

3表的操作

注意(创建表前,要先使用use db语句使用库)

3.1查看所有表

show tables;

3.2创建表

create table table_name

(

field1  datatype,

field2  datatype,

field3  datatype

)

--field:指定列名 datatype:指定列类型

3.3查看表结构

desc 表名;

 

3.3数据类型

数据类型:http://www.runoob.com/mysql/mysql-data-types.html

最常用的有四个:

整型: int

浮点型:double(精度更大,常用)

字符:varchar(可变长度,最常用,按实际占用的为准)

日期:datetime(年月日时分秒)

3.4删除表

drop table 表名;

3.5修改表

1)添加字段

alter table 表名add column 字段名 字段类型;

例:alter table order01 add column odesc varchar(50);

2)删除字段alter table 表名 drop column 字段名;

例:alter table order01 drop column money;

3)修改字段类型

alter table 表名 modify column 字段名 字段类型;

例:alter table order01 modify column otime varchar(20);

4)修改字段名称

alter table 表名 change column 原字段 新字段 字段类型;

例:alter table order01 change column otime ordertime varchar(20);

5)修改表名称

alter table 原表名 rename to 新表名;

例:alter table order01 rename to order02;

4 数据的操作

4.1增加数据

插入所有字段。一定依次按顺序插入

insert into 表名 values(值1,值2,值3...);

-- 注意不能少或多字段值

例:insert into order02 values(1,'名称1','2018-11-13','商品描述');

-- 插入部分字段

insert into student(id,name) values(2,'李四');

insert into 表名(字段1,字段2,字段3...) values(值1,值2,值3...);

例:insert into order02(oid,productname,odesc) values (2,'手表','手表的描述');

4.2修改数据

带条件的修改(推荐使用)

update 表名 set字段名=值,字段名=值,....

例:update student set gender='男',age=30 where id=2;

4.3删除数据

4.3.1-- 带条件的删除(推荐使用)

delete from 表名 where条件;

例:delete from student where id=2;

4.3.2两种全表删除的方式:

delete from: 可以全表删除     

1)可以带条件删除  

2)只能删除表的数据,不能删除表的约束     

3)使用delete from删除的数据可以回滚(以后学“事务”时会再学)

-- truncate table: 可以全表删除  

1)不能带条件删除

2)即可以删除表的数据,也可以删除表的约束

3)使用truncate table删除的数据不能回滚

truncate table student;

类似一个进回收站,一个直接从硬盘上删了 

5查询数据(重点)

以下都是单表查询:

5.1查询所有列

select * from 表名;

5.2查询指定列

select 字段1,字段2,字段3... from student;

例:select id,name,gender from student;

5.3查询时添加常量列(添加别名)

select 字段 as '别名' from 表名;

例:select id,name,gender,age,'java就业班' as '年级'  from student;

5.4查询时合并列

select id,name,(servlet+jsp) as '总成绩' from student;

注意:合并列只能合并数值类型的字段

结果图:

5.5查询时去除重复记录

select distinct 字段 from 表名;

-- 另一种语法

select distinct(字段) from 表名;

5.6条件查询

条件查询使用where

1)逻辑条件and(与)     or(或)

select * from score where sid=1 and ssex='男';

select * from score where sid=2 or ssex='女';

2)比较条件: >   <   >=  <=  =  <>(不等于) between and (等价于>= 且 <=)

例:

select * from score where html>60 and js<=80;

select * from score where html>=60 and html<=100;

select * from score where html between 60 and 100;

select * from score where jquery<>66;

3)判空条件(null 空字符串)

 null vs  空字符串

-- null:表示没有值

-- 空字符串:有值的,但是值是空字符串

例:

/*判断是否为null*/

select * from score where js is not null;

select * from score where js is null;

/*判断是否为空字符串*/

select * from score where ssex <> '';

select * from score where ssex = '';

4)模糊条件

like

% : 表示任意个字符

 _ : 表示一个字符

例:

select * from score where sname like '张%';

select * from score where sname like '__';

select * from score where sname like '%超%';

select * from score where sname like '张__';

5.7聚合查询(使用聚合函数的查询)

常用的聚合函数: sum()  avg()  max()  min()  count()

注意:count()函数统计的数量不包含null的数据

-- 使用count统计表的记录数,要使用不包含null值的字段

例:

select sum(js) as 'js总成绩' from score;

select sum(js) as 'js总成绩' from score where ssex='男';

select avg(html) as 'html平均分' from score;

select max(jquery) from score;

select min(js) from score;

select count(*) from score;

5.8分页查询

语法格式:limit 起始行,查询几行

行:一条记录

起始行从0开始

用户只能点页数,所以可以获取到“当前页”,计算起始行:(当前页-1*每页显示的条数,每页显示的条数可以人为设定。

例:

/*第一页*/

select * from score limit 0,2;

/*第二页*/

select * from score limit 2,2;

/*第三页*/

select * from score limit 4,2;

5.9 查询排序(order by

语法 order by 字段 asc/desc

默认情况下,按照插入记录顺序排序

select * from 表名;

-- asc: 顺序,正序。数值:递增,字母:自然顺序(a-z

-- desc: 倒序,反序。数值:递减,字母:自然反序(z-a)

默认正序,所以asc可以不写 

例:

select * from score order by html;

 

select * from score order by js desc;

先写where,再order by

select * from score where ssex='' order by js desc;

 

按两个字段排序:(当第一个字段有相同值时,再按第二个排)

select * from score order by js,jquery desc;

5.10 分组查询(group by)

5.10.1

select 字段1, 字段2 from 表名 group by 字段; 

例:

select ssex,count(*) from score group by ssex;

select ssex as '姓别',sum(js) as 'js总分' from score group by ssex;

5.10.2分组查询后筛选

分组之前条件使用where关键字,分组之后条件使用having关键字

having只能和group by一起用

例:

/*查询总人数大于2的性别*/

select ssex from score group by ssex having count(*)>2;

查询总结:

注意顺序:

select [all | distinct] 字段或表达式列表 [from子句] [where子句] [group by子句] [having子句] [order by子句] [limit子句]

原文地址:https://www.cnblogs.com/hzhjxx/p/9947860.html