Orcale数据库操作

来源:互联网 发布:淘宝dota2饰品店 编辑:程序博客网 时间:2024/04/30 14:54

第二章:数据库操作:

第一节;Oracle 数据库常用指令:

创建用户: create  user  用户名 identified by 密码;

连接用户: conn 用户名/密码;

修改密码: alter user  用户名identified by 密码;

删除用户: drop user 用户名;

删除表单: drop 用户名.表单名;

分配权限: grant  connect to 用户名;

表单权限: grant  all  on表单名 to  用户名;

收回权限: revoke allon 表单名 from 用户名;

权限继承: grant 权限名 on 表单名 to 用户名 with grantoption;

锁定用户: create  profile abc  limit  failed  login attempts 3  password_lock_time 2;

解锁用户: alter user用户名 account unlock;

注释:常用单词:

declare;声明   Revoke:撤销  Profile:简  Account :解释 ,说明Unlock:解锁

第二节:Oracle 表的创建 \删除\修改\添加数据

表的创建:

create table 表的名(

no number(4),

name varchar2(20),

sex char(2),

salary number(7,2)

);

表的查询:

select * from表的名;

添加类:

alter table表单名 add (添加类名number (2));

修改字段的长度:

alter table 表单名 modify (字段名 varchar2(30));

删除一个字段:

alter table 表单名 drop column 删除的字段名;

修改表单名:

rename 原来的单名 to 新的表单名;

删除表单:

drop table 表单名;

查询表的结构:

SQL> desc 查询表的名字;

添加数据;

insert into 表单名 values ('001','李冰','男','001');

部分添加数据;

insert into 表单名(no,name,sex,classid)values ('001','李冰','男','001');

删除表的数据;

delete from 表单名 where no= '序号';

修改表单的数据;

update 表单名 set no ='001' ,name = '王华' where no ='001';

删除表的所有数据和表单结构;

delete from 表单名;

查询时间

set timing on;

第三节:Oracle 约束

约束包括:not null(非空约束)、unique(唯一约束)、primary key(主键)、foreign key(外键)、check

修改约束:

altertable 表单名 modify 数据的列名 not null;

altertable customer add constraint cardunique unique(cardId);

altertable customer add constraint addresscheck check(address in(‘东城’,’西城’));

查询表单的约束:

select* from user_constraints where table_name=upper('&_good');

删除约束:

altertable 表名 drop constraint 约束名称;

删除主键约束的时候,可能有错误,

altertable 表名 drop primary key cascade;

primarykey约束与unique约束的主要区别:

(1)一个表只能创建一个primary key约束,但可根据需要对不同的列创建若干个unique约束。

(2)primary key字段的值不允许为null,而unique字段的值可取null。

primarykey约束与unique约束的相同点:

两者均不允许表中对应字段存在重复值;在创建primary key约束与unique约束时会自动产生索引。

对于primary key约束与unique约束来说,都是由索引强制实现的。


第四节:代码复习

create table student(no number(4),name varchar2(20),sex char(2),salary number(7,2));-- 创建表
select * from student;--查询表alter table student add (classid number(2));-- 修改字段alter table student drop column salary;-- 添加字段alter table student modify (name varchar2(30));--修改字段的值rename student to stu;-- 修改表的名称select *from stu;rename stu to student;delete from student;drop table student;-- 删除表单
insert into  student values ('001','李冰','男','001');--全局插入insert into student (no,name,sex,classid) values ('003','王亏','男','003');-- 局部插入delete from student where no = '003';-- 删除表单数据update student set no = '004',  name ='团子' where no = '004';--修改表单数据
alter table student modify name not null;--添加约束alter table student modify classid check (classid>0);--添加约束insert into student (no,name,sex,classid) values ('004','null','男','004');--测试约束alter table student drop constraint name  ; --删除约束select * from user_constraints where table_name=upper('&_good');-- 查看表的约束条件