基本sql语句由刀豆文库小编整理,希望给你工作、学习、生活带来方便,猜你可能喜欢“sql基本语句”。
典型SQL语句汇总
Sys用户是超级用户,具有sysdba的角色,密码是:change_on_install。System用户是管理操作员,具有sysoper的角色,密码是:manager。
普通登录用户:
用户名:scott,密码:tiger
用户名:sys,密码:change_on_install
用户名:system,密码:manager
用户名:sysman,密码:oem_temp
1.创建用户:create user
2.给用户修改密码:alter user 用户名 identified by 密码
3.删除用户:drop user 或者drop user 用户名[cascade]
4.授权操作
1)授权连接:grant connect to xiaoming
2)连接数据库:conn xiaoming/m1234
3)授权小明对emp表的选择操作:grant select on emp to xiaoming
4)授权小明对emp表的更新操作:grant update on emp to xiaoming
5)授权小明对emp表的所有操作:grant all on emp to xiaoming
6)对象权限授权:grant select on emp to xiaoming with grant option
7)系统权限授权:grant connect to xiaoming with admin option
5.建表
表1:Create table student(xh number(4),表2:create table claes(Xm varchar2(20),claId number(2),Sex char(2),claName varchar2(40),Birthday date,);
Sal number(7,2));
6.对上述两张表的操作:
1)添加一个字段:alter table student add(claId number(2))
2)修改字段长度:alter table student modify(xm varchar2(30))
3)删除一个字段:alter table student drop column sal
4)修改表的名字:rename student to stu
5)删除表:drop table student
6)改变日期的格式:alter seion set nls_date_format=‟yyyy_mm_dd‟
7)添加数据:insert into student values(„A001‟,‟张三‟,‟男‟,‟11-12月
-1997‟,‟113.5‟,10)或者insert into student(xh,xm,sex,birthday)values(„1‟,‟aa‟,‟女‟,null)
8)修改数据:update student set sex=‟女‟where xh=‟A001‟,修改多个字段:update student set sal=sal/2,claId=3 where sex=‟男‟
9)删除数据:
Delete from student;--删除表
Drop table student;--删除表的结构和数据
Delete from student where sal=2000;--删除表中的数据
Truncate table student;--删除表中的所有记录,表结构还在,无法找回删除的记录,速度快
7.表的查询
Select ename,sal,job from emp
Select sal*13+nvl(comm,0)*13 “年工资”,ename from emp
Select ename,sal from emp where sal>=2000 and sal
Select distinct ename,deptno from emp;--取消重复行
Select * from emp where sal>(select avg(sal)from emp)
8.如何使用like操作符:%:任意0到多个字符,_:表示任意单个字符
Select ename,sal from emp where ename like „s%‟
Select ename,sal from emp where ename like „_s%‟
9.where条件中in的使用:
Select * from emp where empno in(7844,7566)
Select * from emp where mgr is null
10.order by的使用
Select * from emp order by deptno asc,sal desc,hiredate desc;--排序默认是asc
11.group by用于对查询结果进行分组统计。
Having用于限制分组显示结果。
Select avg(sal),deptno from emp
group by deptno
having avg(sal)
12.创建存储过程
1)先创建一个简单的表:
Create table mytest(name varchar2(30),paword varchar2(30))
2)创建过程:
过程1:
create procedure sp_pro1 is
begin
insert into mytest(„EE‟,‟m1234‟);
end;
过程2:
create or replace procedure sp_pro2 is
begin
delete from mytest where name=‟EE‟;
end;
select * from rh_blood where userid='K999998264' and f_datetime_device between TO_DATE('2014-01-01 00:00:01','yyyy-mm-dd hh24:mi:')and
TO_DATE('2014-01-31 23:23:59','yyyy-mm-dd hh24:mi:');