包含你见到的所有常见语句;
包含表的创建,删除,修改字段名,修改属性;
包含记录的增删改查;
包含所有记录的各种查询及内连接外连接;
以下所有例子都用person表和student表为例。
person表包含xingmin(姓名),nianling(年龄),banji(班级),nianji(年级)等字段;
student表包含id和name字段;
两表如下:
person表
student表
create user tom identified by 12345; //创建用户tom及密码12345
grant connect,resource,dba to root; //授予用户权限
drop user tom; //删除用户
create table person //创建person表
(xingmin varchar2(20) primary key not null,
nianling int default 11,
nianji varchar2(20) default '初一',
banji varchar2(20));
select * from user_tab_columns t where t.TABLE_NAME='表名'; //查看表字段与其对应数据类型
truncate table person; //删除表的所有内容,保留结构
drop table student; //完全删除表包括结构
alter table person rename to bbbb; //修改person表名为bbbb
alter table person add shengao int; //增加字段shengao,int型
alter table person modify shengao vachar2(10) default '待定'; //修改shengao字段属性为vachar2(10),默认填写‘待定’
alter table person rename column shengao to aihao; //修改字段名shengao为aihao
alter table person drop column shengao; //删除字段shengao
update person set nianling='16' where xingmin='王菲'; //修改记录
update person set shengao=null; //删除身高着一列的数据
insert into person (xingmin,nianji) values ('赵武','初一'); //增加选定字段的记录
insert into person values ('赵武',17,'初一','二班',172); //增加全部字段记录
delete from person where xingmin='赵武'; //删除一条记录
select * from person; //查询person表下的所有记录
select * from user_tables; //查看当前用户下所有表或者select * from tabs;
select distinct xingmin from person; //查找的姓名重复的只显示一次
select * from person where aihao is not null; //查找非空的爱好
select * from person where aihao is null; //查找为空的爱好
select xingmin 姓名,nianling 年龄 from person; //查询xingmin,nianling时列表标题用别名中文显示
select xingmin,nianling from person where nianji like '%四'; //查询年级中含“某四“的,并显示姓名,年龄
//'%a' 以a结尾的数据
// 'a%' 以a开头的数据
//'%a%' 含有a的数据
//'_a_' 三位且中间字母是a的
//'_a' 两位且结尾字母是a的
//'a_' 两位且开头字母是a的
select '姓名:' || xingmin || ' 年龄:' || nianling || ' 班级:' || banji as 详情 from person; //用||进行拼接“详情”为显示的表头名称
select xingmin,nvl(shengao,160) shengao from person; //查询姓名,身高,身高是空的则显示160,并不改变身高,知识用于显示或平时计算
select * from person where nianling in (15,18); //查寻年龄为15和18的记录
select * from person where nianling not in (15,18); //查询年龄不是15,18的记录
select * from person where xingmin in(select name from student where name='李高')order by aihao ; //使用in进行子查询叫李高的人,xingmin与子查询对比看有没有叫李高的,然后排序输出
select * from person p where exists(select * from student s where p.xingmin=s.name); //和上一条语句一样功能一样,当student表的数据大于person表时用exists效率高
select * from person where nianling between 11 and 16 ; //查寻年龄在11到16的记录,包含11,16
select * from person where nianling not between 11 and 16 ; //查寻年龄不在在11到16的记录
select * from person order by nianling desc; //按照年龄降序排列记录
select * from person order by nianling; //按照年龄默认升序排列记录结尾,或在结尾用asc指明升序排序
select xingmin from person union select name from student; //对两表进行并集,union会去除重复的并默认排序。union不去重且不排序
select p.xingmin,p.nianji, s.id,s.name from person p,student s where p.xingmin='王菲'; //将表person取别名p,student取别名s,进行连接查询
select nianji,count(nianji) from person group by nianji //用group by和count函数查看每个年级人数
select nianji,count(nianji) from person group by nianji having count(nianji)>3; //用group by和count函数查看每个年级人数,用having控制大于3人的才输出
select * from student s inner join person p on s.name=p.xingmin; //两表间的内连接。先找到表1的第一个name记录,然后从头到尾扫描表2的xingmin,将符合连接条件的记录与表1第一条记录连接成结果;
//当表2扫描一遍后,再从表1的第二条记录开始,从头到尾扫描表2,将符合连接条件的与表1第二条记录连接成结果,
//另一种语法形式为select * from student s , person p where s.name=p.xingmin;
select * from student s , person p where s.name=p.xingmin(+); //左连接,name与xingmin比对,符合s.name=p.xingmin的显示,不符合的左边用null填充
select * from student s , person p where s.name(+)=p.xingmin; //右连接name与xingmin比对,符合s.name=p.xingmin的显示,不符合的右边用null填充