oracle 创建表空间、表、主键、外键、序列

来源:互联网 发布:安装mac磁盘被锁定 编辑:程序博客网 时间:2024/06/05 18:40

创建表空间
create tablespace projects
datafile 'D:\projects.dbf'
size 10m autoextend on;

--创建用户名
create user proj
identified by proj
default tablespace projects;

--分配系统权限和角色
grant connect to proj;
--
创键表和其它对象
grant resource to proj;

grant dba to proj;

--连接该用户
connect proj/proj;

--创建序列
create sequence U_seq
minvalue 1
maxvalue 10000
start with 1
increment by 1
cache 20;
--
创建表
create table Users

Id varchar2(10) not null primary key,
uName varchar2(20) not null,
uPwd varchar2(10) not null,
uSex varchar2(10) not null,
uEmail varchar2(20) not null,
uLike varchar2(20)
);
--
为表插入数据
insert into Users values(U_seq.nextval,'aa','123456','
','feiyan811@126.com','唱歌');
insert into Users values(U_seq.nextval,'bb','123456','
','feiyan811@126.com','篮球');

create sequence U_order
minvalue 1
maxvalue 10000
start with 1
increment by 1
cache 20;
--
创建表
create table Orders
(
id varchar(2) not null primary key,
uuid varchar2(10) not null,
oName varchar2(20) not null,
oPrice number not null,
oNum number not null
);
--
创建主外键关系
alter table Orders add constraint fk_Id foreign key (uuid) references Users(id);
--
创建约束
alter table Users add constraint ck_Upwd check(length(Upwd)=6);

插入数据
insert into Orders values(U_order.nextval,'1','oracle
书籍',98.88,1);
insert into Orders values(U_order.nextval,'2','java
书籍',128.88,1);

原创粉丝点击