您的位置:首页 > 数据库

数据库的学习-主表操作

2017-08-11 10:19 211 查看
--create tablespace [表空间的名字]

--datafile '[数据文件的路径]'

--size nM  大小
--autoextend on next nM 满了后选着扩充的大小

创建用户  web  密码123456 对应表空间web

create user web  identfied by 123456 default  tablespace web

赋予 web数据管理员权限 该用户可以操作数据库

grant dba to web

 

--数字

number --代表整型和浮点型

number(p,s)  p代表整体长度,s代表小数的精度

number(5,2) 整数部分为3,小数部分为2

int 实际上是number的子集

number(10,0) 这样就代表的是一个整数

--字符

--定长字符

char 

char(10) 代表创建一个定长字符,最多只能存放10个字符,如果不到10个,那么整个定长字符所占空间还是10个

--非定长字符

--unicode非定长字符

nvarchar

nvarchar2

--非unicode非定长字符

varchar

varchar2

--日期

date 2017-08-10

datetime 2017-08-10 09:41:13

Timestamp 时间戳 20170810094113

text 大文本

clob char large object 

blob binary

create table student  表格式

(

stu_id int,

stu_name varchar(20),

stu_age number(3,0),

stu_gender char(2),

stu_birthday date,

clazz_id int

)

--添加主键

alter table student add constraint pk_stu_id primary key(stu_id)

索引 聚集索引

insert into student(stu_id,stu_name,stu_age,stu_gender,stu_birthday,clazz_id)

values(1,'云妹',22,'男',to_date('1995-08-10','yyyy-MM-dd'),1)

drop table student  删除表

添加表clazz 和其内容

create table clazz

(

clazz_id int,

clazz_name varchar(20)

)

alter table clazz add constraint pk_clazz_id primary key(clazz_id)

insert into clazz(clazz_id,clazz_name) values(1,'J0626')

--外键约束

alter table student add constraint fk_clazz_id foreign key(clazz_id) references clazz(clazz_id)

--级联置空

alter table student add constraint fk_clazz_id foreign key(clazz_id) references clazz(clazz_id) on delete set null

--级联删除

alter table student add constraint fk_clazz_id foreign key(clazz_id) references clazz(clazz_id) on delete cascade

delete from student where stu_id=2

--oracle 从删库到跑路

truncate --彻底删除

delete from clazz where clazz_id=1 单项删除

--check约束    不能添加与约束违背的内容

alter table student add constraint ck_stu_gender check (stu_gender in('男','女'))

alter table student add constraint ck_stu_age check (stu_age <120)

--unique约束 唯一

alter table student add constraint uk_stu_name unique(stu_name)

--非空约束 not null

alter table student modify stu_birthday constraint nt_stu_birthday not null

--修改表名

rename students to student

--删除约束

--删除非空约束

alter table student drop constraint nt_stu_birthday

--删除unique约束

alter table student drop constraint uk_stu_name

--删除check约束

alter table student drop constraint ck_stu_gender

--删除主键约束

alter table student drop constraint pk_stu_id
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: