sql语句的总结
基本查找
select (要查找的内容 *代表是全部 )from 那个表 where 条件
基本更新
update 表名 set 要更新的字段名=要更新的值,要更新的字段名=要更新的值,要更新的字段名=要更新的值
where 条件
基本删除
delete from 表名 where 条件
基本插入
insert into user(如果只是插入特定字段,就需要把特定字段写出来,用逗号隔开,否则就可以省略) values(根据前面写的,插入要插入的值)
sql中的数据基本类型
varchar 长度是可变的
char 长度是不可变的
date :YYYY-MM-dd
time: hh-mm-ss
datetime:YYYY-mm-DD hh-mm-ss默认是为null
timestemp YYYY-mm-DD hh-mm-ss默认是当前时间
数据库语言
DDL:数据定义语言:create(创建,drop(删除,alert(插入:::对数据库的操作
create database 数据库名
drop database 数据库名
alert database character set 字符集(UTF-8)
show databases,show create database 数据库名
切换数据库 use 数据库名
DML:数据操作语言:insert(插入,update(修改,delete(删除::对数据表信息的操作
insert into 表名(列名1,列名2,列名3.。。列名N) values(值1,值2,值3.。值N)可以批量插入只要重复values就行 select 列名或者全部* from表名 where条件 delete from 表名 where条件 update 表名 set 列名=值 where条件
DCL:数据控制语言:设置用户访问权限
DQL:数据查询语言:select from where
where的语法
有一个where 属性 between 值 and 值
模糊查找包含“值”的列where 属性 like ‘%值%’ 模糊查找第二个字是“值的列where 属性 like ‘%_值%’、
范围查找包含1,54,6的列 where 属性 in(1,54,6)
order by 排序
asc ascend 升序(默认的排序)
select * from student where id=’454‘ order by price desc
desc descend 降序
聚合函数:
sum(),avg(),count()统计个数,max(),min()
select 新列名 sum(price) from student
group by
根据cno分组,分组统计每组商品的评价价格,并且平均价格要大于60
select cno,count(*) from product group by cno having avg(price)>60
外键约束:foreign key
给product中的cno添加category的cid外键约束:意思就是product中添加的cno必须是category中cid有的
alter table product add foreign key(cno) referbces category(cid)
交叉查询
内链接:只有匹配上的数据才会线束出来
select * from 表1 ,表2.。。表N where 条件 例如编号相同表1.id=表2.id
外连接:
左边的就是左表右边就是右表
会显示出来左表的所有数据,如果右表没有则用null代替
左外连接:select *from product left outer join category on cno=cid
会显示右表的又有数据,如果坐表没有则用null代替
右外连接:select *from product right ourter join category on cno=cid
分页查询
select * from product limit 起始索引,显示的页数