Oracle数据库3-账户管理&二维表管理
Oracle的账户管理
权限:具备某类事物的操作的能力,此能力称为权限。
角色:一系列权限的集合
Oracle自带账户
system 管理账户
特点:具备大部分oracle的操作权限,主要用来管理普通账户及oralce的数据
使用人:oracle数据维护工作人员
sys 超级管理员账户
特点:具备system的所有权限,同时又具备其他的权限
使用人:oracle攻城狮
1、创建账户
使用system账户,并使用dba身份,登录oracle管理系统
create user test identified by test ;
2、维护账户
赋予权限 grant 权限或者角色名 to 用户名
grant connect to test; --给用户赋予登录权限
grant resource to test; --给用户资源操作权限
grant dba to test; --给用户赋予dba权限
select * from scott.emp; --查看其它用户的表 (用户名.表名)
删除权限 revoke 权限或者角色名 from 用户名
revoke dba from test;
3、删除账户 drop user 用户名
drop user test;
Oracle的二维表管理
oracle二维表的创建
使用:create table 表名(字段名 类型,字段名 类型,....);
数据类型:
number类型
数值类型
整数类型 number(a) 总长度为a
浮点数类型 number(a,b) 总长度为a,小数位长度为b,小数位可以不写。
varchar2类型
字符类型 varchar2(ln) ln表示字符的最大长度,实际存储内存长度是根据字符大小来分配,但是最大不能超过ln
特点:动态分配存储空间,节省空间
char类型
字符类型 char(ln) 不管字符数据长度是多大,直接开辟ln大小的空间存储数据
特点:存储效率高于varchar2
date类型
create table student(
sno number(10),
sname varchar2(100),
sage number(3),
ssex char(100),
sfav varchar2(500),
sbirth date
)
oracle二维表创建并添加约束
2、非空约束
3、检查约束
4、唯一约束
5、外键约束
简单的表创建和字段类型
create table student(
sno number(10) , --primary key
sname varchar2(100) , --not null
sage number(3), --check(sage<150 and sage>0)
ssex char(4) , --check(ssex='男' or ssex='女')
sfav varchar2(500),
sbirth date,
sqq varchar2(30) --unique
--constraints pk_student_sno primary key(sno)--添加主键约束
--constraints ck_student_sname check(sname is not null)--非空约束
--constraints ck_student_sage check(sage<150 and sage>0)--检查约束
--constraints ck_student_ssex check(ssex='男' or ssex='女')--检查约束
--constraints un_student_sqq unique(sqq)--唯一约束
)
1、添加主键约束
alter table student add constraints pk_student_sno primary key(sno);
alter table student drop constraints pk_student_sno;
1)直接在创建表的字段后使用 primary key
2)在创建表的语句的最后面使用 constraints pk_表名_字段名 primary key(字段名)
3)在创建表后使用 alter table 表名 add constraints pk_表名_字段名 primary key(字段名);
删除主键 alter table student drop constraints 主键的约束名;
2、添加非空约束
alter table student add constraints ck_student_sname check(sname is not null);
alter table student drop constraints ck_student_sname;
2)在创建表的语句的最后面使用 constraints ck_表名_字段名 check(字段名 is not null)
3)在创建表后使用 alter table 表名 add constraints ck_表名_字段名 check(字段名 is not null);
删除非空约束 alter table student drop constraints 非空约束名;
3、添加检查约束
alter table student add constraints ck_student_sage check(sage<150 and sage>0)
alter table student drop constraints ck_student_sage;
添加检查约束校验性别
alter table student add constraints ck_student_ssex check(ssex='男' or ssex='女')
alter table student drop constraints ck_student_ssex;
2)在创建表的语句的最后面使用 constraints ck_表名_字段名 check(条件)
3)在创建表后使用 alter table 表名 add constraints ck_表名_字段名 check(条件);
删除检查约束 alter table student drop constraints 检查约束名;
4、添加唯一约束
alter table student add constraints un_student_sqq unique(sqq)
alter table student drop constraints un_student_sqq;
1)直接在创建表的字段后使用 unique
2)在创建表的语句后面使用 constraints un_表名_字段名 unique(字段名);
3)在创建表后使用 alter table 表名 add constraints un_表名_字段名 unique(字段名);
删除约束:alter table 表名 drop constraints 唯一约束名;
5、 外键约束
create table student(
sno number(10) primary key,
sname varchar2(100) not null,
sage number(3) check(sage>0 and sage<150),
ssex char(4) check(ssex='男' or ssex='女'),
sfav varchar2(500),
sqq varchar2(30) unique,
cid number(10) --references clazz(cno)
--constraints fk_student_cid foreign key(cid) references clazz(cno)--外键
)
添加外键
alter table student add constraints fk_student_cid foreign key(cid) references clazz(cno) on delete set null
alter table student drop constraints fk_student_cid
创建班级表
create table clazz(
cno number(10) primary key,
cname varchar2(100) not null,
cdesc varchar2(300)
)
使用外键:
作用:当在子表中插入的数据在父表中不存在,则会自动报错。
概念:当一张表的某个字段的值需要依赖另外一张表的某个字段的值,则使用外键约束。
其中主动依赖的表称为子表,被依赖的表称为父表。外键加在子表中。
使用:
1)在子表中的字段后直接使用 references 父表名(字段) 例如: cid number(10) references clazz(cno)
2)在创建表语句的最后面使用 constraints fk_子表名_字段名 foreign key(字段名) references 父表名(字段名)
3)在创建表后使用:alter table 表名 add constraints fk_子表名_字段名 foreign key(字段名) references 父表名(字段名)
删除外键:alter table 表名 drop constraints 外键约束名
外键选取:
一般选取父表的主键作为子表的外键。
外键的缺点:
无法直接删除父表数据,除非级联删除
级联删除:在添加外键约束时,使用关键字 on delete cascade
使用:当删除父表数据时,自动删除子表相关所有数据。
缺点:无法保留子表历史数据。
使用关键字 on delete set null
删除父表数据时,将子表中的依赖字段的值设置为null。
注意:子表依赖字段不能添加非空约束。
二维表的维护
alter table 表名 add 字段名 类型
alter table student add sphone number(11)--在学生表中添加新的字段
修改原有字段
修改字段类型
alter table 表名 modify 字段名 新的类型
alter table student modify sphone varchar2(11)
修改字段名
alter table 表名 rename column 字段名 to 新的字段名
alter table student rename column sphone to phone
删除字段
alter table 表名 drop column 字段名
alter table student drop column phone
修改表名
rename 原有表名 to 新的表名
rename student to student2
删除表
drop table 表名
drop table student
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 单元测试从入门到精通