SQL基础

1.集合的操作:
UNION:将多个查询结果相加形成一个结果集。将第一个查询中的所有行与第2个查询中的所有行相加,并消除其中相同的行形成一个集合。
UNIION ALL:与UNION的工作原理相同,不同的是UNION ALL不会消除相同的行。
INTERSECT:处理多个查询结果的交集。
MINUS:处理两个查询结果集合的差集。
2.添加/删除字段:alter table user add pwd varchar2(10);
alter table user drop column pwd;
修改:
有一个表名为tb,字段段名为name,数据类型nchar(20)。

1、假设字段数据为空,则不管改为什么字段类型,可以直接执行:
alter table tb modify (name nvarchar2(20));
2、假设字段有数据,则改为nvarchar2(20)可以直接执行:
alter table tb modify (name nvarchar2(20));
3、假设字段有数据,则改为varchar2(40)执行时会弹出:“ORA-01439:要更改数据类型,则要修改的列必须为空”,这时要用下面方法来解决这个问题:

/修改原字段名name为name_tmp/
alter table tb rename column name to name_tmp;
/增加一个和原字段名同名的字段name/
alter table tb add name varchar2(40);
/将原字段name_tmp数据更新到增加的字段name/
update tb set name=trim(name_tmp);
/更新完,删除原字段name_tmp/
alter table tb drop column name_tmp;
总结:
1、当字段没有数据或者要修改的新类型和原类型兼容时,可以直接modify修改。
2、当字段有数据并用要修改的新类型和原类型不兼容时,要间接新建字段来转移。
3.授权/回收授权语句:
1)只读权限:grant select on table tableName to user;
回收授权:revoke select on table tableName to user;
2)表的所有操作权限: grant all privileges on table table1,table2 to user;
回收授权:revoke select on table table1,table2 to user;
3)授权给所有用户:grant select on table tableName to public;
回收授权:revoke select on table tableName to public;
4)修改某个字段的权限:grant update(id),select on table tableName on user;
回收授权:revoke update(id),select on table tableName to user;
5)授权给user用户,并允许他授权给其他用户:grant select on table tableName to user with grant option;
回收授权:revoke select on table table1,table2 to user;
4.添加约束:alter table users add constraint user_unique(id,name)或者建表时直接添加:constraint users primary by (id,name)

PL/SQL语言及编程
1.PL/SQL的结构:
Declare
- - 变量,游标,常量(可选)
Begin
- -业务(必须要有的部分)
Exception
- -异常,异常后的处理(可选)
End;
2.PL/SQL常量与变量
常量的关键字:constant 语法:<常量名> constant <数据类型> := <值>
Pass_scort constant INTEGER := 60; 定义了一个常量Pass_scort,数据类型为 整数,值为60
3.if…else..语句
If {条件} then {语句序列}
Else {语句序列}
End ;
4.循环语句:
1)
Control_val := 0; - -初始化变量值为0
Loop - -开始循环
If Control_val > 5
Exit;
End if;
Control_val := Control_val + 1;
End loop;
2)for..in..loop..end
For Control_val in 0…5 loop
- -业务操作
End loop;
3.游标分为显示游标和隐示游标,游标的原理:oracle在执行select,update,delete时,oracle会在内存中分配上下文区即缓冲区,游标就是指向缓冲区的指针
显示游标:是由用户声明和操作的一种游标
隐示游标:是由oracle为所有数据操纵语句自动声明和操作的一种游标;必须要有一个into字句,隐示游标的select只能选中一行数据或者产生一行数据。
显示游标语法:
实例一:
–申明游标
declare
user_name varchar2(30);
pwd varchar2(30);
Cursor user_cur is
Select username,password from users;
Begin
–打开游标
Open user_cur ;
–提取游标
fetch user_cur into user_name,pwd;
Loop
Exit when not user_cur%found; –如果游标到尾则结束
Insert into u(name,pwd) values(user_name ,pwd);
Commit;
End loop;
–关闭游标
Close user_cur;
End;
实例二:更新
declare
type user_recode is record( –定义一个新的记录类型

 user_name varchar2(30),
 pwd varchar2(30),
 createtime date,
 lupdate date

);
user_recodes user_recode;
cursor users_cur is
select username,pwd,createtime,lupdate from user_cur;
begin
for user_recode in users_cur loop
if user_recodes.user_name=1 then update user_cur set pwd=’11’;
– commit;
end if;
end loop;
end;
隐示游标:
Begin
Select id,username,pwd into tid,user_name,password from users where id=1; (只有一行数据)
End;
4.游标变量:
type <游标变量> is ref cursor;
Return <返回类型>;
实例:
Declare
Type t_student is ref cursor;
return users%rowtype;
T_studentss t_student;
Begin
Open T_studentss for select * from users;
End;
7.创建索引:create unique index ind1 on table(id,name);
8.创建序列:
create sequence SQL_USER
increment by 1 –每次增长1
start 1
mnvalue 1
maxvalue 99999999;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值