--1、主键约束:
create table n_employee(
nId Integer PRIMARY KEY,
sex varchar2(20),
age varchar2(20),
deptNo integer);
create table n_dept(
deptNo integer PRIMARY KEY,
sex varchar2(20),
age varchar2(20));
insert into n_dept values(1,'m','23');
select * from n_dept;
--修改表的主键
alter table n_employee add constraint pk_1 primary key(nId);
--2、check约束
alter table n_employee add constraint check_1 check(sex='f'or sex='m');
--
3、外键约束
alter table n_employee add constraint fk_1 foreign key(deptNo) references n_dept(deptNo);
--4、创建序列:
create sequence seq_msg --emp_sequence序列名
increment by 1 -- 每次加几个
start with 1 -- 从1开始计数
nocycle-- 一直累加,不循环
--5、创建表空间:
create tablespace db_temp
datafile 'D:\oracle\product\11.2.0\dbhome_1\oradata\1706.DBF'
size 32M
autoextend ON
NEXT 32M MAXSIZE UNLIMITED
EXTENT MANAGEMENT LOCAL;
--6、plsql语句:
--if 语句
declare
score number := 50;
begin
if score >= 90 then
dbms_output.put_line('很优秀');
elsif score >= 70 then
dbms_output.put_line('良好');
else
dbms_output.put_line('不及格');
end if;
end;
--loop(循环)
declare
a int := 10;
f int := 1;
begin
loop
f := f * a;
a := a - 1;
exit when a = 1;
end loop;
a := 10;
dbms_output.put_line(a || '的阶乘是:' || f);
end;
--for
declare
a int := 10;
t int := 1;
j int;
begin
for j in 1 .. a loop
t := t * j;
end loop;
dbms_output.put_line(a || '的阶乘是:' || t);
end;
--7事务:
总结为ACID即
原子性atomicity:语句级原子性,过程级原子性,事务级原子性
一致性consistency:状态一致,同一事务中不会有两种状态
隔离性isolation:事务间是互相分离的互不影响(这里可能也有自治事务)
持久性durability:事务提交了,那么状态就是永久的
原子性atomicity:语句级原子性,过程级原子性,事务级原子性
一致性consistency:状态一致,同一事务中不会有两种状态
隔离性isolation:事务间是互相分离的互不影响(这里可能也有自治事务)
持久性durability:事务提交了,那么状态就是永久的
declare
v_extp EXCEPTION;
begin
--将李白账户减去1000
update t_staff set stasalary= stasalary-1000 where staname='李白';
--给王五账户1000
update t_staff set stasalary= stasalary+1000 where staname='王五';
--遇到不可抗原因
raise v_extp;
--提交事务
commit;
exception
when others then
ROLLBACK;---回滚
end;
select * from t_staff;
删除表:Drop table
表名;
删除表中数据:Delete from 表名;
空值不等于0
,凡是空值参与的运算,结果都为空(null
)