mysql一些基础语法

sql语句

select sno,grade
from sc
where cno=‘3’
order by grade desc;
数据查询 select
数据定义 create,drop,alter
数据操纵 insert,update,delete
数据控制 grant,revoke
三级模式结构:
内模式、模式、外模式
基本表、存储文件、视图
学生-课程模式S—T:
学生表:Student(Sno,Sname,Ssex,Sage,Sdept)
课程表:Course(Cno,Cname,Cpno,Ccredit)
学生选课表:SC(Sno,Cno,Grade)
1.数据定义
模式定义
表定义
视图定义
索引定义
操作对象 操作方式
创建 删除 修改
模式 create schema drop schema
表 create table drop table alter table
视图 create view drop view
索引 create index drop index alter index
一个数据库可以建多个模式
一个模式下通常包含多个表、视图和索引等数据库对象
数据字典:以表的方式存取
模式定义
create schema “S-T” authorization wang;
create schema authorization wang;//没有指定模式名,模式名隐含为用户名
create schema “Test” authorization zhang;
create table tab1(col1 smallint,
col2 int
)
删除模式
drop schema 模式名 cascade|restrict
级联 限制
级联:删除模式的同时把该模式中的所有数据库对象全部删除
限制:如果该模式中定义了下属的数据库对象(如表、视图等),则拒绝该删除语句的执行
仅当该模式中没有任何下属的对象才能执行
基本表的定义
列级完整性约束条件:涉及相应属性列的完整性约束条件
表级完整性约束条件:涉及一个或多个属性列的完整性约束条件
如果完整性约束条件涉及到该表的多个属性列,则必须定义在表级上
create table Student
foreign key (cpno)references Course(Cno),
primary key(Cno)
设置搜索路径的方式:
SET search_path To “S-T”,PUBLIC;
drop table 表名 restrict|cascade;
索引:加快查询的速度
create unique|cluster index 索引名 on Student(Sno);
聚簇索引
alter index 旧索引名 rename to 新索引名;
drop index 索引名;
数据查询单表查询
别名在列名之后用一个空格
消除取值重复的行distinct
查询条件 谓词
比较
确定范围 between and,not between and
确定集合 in, not in
字符匹配 like,not like
空值 is null,is not null
多重条件(逻辑运算) and,or,not
select distinct sno
from sc
where grade<60;
sdept in(‘cs’,‘ma’)
like字符串匹配
%任意长度的字符串
_单个字符
where Sname like’刘%’;
使用换码字符将通配符转义为普通字符
select Cno,ccredit
from Course
where Cname like ‘DB_Design’ESCAPE’’;
order by对结果的排序desc 降序 asc 升序
单表查询2
聚集函数的使用:
count():统计元组个数
count(distinct|all 列名):统计一列中值的个数
sum(distinct|all 列名):计算一列值的总和
avg(distinct|all 列名):计算一列值的平均值
求一列值中的最大和最小值:
max(distinct|all 列名)
min(distinct|all 列名)
group by语句
细化聚集函数的作用对象
如果未对查询结果分组,聚集函数将作用于整个查询结果
对查询结果分组后,聚集函数将分别作用于每个组
按指定的一列或多列分组,值相等的为一组
求各个课程号及相应的选课人数
select Cno,Count(Sno)
from SC
GROUP BY Cno;
查询选修了3门以上课程的学生学号
select Sno
from sc
group by sno
having count(
)>3;
在where子句中不能用聚合函数作为条件表达式
where作用于记录
having作用于记录下的分组
连接查询
等值与非等值连接
查询每个学生及其选修课程的情况
select Student.,sc.
from Student,SC
where Student.Sno=SC.Sno;
冗余,列出全部属性
1.嵌套循环法
2.排序合并
3.索引连接
查询选修了二号课程且成绩在90分以上的所有学生的学号和姓名
select student.sno,sname
from student,sc
where student.sno=sc.sno and sc.cno=‘2’ and sc.grade>90;
自身连接
别名
查询每一门课的直接先修课的名称
select first.cname,second.cname
from course first,course second
where first.cpno=second.cno;
外连接
select student.sno,sname,ssex,sage,sdept,cno,grade
from student left out join sc on (student.sno=sc.sno);
嵌套查询:
order by 不能出现在嵌套块里面,只出现在最外层的子查询里面
不相关子查询
相关子查询
带in谓词的子查询
查询与刘晨在一个系的学生
select sno,sname,sdept
from student
where sdept in
(select sdept
from student
where name=‘刘晨’);
带有比较运算符的子查询
带有any或all谓词的子查询
select sname,sage
from student
where sage<any(
select sage
from student
where sdept=‘cs’)
and sdept<>‘cs’
带有exist谓词的子查询
不返回任何数据,只产生逻辑真值“true”或“false”
feikong kong
查询所有选修了一号课程的学生姓名
select sname
from student
where exists
(
select *
from sc
where sno=student.sno and cno=‘1’);
没有
select sname
from student
where not exists
(
select *
from sc
where sno=student.sno and cno=‘1’);
集合查询
并 union
union系统自动去重
union all 不去重
交intersect
差except
数据更新
插入数据
insert
into 表名(属性名1,属性名2)
values (常量1,常量2)
修改数据
update 表名
set 列名=表达式,列名=表达式…
where 条件;
1.修改某一元组
update student
set sage=22
where sno=‘201215121’;
2.修改多个元组的值
update student
set sage=sage+1;
3.带子查询的修改语句
将计算机科学系全体学生的成绩置0
update scc
set grade=0
where sno in
(select sno
from student
where sdept=‘cs’);
删除数据
delete from 表名
where 条件;
删除某一个元组的值
delete from student
where sno=‘201215128’;
删除多个元组的值
delete from sc;
带子查询的删除语句
delete
from sc
where sno in
(select sno
from student
where sdept=‘cs’);
空值的处理
不知道、不存在、无意义
1.该属性应该有一个值,但目前不知道它的具体值
2.该属性不应该有值
3.由于某种原因不便于填写
空值的判断
select *
from student
where ssex is null or sage is null;
属性定义:
有not null约束条件的不能取空值
加了unique限制的属性不能取空值
码属性不能取空值
算术运算 空值
比较运算 unknow

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

想成为前端工程师滴小小白

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值