首页 > 代码库 > Oracle 常用语法

Oracle 常用语法

--创建表空间
create tablespace myschool
datafile ‘D:/ms.dbf‘
size 50M

--创建用户
create user t87 identified by t87;

--授予权限(dba,connect,resource)
grant connect,resource to t87;

--撤销权限
revoke connect,resource from t87;

--修改用户密码
alter user t87 identified by a;

--锁定用户
alter user t87 account lock;

--解锁
alter user t87 account unlock;

--删除用户
drop user t87;

--建表
--主键约束:primary key
--非空约束:not null
--检查约束check (sex in(‘男‘,‘女‘))
--唯一约束:unique
--默认约束:default 20
--外键约束:constraint student_sgroup_fk foreign key(gid) references sgroup(gid)
create table student(
stuid number(8) primary key,
stuname varchar2(50) not null,
sex varchar2(10) check (sex in(‘男‘,‘女‘)),
seat number(8) unique,
age number(8) default 20,
gid number(8) not null,
constraint student_sgroup_fk foreign key(gid) references sgroup(gid)
);

alter table student add constraint student_sgroup_fk foreign key(gid) references sgroup(gid);
create table sgroup(
gid number(8) primary key,
gname varchar2(50) not null
);

--添加列
alter table student add email varchar2(50);

--修改列
alter table student modify email varchar2(100);

--删除列
alter table student drop column email;

--删除表
drop table student purge;

select * from tab;

select * from student for update;
select * from sgroup for update;

-- 创建序列
create sequence stuseq;

select stuseq.nextval from dual;
select stuseq.currval from dual;

select
from biao1 ,biao2
where biao1.ziduan=biao2.ziduan

insert into student
(stuid, stuname, sex, seat, age, gid)
values
(stuseq.nextval, v_stuname, v_sex, v_seat, v_age, v_gid);

Oracle 常用语法