---
title: "oracle表的创建"
author: "junner"
date: "2014-11-26T21:41:21Z"
tags: []
categories: ["编程语言"]
slug: "oracle"
draft: false
lastmod: "2014-11-26T21:41:21Z"
id: 2161043
url: "https://junner.iteye.com/blog/2161043"
---

--创建表空间  
create tablespace jt16  
datafile 'C:\\oracle\\oradata\\orcl\\jt16.dbf'  
size 100M  
autoextend on;

--创建用户  
create user yl identified by accp  
default tablespace jt16;

--给用户授权  
grant connect,resource to yl;  
\--收回权限  
revoke connect from yl;

--创建表  
/\*create table 表名(  
列名  列的数据类型 列的特征  
)\*/

create table student(  
stuid number(4) not null,  
stuname varchar2(50) not null,  
stusex char(2) not null,  
stuage number(3) not null,  
stuaddress nvarchar2(100),  
stuqq number(11),  
stuemail varchar2(100)  
);

--创建课程表  
create table course(  
cid number(1) primary key,  
cname varchar2(50) not null  
);

--添加一列  
\--修改表的结构用 alter table 表名 add()  
alter table student add(stuphone varchar2(13));  
\--修改列  
alter table student modify(stusex varchar2(4) default '男');  
\--删除列  
alter table student drop column stuphone;  
\--删除多列  
alter table student drop (stuemail,stuqq);  
\--给学生表添加一个课程列  
alter table student add(cid number(1));

--删除表的语法  
drop table student;

--为表添加约束  
\--为学生编号列添加主键约束  
alter table student add constraint pk\_stuid primary key(stuid);  
\--给性别加一个检查约束  
alter table student add constraint ck\_stusex check(stusex='男' or stusex='女');  
\--给QQ号加一个唯一约束  
alter table student add constraint uq\_stuqq unique(stuqq);  
\--给学生的课程列加一个外键  
alter table student add constraint fk\_cid foreign key(cid) references course(cid);  
\--删除约束  
alter table student drop constraint fk\_cid;