SQL陳述句介紹
- 資料定義語言(DDL),包括 CREATE、 ALTER、 DROP等,
- 資料操縱語言(DML),包括 INSERT、 UPDATE、 DELETE、 SELECT … FOR UPDATE等,
- 資料查詢語言(DQL),包括基本查詢陳述句、 Order By 子句、 Group By 子句等,
- 事務控制語言(TCL),包括 COMMIT、 SAVEPOINT、ROLLBACK,
- 資料控制語言(DCL), GRANT、 REVOKE,
欄位型別
VARCHAR2(length)
字串型別:存盤可變的長度的字串,length是字串的最大長度,默認是1,最大不超過4000,
CHAR(length)
字串型別:存盤固定長度的字串,length字串的固定長度,默認是1,最大不超過2000,
NUMBER(a,b)
數值型別:存盤數值型別,可以存整數和浮點型,a代表數值的最大位數,包含小數位和小數點;b代表小數的位數,例子:
number(6,2),輸入123.12345,實際存入:123.12 ,
number(4,2),輸入12312.345,提示不能存入,超過存盤的指定的精度,
DATA
時間型別:存盤的是日期和時間,包括年、月、日、時、分、秒,
內置函式sysdate獲取的就是DATA型別,
TIMESTAMP
時間型別:存盤的不僅是日期和時間,還包含了時區,
內置函式systimestamp獲取的就是timestamp型別,
CLOB
大欄位型別:存盤大文本,大于4000長度的字串,
BLOB
二進制型別:存盤的是二進制物件,比如圖片、視頻、聲音等轉換過來的二進制物件,
創建表
-- stuinfo學生資訊表
create table STUDENT.stuinfo
(
stuid varchar2(11) not null,--學號
stuname varchar2(50) not null,--學生姓名
sex char(1) not null, --性別
age number(2) not null, --年齡
classno varchar2(7) not null, --班號
stuaddress varchar2(100) default '未錄入', --地址
grade char(4) not null, --年級
enroldate date, --入學時間
idnumber varchar2(18) default '身份證未采集' not null --身份證
)
-- stuinfo存盤的表空間是users
-- storage表示存盤引數
-- initial表示區段(extent)一次擴展64k
-- minextents最小區段數為1
-- maxextents最大的區段數不限制
tablespace USERS
storage
(
initial 64K
minextents 1
maxextents unlimited
);
-- Add comments to the table
comment on table STUDENT.stuinfo
is '學生資訊表';
-- Add comments to the columns
comment on column STUDENT.stuinfo.stuid
is '學號';
comment on column STUDENT.stuinfo.stuname
is '學生姓名';
comment on column STUDENT.stuinfo.sex
is '學生性別';
comment on column STUDENT.stuinfo.age
is '學生年齡';
comment on column STUDENT.stuinfo.classno
is '學生班級號';
comment on column STUDENT.stuinfo.stuaddress
is '學生住址';
comment on column STUDENT.stuinfo.grade
is '年級';
comment on column STUDENT.stuinfo.enroldate
is '入學時間';
comment on column STUDENT.stuinfo.idnumber
is '身份證號';
添加約束
-- 創建/重建主鍵索引、唯一索引、外鍵索引
-- 把 stuid 設為主鍵,主鍵欄位的資料必須是唯一性的(學號是唯一的)
alter table STUDENT.STUINFO
add constraint pk_stuinfo_stuid primary key (STUID);
-- 創建/重建檢查約束
-- 年齡age添加約束,學生的年齡只能0-50歲之間
alter table STUDENT.STUINFO
add constraint ch_stuinfo_age check (age>0 and age<=50);
alter table STUDENT.STUINFO
add constraint ch_stuinfo_sex
check (sex='1' or sex='2');
alter table STUDENT.STUINFO
add constraint ch_stuinfo_GRADE
check (grade>='1900' and grade<='2999');
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/14916.html
標籤:Oracle
上一篇:ORACLE資料庫逐步解決ORA-12541、ORA-01034和ORA-27101、ORA-00119和ORA00132的程序
