數據庫 →→表設計
一、設計表
設計表首先應該按需遵循三範式
1、確定表名
2、確定字段名 類型 +約束(主鍵 外鍵 非空 默 檢查認 唯一)
主鍵: 唯一標識一條記錄(唯一併且非空)
唯一: 唯一
非空:不能爲空
默認: 當沒給值時使用給定一個默認值
外鍵:參考其他表(自己)的某個(某些)字段
檢查:自定義的規則
用戶表
表名 tb_user
主鍵 userid
字段名 中文 類型 爲空 默認值 其他說明
userid 流水號 number(5) 否 主鍵
username 用戶名 varchar2(30) 否 長度在4-20
userpwd 密碼 varchar2(20) 否 長度在4-18
age 年齡 number(3) 18 大於>=18
gender 性別 char(2) 男 男or 女
email 郵箱 varchar2(30) 唯一
regtime 註冊日期 date sysdate
文章表
表名 tb_txt
主鍵 txtid
字段名 中文 類型 爲空 默認值 其他說明
txtid 流水號 number(10) 否 主鍵
title 標題 varchar2(32) 否 長度在4-30
txt 正文 varchar2(1024)
pubtime 發佈時間 date sysdate
userid 發佈人 number(5) 外鍵,參考tb_user的userid列
二、創建表
表名必須唯一,如果存在 ,必須刪除
drop table 表名;
1、創建表(不加約束)
1)、創建新表
create table 表名(
字段名 類型(長度) 約束,
...其他字段....
..約束........
);
表名 tb_user
主鍵 userid
字段名 中文 類型 爲空 默認值 其他說明
userid 流水號 number(5) 否 主鍵
username 用戶名 varchar2(30) 否 長度在4-20
userpwd 密碼 varchar2(20) 否 長度在4-18
age 年齡 number(3) 18 大於>=18
gender 性別 char(2) 男 男or 女
email 郵箱 varchar2(30) 唯一
regtime 註冊日期 date sysdate
create table tb_user(
userid number(5),
username varchar2(30),
userpwd varchar2(20),
age number(3) ,
gender char(2) ,
email varchar2(30),
regtime date
);
--加入註釋
comment on table tb_user is '用戶表';
comment on column tb_user.userid is '流水號,主鍵';
comment on column tb_user.username is '用戶名';
comment on column tb_user.userpwd is '密碼';
comment on column tb_user.age is '年齡';
comment on column tb_user.gender is '性別';
comment on column tb_user.email is '郵箱';
comment on column tb_user.regtime is '註冊日期';
表名 tb_txt
主鍵 txtid
字段名 中文 類型 爲空 默認值 其他說明
txtid 流水號 number(10) 否 主鍵
title 標題 varchar2(32) 否 長度在4-30
txt 正文 varchar2(1024)
pubtime 發佈時間 date sysdate
userid 發佈人 number(5) 外鍵,參考tb_user的userid列
create table tb_txt(
txtid number(10),
title varchar2(32),
txt varchar2(1024),
pubtime date,
userid number(5)
);
--註釋
comment on table tb_txt is '文章表';
comment on column tb_txt.txtid is '流水號,主鍵';
comment on column tb_txt.title is '標題';
comment on column tb_txt.txt is '正文';
comment on column tb_txt.pubtime is '發佈時間';
comment on column tb_txt.userid is '發佈人,外鍵,參考 tb_user 的 userid 列';
--刪除 (先刪除從表 再刪除主表 ;同時刪除約束)
drop table tb_txt cascade constraints;
drop table tb_user cascade constraints;
2)、已有表中拷貝結構(較爲重要)
create table 表名 as select 字段列表 from 已有表 where 1!=1;
--拷貝結構 emp
create table emp_his as select ename,sal from emp where 1!=1;--作爲一個恆不等值,可以只獲得結構不要數據
--拷貝結構 emp +數據
create table emp_his2 as select ename,sal from emp where sal>2000;
3)、刪除表
drop table 表名 cascade constraints
--刪除表
drop table emp_his2 cascade constraints;
drop table emp_his cascade constraints;--級聯刪除
2、創建表(同時創建約束+默認名稱)
這種在創建表的同時創建約束並使用默認約束名稱的方式,後期不方便排錯, 所以不推薦使用。其主要的優點是簡單。
--刪除 (先刪除從表 再刪除主表 ;同時刪除約束)
drop table tb_txt cascade constraints;
drop table tb_user cascade constraints;
表名 tb_user
主鍵 userid
字段名 中文 類型 爲空 默認值 其他說明
userid 流水號 number(5) 否 主鍵
username 用戶名 varchar2(30) 否 長度在4-20
userpwd 密碼 varchar2(20) 否 長度在4-18
age 年齡 number(3) 18 大於>=18
gender 性別 char(2) 男 男or 女
email 郵箱 varchar2(30) 唯一
regtime 註冊日期 date sysdate
create table tb_user(
userid number(5) primary key,
username varchar2(30) check(length(username) between 4 and 20) not null ,
userpwd varchar2(20) not null check(length(userpwd) between 4 and 18),
age number(3) default(18) check(age>=18),
gender char(2) default('男') check(gender in('男','女')),
email varchar2(30) unique,
regtime date default(sysdate)
);
--加入註釋
comment on table tb_user is '用戶表';
comment on column tb_user.userid is '流水號,主鍵';
comment on column tb_user.username is '用戶名';
comment on column tb_user.userpwd is '密碼';
comment on column tb_user.age is '年齡';
comment on column tb_user.gender is '性別';
comment on column tb_user.email is '郵箱';
comment on column tb_user.regtime is '註冊日期';
表名 tb_txt
主鍵 txtid
字段名 中文 類型 爲空 默認值 其他說明
txtid 流水號 number(10) 否 主鍵
title 標題 varchar2(32) 否 長度在4-30
txt 正文 varchar2(1024)
pubtime 發佈時間 date sysdate
userid 發佈人 number(5) 外鍵,參考tb_user的userid列
create table tb_txt(
txtid number(10) primary key,
title varchar2(32) not null check(length(title)>=4 and length(title)<=30),
txt varchar2(1024),
pubtime date default(sysdate),
userid number(5) references tb_user(userid) on delete set null
);
--註釋
comment on table tb_txt is '文章表';
comment on column tb_txt.txtid is '流水號,主鍵';
comment on column tb_txt.title is '標題';
comment on column tb_txt.txt is '正文';
comment on column tb_txt.pubtime is '發佈時間';
comment on column tb_txt.userid is '發佈人,外鍵,參考tb_user的userid列';
--刪除 (先刪除從表 再刪除主表 ;同時刪除約束)
drop table tb_txt cascade constraints;
drop table tb_user cascade constraints;
3、創建表(同時創建約束+指定名稱)
創建表的同時創建約束並指定約束的名稱,後期方便排錯,推薦使用
--刪除 (先刪除從表 再刪除主表 ;同時刪除約束)
drop table tb_txt cascade constraints;
drop table tb_user cascade constraints;
表名 tb_user
主鍵 userid
字段名 中文 類型 爲空 默認值 其他說明
userid 流水號 number(5) 否 主鍵
username 用戶名 varchar2(30) 否 長度在4-20
userpwd 密碼 varchar2(20) 否 長度在4-18
age 年齡 number(3) 18 大於>=18
gender 性別 char(2) 男 男or 女
email 郵箱 varchar2(30) 唯一
regtime 註冊日期 date sysdate
create table tb_user(
userid number(5),
username varchar2(30) constraint nn_user_name not null ,
userpwd varchar2(20) constraint nn_user_pwd not null ,
age number(3) default(18) ,
gender char(2) default('男'),
email varchar2(30),
regtime date default(sysdate),
constraint pk_user_id primary key (userid),
constraint ck_user_name check(length(username)between 4 and 20) ,
constraint ck_user_pwd check(length(userpwd) between 4 and 18),
constraint ck_user_age check(age>=18),
constraint ck_user_gender check(gender in('男','女')),
constraint uq_user_email unique(email)
);
--加入註釋
comment on table tb_user is '用戶表';
comment on column tb_user.userid is '流水號,主鍵';
comment on column tb_user.username is '用戶名';
comment on column tb_user.userpwd is '密碼';
comment on column tb_user.age is '年齡';
comment on column tb_user.gender is '性別';
comment on column tb_user.email is '郵箱';
comment on column tb_user.regtime is '註冊日期';
表名 tb_txt
主鍵 txtid
字段名 中文 類型 爲空 默認值 其他說明
txtid 流水號 number(10) 否 主鍵
title 標題 varchar2(32) 否 長度在4-30
txt 正文 varchar2(1024)
pubtime 發佈時間 date sysdate
userid 發佈人 number(5) 外鍵,參考tb_user的userid列
create table tb_txt(
txtid number(10),
title varchar2(32) constraint nn_txt_title not null,
txt varchar2(1024),
pubtime date default(sysdate),
userid number(5) ,
constraint pk_txt_id primary key(txtid),
constraint ck_txt_id check(length(title)>=4 and length(title)<=30),
constraint fk_txt_ref_user_id foreign key(userid) references tb_user(userid) on delete cascade
);
--註釋
comment on table tb_txt is '文章表';
comment on column tb_txt.txtid is '流水號,主鍵';
comment on column tb_txt.title is '標題';
comment on column tb_txt.txt is '正文';
comment on column tb_txt.pubtime is '發佈時間';
comment on column tb_txt.userid is '發佈人,外鍵,參考tb_user的userid列';
--刪除 (先刪除從表 再刪除主表 ;同時刪除約束)
drop table tb_txt cascade constraints;
drop table tb_user cascade constraints;
4、創建表(追加創建約束+指定名稱)
推薦, 便於後期排錯
--刪除 (先刪除從表 再刪除主表 ;同時刪除約束)
drop table tb_txt cascade constraints;
drop table tb_user cascade constraints;
表名 tb_user
主鍵 userid
字段名 中文 類型 爲空 默認值 其他說明
userid 流水號 number(5) 否 主鍵
username 用戶名 varchar2(30) 否 長度在4-20
userpwd 密碼 varchar2(20) 否 長度在4-18
age 年齡 number(3) 18 大於>=18
gender 性別 char(2) 男 男or 女
email 郵箱 varchar2(30) 唯一
regtime 註冊日期 date sysdate
create table tb_user(
userid number(5),
username varchar2(30) ,
userpwd varchar2(20) ,
age number(3) ,
gender char(2) ,
email varchar2(30),
regtime date default(sysdate)
);
--追加約束
alter table tb_user add constraint pk_user_id primary key (userid);
alter table tb_user add constraint ck_user_name check(length(username)between 4 and 20) ;
alter table tb_user add constraint ck_user_pwd check(length(userpwd) between 4 and 18);
alter table tb_user add constraint ck_user_age check(age>=18);
alter table tb_user add constraint ck_user_gender check(gender in('男','女'));
alter table tb_user add constraint uq_user_email unique(email);
--非空與默認
alter table tb_user modify (username constraint nn_user_name not null);
alter table tb_user modify (userpwd constraint nn_user_pwd not null);
alter table tb_user modify (age default(18));
alter table tb_user modify (gender default('男'));
--加入註釋
comment on table tb_user is '用戶表';
comment on column tb_user.userid is '流水號,主鍵';
comment on column tb_user.username is '用戶名';
comment on column tb_user.userpwd is '密碼';
comment on column tb_user.age is '年齡';
comment on column tb_user.gender is '性別';
comment on column tb_user.email is '郵箱';
comment on column tb_user.regtime is '註冊日期';
表名 tb_txt
主鍵 txtid
字段名 中文 類型 爲空 默認值 其他說明
txtid 流水號 number(10) 否 主鍵
title 標題 varchar2(32) 否 長度在4-30
txt 正文 varchar2(1024)
pubtime 發佈時間 date sysdate
userid 發佈人 number(5) 外鍵,參考tb_user的userid列
create table tb_txt(
txtid number(10),
title varchar2(32),
txt varchar2(1024),
pubtime date,
userid number(5)
);
--追加約束
alter table tb_txt add constraint pk_txt_id primary key(txtid);
alter table tb_txt add constraint ck_txt_id check(length(title)>=4 and length(title)<=30);
--三種級聯刪除規則
alter table tb_txt add constraint fk_txt_ref_user_id foreign key(userid) references tb_user(userid);
alter table tb_txt add constraint fk_txt_ref_user_id foreign key(userid) references tb_user(userid) on
delete cascade ;
alter table tb_txt add constraint fk_txt_ref_user_id foreign key(userid) references tb_user(userid) on
delete set null;
--注意非空 默認
alter table tb_txt modify (title constraint nn_txt_title not null) ;
alter table tb_txt modify (pubtime default(sysdate));
--註釋
comment on table tb_txt is '文章表';
comment on column tb_txt.txtid is '流水號,主鍵';
comment on column tb_txt.title is '標題';
comment on column tb_txt.txt is '正文';
comment on column tb_txt.pubtime is '發佈時間';
comment on column tb_txt.userid is '發佈人,外鍵,參考tb_user的userid列';
--刪除 (先刪除從表 再刪除主表 ;同時刪除約束)
drop table tb_txt cascade constraints;
drop table tb_user cascade constraints;
三、約束(瞭解)
在 oracle 中所有的一切都是對象, 約束也是一個個的對象,除了能創建約束我們還能對約束進行一些其他的操作
1、查看某個用戶的約束(很少會用到)
select constraint_name, constraint_type
from user_constraints
where owner = upper('scott');
2、查看錶的約束
select constraint_name,constraint_type
from user_constraints
where table_name=upper('tb_user');
3、查看 字段名+約束
select constraint_name, column_name
from user_cons_columns
where table_name = upper('tb_user');
4、約束的禁用與啓用
ALTER TABLE tb_user disable constraint nn_user_name;
ALTER TABLE tb_user enable constraint nn_user_name;
5、刪除約束
alter table tb_user drop constraint uq_user_email cascade;
6、修改約束
--非空
alter table tb_user modify (username varchar2(20));
--默認
alter table tb_user modify (age default null);
四、表的其他操作
1、修改表結構
1、修改表名 :rename to
--修改表名
rename tb_txt to tb_txt_new;
2、修改列名: alter table 表名 rename column to
--修改列名
alter table tb_txt_new rename column txtid to tid;
3、修改類型: alter table 表名 modify(字段 類型)
--修改類型
alter table tb_txt_new modify(tid varchar2(20));
4、修改約束: 先刪除 後添加
5、添加列: alter table 表名 add 字段 類型
--添加列
alter table tb_txt_new add col varchar2(30);
6、刪除列:alter table 表名 drop column 字段
--刪除列
alter table tb_txt_new drop column col;
select * from tb_txt_new;
2、刪除表
1、先刪除從表 再刪除主表
2、刪除表的同時刪除約束
drop table tb_txt_new cascade constraints;
drop table tb_user cascade constraints;
3、截斷數據
truncate: 截斷所有的數據 ,如果截斷的是主表,結構不能存在 外鍵關聯
截斷數據同時 從結構上 檢查
create table emp_his as select * from emp where 1=1;
select * from emp_his;
–截斷所有的數據
truncate table emp_his;
–不能截斷: truncate table dept;
五、序列
使用工具|程序管理流水號,序列在創建時 沒有與表關聯 ,在操作數據時 與表關聯
1、創建
create sequence 序列名 start with 起始值 increment by 步進;
2、使用
在操作數據 添加 更新 -->主鍵
1)、currval :當前值
2)、nextval:下個值
create sequence seq_tb_user start with 2 increment by 2;
drop sequence seq_tb_user;
select seq_tb_user.nextval from dual;
select seq_tb_user.currval from dual
3、刪除
drop sequence 序列名