Oralce常用命令

 

用户管理

为scott用户解锁

1:解锁
  alter user scott account unlock;

2: 更改密码
   alter user scott identified by tiger;

3: 跳转
   connect sqlplus scott/tiger

4  查询测试OK
    select * from emp;

创建用户
create user "username" identified by "userpassword";


删除用户
drop user "username" cascade;

授权
grant connect,resource,dba to "username";

查看当前用户的角色
select * from user_role_privs;
select * from session_privs;

查看当前用户的系统权限和表级权限
select * from user_sys_privs;
select * from user_tab_privs;

查询用户表
select username from dba_users;

修改用户口令密码
alter user "username" identified by "password";

显示当前用户
show user;


表空间管理

创建表空间
create  tablespace data01 datafile 'd:/DATA01.DBF' size 10M;

drop tablespace data01 including contents and datafiles;

修改表空间大小
alter database datafile 'D:/DATA01.DBF' resize 5M;

增加表空间
alter tablespace newccs add datafile 'D:/DATA02.DBF' size 40M;

查看数据库文件
select * from dba_table_files;
 
查询当前存在的表空间
select * from v$tablespace;

表空间情况
select tablespace_name,sum(bytes)/1024/1024 from dba_data_files group by tablespace_name;

查询表空间剩余空间
select tablespace_name,sum(bytes)/1024/1024 from dba_free_space group by tablespace_name;

查看表结构
desc table;



约束管理

非空约束
创建非空约束
create table test(id number(6) not null);

添加非空约束
alter table 表名 modify 列名 not null

删除表中的非空约束
alter table 表名 modify 列名 null

主键约束 primary key

创建primary key 约束
 create table test (id number(6) primary key);

添加primary key 约束
 alter table 表名 add constraint 自定义约束名 primary key (列名)

删除约束

alter table 表名 drop constraint 约束名

唯一约束 unique

创建表时
create table users(
 id number(6) primary key not null,
 name varchar2(20) unique);
)

添加unique约束
alter table 表名 add unique(列名)


检查约束 check

创建表时
create table users(
  id number(6) primary key not null,
  sex varchar2(2) check(sex='男' or sex='女') default '男';
)

添加check约束
alter table users addsex varchar2(2) check(sex = '男' or sex = '女') default '男';

外键约束 foreign key

先创建一个班级表
create  table class(
  classId number(6) not null primary key,
  className varchar2(10),
  count number(100)
);

再创建一个学生表
create table student(
  studentId number(6) not null primary key,
  studentName varchar2(20),
  studentClass number(6) references class(classId)
);

添加外键约束
alter table student add constraint fk_student_class referenc class (classId);

级联删除约束
添加外键时在外键后面加
alter table student add constraint fk_student_class referenc class (classId) on delete cascade;

删除外键时添加级联删除约束
alter table 表名 drop primary key cascade;

查看表的所有约束以下两种方法都可以
select table_name,constraint_name,constraint_type from user_constraints
where table_name='大写的表名'

select table_name,constraint_name,constraint_type from dba_constraints
where table_name='大写的表名'

转载于:https://www.cnblogs.com/ZHANYU/p/3631344.html

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值