约 束
约束包括6个:非空约束、主键约束、外键约束、唯一性约束、默认值约束 、检查约束
约束用于实施数据完整性,以确保表中的数据的完整
以emp_5和dept表为例 建立各种约束条件:
非空约束:not null
alter table emp_5 modify empno not null;
在建表时使用:
create table 表名(
id number not null;
.. .
)
默认值约束: default
alter table emp_5 modify scoo default 'null';
在建表时使用:
create table 表名(
vchar varchar2 default '默认值';
.....
)
唯一性约束:constraint uniq_empno unique(列名)
alter table emp_5 add constraint uniq_empno(别名) unique(empno);
在建表时使用:
create table 表名(
fid number,
fidefitity varchar2;
....
const
constraint pk_fid(别名) primary key(fid), -------- 主键约束
constraint unique_ide(别名) unique(fidefitity) ----- 唯一性约束
)
唯一性约束 唯一性约束有一个B-TREE 索引增强;
创建唯一性约束 的同时也给目标数据创建了一个唯一的索引;
主键约束:primary key
alter table emp_5 add constraint pri_enpno(别名) primary key(enpno);
在建表时使用:
create table 表名(
fid number auto_increment not null primary key,
....
)
注: auto_increment 让每个记录自动增加 1;
外键约束:
Alter table emp_5 add constraint fk_emp_5(别名) foreign key('外键' ) references dept(empno) (另外一个表的主键) on delete set null; -----------删除主键 所在的行 时 设置与之相关联的 外键为 null
Alter table emp_5 add constraint fk_emp_5(别名) foreign key('外键' ) references dept(deptno)
on delete cascade ; ------------- 级连删除
在建表时使用:
表1:
create table 表名1(
fid number(4) not null primary key,
fname varchar2(10),
...
)
表2:
create table 表名2(
t_id number(4) not null primary key,
t_num number(4),
...
constraint fk_num foreign key(t_num) references 表名1(fid) on delete set null,
( constraint fk_num foreign key(t_num) references 表名1(fid) on delete cascade,)
)
注: 1。 外键约束 定义在外键所在的表里头。
2。外键和主键的 类型要相同 否则会报错。
检查约束:check 用于限制表中数据的范围;
alter table emp_5 add constraint ck_sex(别名) check(sex in('M','F'));
在建表时使用:
create table 表名(
fid number not null primary key,
fsex varchar2(1),
idefitity varchar2(18),
...
constraint chk_sex check(fsex in('M','F')),
constraint chk_ide check( length(idefitity) in(15,18))
)
删除约束:
alter table table_name drop constraint constraint_name;
禁用约束:
Alter table table_name disable constraint constraint_name;
启用约束:
Alter table table_name enable constraint constraint_name;
约束不能修改 只能删除 让后再建立!
--------------------------------------------------------------------------------
创建用户:
语法:Create User userName identified by password ;
如: create user zhangsan identied by 123456;
更改密码:
Alter User username identified by password;
授予权限:
授予系统权限:
语法:grant system_priv to username;
如:grant create session to zhangsan ------- 授予系统权限;
常用的系统权限:
Create session
Create table ---创建表的权限
Create view ---- 创建 视图的权限
Create procedure ----创建存储过程的权限
授予对象权限:
语法:grant alter on table_name to user_name;
Grant index on table_name to User_name;
Grant [update | insert ](col_name) on table_name to user_name;
Grant select 0n table_name to user_name;
取消用户权限:
Revoke system_priv [,system_priv…] from user_name;
约束包括6个:非空约束、主键约束、外键约束、唯一性约束、默认值约束 、检查约束
约束用于实施数据完整性,以确保表中的数据的完整
以emp_5和dept表为例 建立各种约束条件:
非空约束:not null
alter table emp_5 modify empno not null;
在建表时使用:
create table 表名(
id number not null;
.. .
)
默认值约束: default
alter table emp_5 modify scoo default 'null';
在建表时使用:
create table 表名(
vchar varchar2 default '默认值';
.....
)
唯一性约束:constraint uniq_empno unique(列名)
alter table emp_5 add constraint uniq_empno(别名) unique(empno);
在建表时使用:
create table 表名(
fid number,
fidefitity varchar2;
....
const
constraint pk_fid(别名) primary key(fid), -------- 主键约束
constraint unique_ide(别名) unique(fidefitity) ----- 唯一性约束
)
唯一性约束 唯一性约束有一个B-TREE 索引增强;
创建唯一性约束 的同时也给目标数据创建了一个唯一的索引;
主键约束:primary key
alter table emp_5 add constraint pri_enpno(别名) primary key(enpno);
在建表时使用:
create table 表名(
fid number auto_increment not null primary key,
....
)
注: auto_increment 让每个记录自动增加 1;
外键约束:
Alter table emp_5 add constraint fk_emp_5(别名) foreign key('外键' ) references dept(empno) (另外一个表的主键) on delete set null; -----------删除主键 所在的行 时 设置与之相关联的 外键为 null
Alter table emp_5 add constraint fk_emp_5(别名) foreign key('外键' ) references dept(deptno)
on delete cascade ; ------------- 级连删除
在建表时使用:
表1:
create table 表名1(
fid number(4) not null primary key,
fname varchar2(10),
...
)
表2:
create table 表名2(
t_id number(4) not null primary key,
t_num number(4),
...
constraint fk_num foreign key(t_num) references 表名1(fid) on delete set null,
( constraint fk_num foreign key(t_num) references 表名1(fid) on delete cascade,)
)
注: 1。 外键约束 定义在外键所在的表里头。
2。外键和主键的 类型要相同 否则会报错。
检查约束:check 用于限制表中数据的范围;
alter table emp_5 add constraint ck_sex(别名) check(sex in('M','F'));
在建表时使用:
create table 表名(
fid number not null primary key,
fsex varchar2(1),
idefitity varchar2(18),
...
constraint chk_sex check(fsex in('M','F')),
constraint chk_ide check( length(idefitity) in(15,18))
)
删除约束:
alter table table_name drop constraint constraint_name;
禁用约束:
Alter table table_name disable constraint constraint_name;
启用约束:
Alter table table_name enable constraint constraint_name;
约束不能修改 只能删除 让后再建立!
--------------------------------------------------------------------------------
创建用户:
语法:Create User userName identified by password ;
如: create user zhangsan identied by 123456;
更改密码:
Alter User username identified by password;
授予权限:
授予系统权限:
语法:grant system_priv to username;
如:grant create session to zhangsan ------- 授予系统权限;
常用的系统权限:
Create session
Create table ---创建表的权限
Create view ---- 创建 视图的权限
Create procedure ----创建存储过程的权限
授予对象权限:
语法:grant alter on table_name to user_name;
Grant index on table_name to User_name;
Grant [update | insert ](col_name) on table_name to user_name;
Grant select 0n table_name to user_name;
取消用户权限:
Revoke system_priv [,system_priv…] from user_name;