一、SQL 支持下列类别的命令:
(1)数据定义语言(DDL)
(2)数据操纵语言(DML)
(3)事务控制语言(TCL)
(4)数据控制语言(DCL)
二、执行命令:
(1)查询系统时间
SQL> select sysdate from dual;
注:dual表是一张神奇的表
(2)将系统时间以年月日方式显示
SQL> select to_char(sysdate,'yyyymmdd hh24:mi:ss') from dual;
(3)将系统时间以年月日展示且秒显示到小数点后6位
SQL> select to_char(systimestamp,'yyyymmdd hh24:mi:ssxff') from dual;
(4)创建表
SQL> create table student(sno number(6),sname varchar2(10),birthday date);
(5)增加表中的列
SQL> alter table student add telephone varchar2(11);
(6)修改表中列的属性值
SQL> alter table transcript3 modify grade char(10);
(6)删除表某一列
SQL> alter table student drop column telephone;
(7)更改当前会话的时间显示格式(30-AUG-19)为20190830,退出当前sql会话后,自动回复30-AUG-19格式
SQL> alter session set nls_date_format='yyyymmdd';
(8)查询当前表中空值的列,不能使用sname = null,需要使用sname is null
SQL> select * from student where sname is null;
(9)复制表(复制student表所有结构及内容到student2)
SQL> create table student2 as select * from student;
(10)复制表结构,不复制内容
SQL> create table student3 as select * from student where 1>2;
(11)清空表数据,表结构不变,清空时不写入日志,所以无法还原,慎用
SQL> truncate table student4;
(12)删除表数据,清空时,写入日志,可以还原
SQL> delete table student4;
(13)查找sname为大写的值
SQL> select * from student2 where upper(sname)='A';
(14)查询列sname中以A开头的
SQL> select * from student2 where sname like 'A%';
(15)查询sname中长度为4的行,
SQL> select * from student2 where length(sname)=4;
(16)按sno降序插叙
SQL> select * from student2 order by sno desc;
(17)别名使用,如果中间没有空格,可以直接使用别名;如果别名中间有空格,需要加双引号
SQL> select * from student2 order by sno asc,birthday;
一张成绩单:
" SNO KM SCORE
---------- ---------- ----------
1 chinese 60
1 math 60
1 english 60
2 chinese 70
2 math 70
3 chinese 80"
(18)查出每个学生的成绩总和
SQL> select sno,sum(score) from Transcript group by sno
(19)查询每门课程(KM)的平均分数
SQL> select km,avg(score) from Transcript group by km;
(20)查询(按照sno分组,即针对每个人),平均分数大于60的学生的学号和平均成绩
SQL> select sno,avg(score) from Transcript group by sno having(avg(score)>60);
(21)查询(按照sno分组,即针对每个人),平均分数大于所有学生平均成绩的学生的学号和平均分数
SQL> select sno,avg(score) from Transcript group by sno having(avg(score)>(select avg(score) from Transcript));
(22)查询(按照sno分组,即针对每个人),平均分数大于60的学生的学号和平均成绩,并按照学号的降序排列(说明了几个谓词的顺序)
SQL> select sno,avg(score) from Transcript group by sno having(avg(score)>60) order by sno desc;
(23)选择无重复的行
(24)表操作:
a、SQL> insert into student(sno,sname) values(4,'sunliu');
三、SQL操作符
四、SQL函数
1、字符函数:
2、日期时间函数
3、数字函数
4、转换函数
(1)to_char:把数值转换为字符:SQL> select to_char(33,'L99.9999')from dual;——>$33.0000
SQL> select to_char(sysdate,'yyyy-mm-dd') from dual;——>2020-02-26
(2)to_number:字符转换为数值,
SQL> select to_number('123') from dual;——>123
(3)to_date:把数值转换为日期,
SQL> select to_date('20211112','yyyymmdd') from dual;——12-NOV-21.
SQL> select to_date('20211112','yyyyddmm') from dual;——11-DEC-21.
5、混合函数:
(1)NVL,第一为空返回第二个;否则返回第一个。SQL> select nvl(1,2) from dual;——>1;SQL> select nvl('',2) from dual;——>2
(2)NVL2,第一个不空则返回第二个;否则返回第三个。SQL> select nvl2(1,2,3) from dual;——>2;SQL> select nvl2(null,2,3) from dual;——>3
(3)NULLIF,两个表达式,相等则返回空;否则第一个。SQL> select NULLIF(2,2) from dual;——>空的;SQL> select NULLIF(2,3) from dual;——>2
五、多表查询:
1、查看有多少表:SQL> select * from tab;
2、 创建表1:
create table student(sno number(6),sname varchar2(12),sage int);
insert into student values(1,'zhangsan',21);
insert into student values(2,'zhangfeifei',22);
insert into student values(3,'wangwu',23);
insert into student values(4,'zhaoyun',24);
3、创建表2
create table address(sno number(6),zz varchar2(10));
insert into address values(1,'zhengzhou');
insert into address values(2,'kaifeng');
insert into address values(3,'luoyang');
insert into address values(5,'xinxiang');
4、连接表
方法一: SQL> select s.sno,s.sname,a.sno,a.zz from student s inner join address a on s.sno=a.sno;
方法二:SQL> select s.sno,s.sname,a.sno,a.zz from student s,address a where s.sno=a.sno;
5、左外连接(就是一个表的信息全显示)
方法一:在右表的连接列中写上(+)
SQL> select s.sno,s.sname,a.sno,a.zz from student s,address a where s.sno=a.sno(+);
方法二:把inner换为 left outer
SQL> select s.sno,s.sname,a.sno,a.zz from student s left outer join address a on s.sno=a.sno;
6、右外连接
方法一:在左表的连接列中写上(+)
SQL> select s.sno,s.sname,a.sno,a.zz from student s,address a where s.sno(+)=a.sno;
方法二:把inner换为 right outer
SQL> select s.sno,s.sname,a.sno,a.zz from student s right outer join address a on s.sno=a.sno;
7、全连接:
SQL> select s.sno,s.sname,a.sno,a.zz from student s full outer join address a on s.sno=a.sno;
六、集合操作符
1、union
2、union all(相当于并集)
3、minus(减)
4、intersect(交集)
七、重命名表:
1、重命名整张表:SQL> rename student to newstudent;
2、重命名一张表的某个列:SQL> alter table newstudent rename column sno to newsno;
文章评论