Oracle数据库(约束、创建、管理)

约束,Oracle中都可以给约束取名

primary key 主键约束(组合主键:PK_表名)
–not null 非空(NN_表名_字段名)
–unique key 唯一性约束(UK_表名_字段名)
–foreign key(字段A) references 表B(字段A) 外键约束(FK_表A_表B_字段A)
–check 检查约束(CK_表名_字段名)
–check约束:检查约束,一般是指表中的一个列需要满足某个条件规划(比如:性别只能是‘男’或‘女’,而不能是其他)

–主键约束(组合主键:PK_表名)的操作

添加名为PK_student的组合主键,主键同时为stuid和stuname
	创表时添加组合主键:
	 create table student(
		   stuid varchar2(16),
		   stuname varchar2(16),
		   stusex varchar2(6)constraint PK_student primary key(stuid,stuname)
	);
修改表时添加组合主键:
alter table 表名 add constraint PK_student primary key(stuid,stuname);


删除主键约束:
alter table 表名 drop constraint PK_student;

–非空约束(NN_表名_字段名)的操作

创表时添加组合主键:
 create table student(
	   stuid varchar2(16),
	   stuname varchar2(16) constraint NN_表名_字段名 not null,
	   stusex varchar2(6)
);


修改表时添加非空约束:
alter table 表名 modify(字段名 not null);


删除非空约束:
①该约束没有名字:alter table 表名 modify(字段名 null);
②该约束有名字:alter table 表名 drop constraint 约束名(NN_表名_字段名);

–唯一性约束(UK_表名_字段名)的操作

创表时添加组合主键:
 ①create table student(
	   stuid varchar2(16),
	   stuname varchar2(16),
	   constraint UK_表名_字段名 unique(字段名)
);create table student(
	   stuid varchar2(16),
	   stuname varchar2(16) unique,
	   stusex varchar2(6)
);



修改表时添加唯一性约束:
alter table 表名 add constraint UK_表名_字段名 unique(字段名);


删除唯一约束:
alter table 表名 drop constraint UK_表名_字段名;

–外键约束(FK_表A_表B_字段A)的操作

创建表时添加外键
create table student(
	   stuid varchar2(16),
	   stuname varchar2(16),
	   stusex varchar2(6) foreign KEY(字段名A) references 参考表B(参考字段名A)
);

foreign KEY(字段名)
references 参考表名(参考字段名)


修改表时添加外键
alter table 表A add constraint FK_表A_表B_字段A foreign key(字段A) references 表B(字段A);
(此时,系统将表A中的字段A设为外键,参考表B中的字段A)


删除外键约束
alter table 表A drop constraint FK_表A_表B_字段A;

–check 检查约束(CK_表名_字段名)的操作

创建表时添加指定check约束
create table student(
	   stuid varchar2(16),
	   stuname varchar2(16),
	   stusex varchar2(6),
	   check(stusex in('男','女'))
);


修改表时添加指定check约束
alter table 表名 add constraint CK_表名_字段名 check(字段>=0 and 字段A<=100);


删除check约束
alter table 表A drop constraint CK_表名_字段名;

表的数据类型

①字符串类型
	 char(n)--查询速度更快,使用较少
	 varchar2(n)--更节省空间,使用较多
②数值类型
	 Int/Integer--整数
	 float--浮点数
③日期类型
	 Date

DDL数据库定义语言

create table student(
	   stuid varchar2(16) primary key,
	   stuname varchar2(16),
	   stusex varchar2(6),
	   stubir date
);
create table course(
	   courseid varchar2(16) primary key,
	   coursename varchar2(16)
);

create table teacher(
	   teacherid varchar2(16) primary key,
	   teachername varchar2(16),
	   teachersex varchar2(6),
	   teachertitle varchar2(16)
);
create table score(
	   stuid varchar2(16) references student(stuid),
	   courseid varchar2(16) references teacher(teacherid),
	   type varchar2(6),
	   score float
);
drop table student--删除表student


alter table student add(age number(2))--增加列age,类型为number(2)

alter table student drop (stubir,stuid);--删除列stubir和stuid

alter table student rename column stubir to age--修改列名stubir为age

alter table student modify (stubir number(4))--修改列stubir的类型为number(4)

DML数据操作语言

insert--添加数据
	insert into student values ('201701','李白','男','18');
	insert into student values ('201702','张三','女','18');
	insert into student values ('201703','李四','男','21');
	insert into student values ('201704','王三','女','20');
	insert into student values ('201705','王五','男','22');
	insert into student values ('201706','赵弄','女','19');
当有字段类型为日期(to_date是转换为日期的函数):
	insert into 表名(字段) values('01','赵弄',to_date('1999-1-21','YYYY-MM-DD'));
新增时表示空值:
    ①不填(''-----insert into student values ('201706','','女');
	②用null代替-----insert into student values ('201706',null,'女');
	
	
添加一些其他记录:
	insert into 表A select 字段1,字段2,'新字段名X' from 表B 
		where 字段1 in (
		   select 字段1 from 表C group by 字段1 having avg(字段名)>90
		   );
	
	

```sql
--delete:
--删除一个数据
	delete from student where stuid='201706';
	
--省略from
	delete student where stuid='201706';
	
--删除全部数据
	delete from student;
	truncate student;

--truncate不可以回滚;可以释放表存储空间;直接删除表中全部记录;不激发触发器,执行比delete快
--delete可以回滚;不能释放空间;可以删除部分记录;
--update:
	update student set age=age+1 where age='19';

–marge

TPL事务处理语言

–commit; 提交 保存到数据库里 不用每次都要手动点击按钮提交

–rollback; 回滚 回退上一步的数据操作 不用每次都要手动点击按钮回滚

–savepoint 事务保存点 如果事务很长,只想回滚事务的一部分,可借助于保存点进行回滚,语法为:

  修改语句 
  ...
  savepoint 保存点名称1
  修改语句 
  ...
  savepoint 保存点名称2
  修改语句 
  ...
  savepoint 保存点名称3
  rollback to 保存点名称2/保存点名称3
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值