------------------------------------最全sql语句-----------------------------
#查看数据库
注:sql语句不区分大小写
show
databases;
#创建数据库
create
database 1705A;
#使用数据库
use
1705A;
#
创建表
create
table student(
id
int (11) auto_increment primary key, //设置主键
name
varchar(20) unique ,//不能为空
address
varchar(30)
);
#查看表设计
desc
student ;
#添加数据
insert
into student (id,name,adress)values(1,'张三','北京');//添加单行
#添加多行
insert
into student (id,name,adress)values
(1,'张三','北京'),
(2,'李四','上海'),
(3,'王五','广州');
#查看表内容
select
*from student;
-----------------------------表设计-------------------------------
下面是修改表设计
#添加表字段
alter
table student add age int;//默认长度为 11
#将某个字段添加到另外一个后面
alter
table student modify address varchar(30);
#修改字段类型
alter
table student change name namechar(5);
#修改字段名称
alter
table student change name username varchr(100);
#删除一个字段
alter
table student drop age;
#修改表名称
alter
table student rename stu;//把student 换成了 stu
#删除表
drop
tanle stu;
#删除数据库
drop
databanse 1705A;
-----------------------------修改--------------------------------
修改表中的数据
#update根据ID 修改SEX 性别
update
student set sex ='女' where id=1;//把ID等于一的性别改成了 女
update
student set sex ='女'; //把所有的性别都换成了 女
#将姓名地址进行修改
update
student set name ='赵六',address ='清华' where id=4;
#根据地址是
上海 年龄为25岁的学生修改的字段
update
student set name ='熊大',sex='男' where address ='上海' and age='男';
------------------------删除----------------------------------
#删除名字是
张三的 所有信息
delete
' ;
from student where name ='张三
#删除表中的所有内容但是表依然存在
delete from student;
#删除地址是清华或者年龄是16 岁的
delete from student where address ='清华' or age=16;
----------------------------查询------------------------------
#查询表中所有内容 *代表所有的字段
select*from student;
#查询名字,地址的内容
select name from student;//查询名字
select name ,address student;//查询名字和地址
#查询上海 ,并且18岁的学生
select name from student where address='上海' and age=18;
#查询姓名是 aa 或者年龄是16岁的学生信息
select id ,name,age,address from student where name='aa' or=16;
#查询年龄在20岁-30岁之间的信息
select name,age from where age between 20 and 30;
#查询年龄大于16岁的学生姓名和年龄
select name ,agefrom student where age>16;
#查询不是李四的学生名称(有两种写法)
select name from student where name !='李四';
select name from student where name <李四>;
#查询ID是 7,9,15的学生信息
select *from student where id in(1,9,15);//安装从小到大排序
#分页查询
select*from student limit 0,2; //0是索引 然后数了2页
#查询id为偶数的学生信息
select*from student where id%2=0;
#按照从小到大排序
select *from student order by age asc;//注:asc 可以不写 默认是从小到大排序
#按照从大到小排序
select*from student order by age desc;//注:desc必须写
#查询和赵六年龄相同的学生信息和年龄
select name,age from student where age=(select age from student where name='赵六');
#查询姓 李 的学生姓名
select name from student where name like '李%'//姓李的,也就是第一个字是 李
select name from student where name like '%李%';//名字中间含有 李 字的
select name from student where name like '李_';//查询两个字然后第一个字是 李
select name from student where name like '李__';//查询三个字是姓李的
------------------------聚合函数-----------------------------
#查询address
不同的学生数量
select
count(*) address as '数量' from student group by address;
#聚合函数中可以存放字段
select
count(id) as co ,address from student group by address;
#存放整数
select
count (1)as co ,address from student group by address;
#求年龄的平均值
select
avg(age) as'平均年龄' from student;
#获取最大值
select
max (age) from student;
#获取最小值
select
min (age) from student;
#求和
select
sum(age)from student;
#统计相同地方的人数大于2
的有哪些
select
count(id) as co,address from student group by address having co>2;//注group可以单独使用,但是having 不能单独使用
#group
by 和order by 的区别
一.group
by为分组,需要与聚合函数一起使用 条件是 having 表示
二.order
by 为排序 可以按照 从大到小 从小到大 排序 asc和desc
#查询清华的人数
select
count (id)as co ,address from student group by address having address='清华';
选择:
select
*
from
table1
where
范围
插入:
insert
into
table1(field1,field2)
values
(value1,value2)
删除:
delete
from
table1
where
范围
更新:
update
table1
set
field1=value1
where
范围
查找:
select
*
from
table1
where
field1
like
’%value1%’
---like的语法很精妙,查资料!
排序:
select
*
from
table1
order
by
field1,field2 [
desc
]
总数:
select
count
as
totalcount
from
table1
求和:
select
sum
(field1)
as
sumvalue
from
table1
平均:
select
avg
(field1)
as
avgvalue
from
table1
最大:
select
max
(field1)
as
maxvalue
from
table1
最小:
select
min
(field1)
as
minvalue
from
table1