Oracle系列:(17)创建表和约束
回顾MySQL创建表语句users(id整型/name字符串/birthday日期型,默认值) drop table if exists users;create table users( id int(5) auto_increment primary key, name varchar(4) not null, birthday date default '2015-4-27'); |
使用oracleSQL,创建用户表users(id整型/name字符串/birthday日期/sal整型,默认今天)
create table users( id number(5) primary key, name varchar2(8) not null unique, sal number(6,2) not null, birthday date default sysdate);
进入回收站
drop table users;
查询回收站中的对象
show recyclebin;
闪回,即将回收站还原
flashback table 表名 to before drop;flashback table 表名 to before drop rename to 新表名;
彻底删除users表
drop table users purge;
清空回收站
purge recyclebin;
测试如下类型
(1)number(5):
insert into users(id,name,sal) values(1,'A',6666.66);
insert into users(id,name,sal) values(11,'AA',6666.66);
insert into users(id,name,sal) values(111,'AAA',6666.66);
insert into users(id,name,sal) values(1111,'AAAA',6666.66);
insert into users(id,name,sal) values(99999,'AAAAA',6666.66);
insert into users(id,name,sal) values(100000,'AAAAAA',6666.66); 错
5表示最多存99999
(2)number(6,2):
col sal for 9999.99
insert into users(id,name,sal) values(1,'A',6.66);
insert into users(id,name,sal) values(11,'AA',66.666);
insert into users(id,name,sal) values(111,'AAA',666.6666);
insert into users(id,name,sal) values(1111,'AAAA',6666.66666);
insert into users(id,name,sal) values(11111,'AAAAA',66666.666666);错
number(6,2)
其中2表示最多显示2位小数,采用四舍五入,不足位数补0,同时要设置col ... for ...
其中6表示小数+整数不多于6位
其中整数位数不得多于4位,可以等于4位
(3)varchar2(8):
insert into users(id,name,sal) values(1,'A',7777.77);
insert into users(id,name,sal) values(2,'AA',7777.77);
insert into users(id,name,sal) values(3,'AAA',7777.77);
insert into users(id,name,sal) values(4,'AAAA',7777.77);
insert into users(id,name,sal) values(5,'AAAAA',7777.77);
insert into users(id,name,sal) values(6,'AAAAAA',7777.77);
insert into users(id,name,sal) values(7,'AAAAAAA',7777.77);
insert into users(id,name,sal) values(8,'AAAAAAAA',7777.77);
insert into users(id,name,sal) values(9,'AAAAAAAAA',7777.77);错
insert into users(id,name,sal) values(1,'哈',7777.77);
insert into users(id,name,sal) values(2,'哈哈',7777.77);
insert into users(id,name,sal) values(3,'哈哈哈',7777.77);
insert into users(id,name,sal) values(4,'哈哈哈哈',7777.77);
insert into users(id,name,sal) values(5,'哈哈哈哈哈',7777.77);错
varchar2(8)
8表示字节
GBK 一个汉字 2字节
UTF-8 一个汉字 3字节
(4)date:默认格式为:'27-4月-15'
(5)CLOB【Character Large OBject】:大文本对象,即超过65565字节的数据对象,最多存储4G
(6)BLOB【Binary Large OBject】:大二进制对象,即图片,音频,视频,最多存储4G
为users表增加p_w_picpath列,alter table 表名 add 列名 类型(宽度)
alter table usersadd p_w_picpath blob;
修改name列的长度为20个字节,alter table 表名 modify 列名 类型(宽度)
alter table usersmodify name varchar2(20);
删除p_w_picpath列,alter table 表名 drop column 列名
alter table usersdrop column p_w_picpath;
重名列名name为username,alter table 表名 rename column 原列名 to 新列名
alter table usersrename column name to username;
将users表重命名myusers,rename 原表名 to 新表名
rename users to myusers;
注意:修改表时,不会影响表中原有的数据
笔试题:有【1000亿】条会员记录,如何用最高效的方式将薪水字段清零,其它字段内容不变?
第一:从emp表中删除sal字段
alter table emp drop column sal;
第二:向emp表中添加sal字段,且内容默认0
alter table emp add sal number(6) default 0;
修改表不可回滚
创建表customers(单)和orders(多),使用primary key/not null/unique/default/foreign key约束
要体现【on delete cascade/on delete set null】
需求:删除客户,级联删除他所有的订单
delete from customers where id = 1;
需求:删除客户,不级联删除他所有的订单,只是将外健设置为NULL
delete from customers where id = 1;
create table customers( id number(3) primary key, name varchar2(4) not null unique);insert into customers(id,name) values(1,'A');insert into customers(id,name) values(2,'B');
create table orders( id number(3) primary key, isbn varchar2(6) not null unique, price number(3) not null, cid number(3), --constraint cid_FK foreign key(cid) references customers(id) on delete cascade constraint cid_FK foreign key(cid) references customers(id) on delete set null );insert into orders(id,isbn,price,cid) values(1,'isbn10',10,1);insert into orders(id,isbn,price,cid) values(2,'isbn20',20,1);insert into orders(id,isbn,price,cid) values(3,'isbn30',30,2);insert into orders(id,isbn,price,cid) values(4,'isbn40',40,2);
创建表students,包括id,name,gender,salary字段,使用check约束【性别只能是男或女,薪水介于6000到8000之间】
create table students( id number(3) primary key, name varchar2(4) not null unique, gender varchar2(2) not null check ( gender in ('男','女') ), salary number(6) not null check ( salary between 6000 and 8000 ));
insert into students(id,name,gender,salary) values(1,'哈哈','中',6000);错
insert into students(id,name,gender,salary) values(2,'呵呵','男',5000);错
insert into students(id,name,gender,salary) values(3,'嘻嘻','女',7000);对