oracle數據庫基本語法——表的約束 修改表結構 數據的增刪改查 表的權限 表空間

表的約束

1.建表
create table 表名(列名 數據類型 約束,列名 數據類型 約束…)
數據類型:
字符串:
varchar2(2)
nvarchar2(1)
char()定長,用來存長度固定的數據
nchar()存數據長度不固定的數據
數字
number(7,0)存7位整數
number(7,2)整數部分7位,小數2位
日期
date 只識別 “7-7月-2019”格式
大數據類型
blob 存長的電影數據類型,最多4g
clob 存大的文本類型

2.約束
保證數據的完整性:正確,有效
A.實體完整性
每行數據唯一的
B.域完整性
每列的數據都在一個有效的範圍
C.引用完整性
A B
B某一列數據參照A某一列
D.自定義完整性
對特殊場合的特殊要求
實體完整性:主鍵,唯一
域完整性: 檢查 非空 默認值 數據類型
引用完整性: 外鍵約束
自定義完整性: 觸發器 過程

主鍵約束 primary key
1.非空
2.唯一
3.一個表中只能有一個主鍵,但一個主鍵可以是多列組合

創建主鍵語法:
alter table 表名
add constraint pk_列名 primary key(列名)

唯一約束 unique
數據唯一
alter table 表名
add constraint uq_列名 unique(列名)
檢查約束 check
alter table 表名
add constraint ck_列名 check(條件表達式)
關係運算符 > < >= <= = != <>
邏輯運算符 and or not
空值 is null
非空 is not null
範圍: between … and
等值範圍 in
like 模糊匹配
通配符: % 匹配任意長度的一段任意字符 %.jpg
_ 匹配一個任意字符 張_
例如 name like '張_%'表示張後面至少有一個字
默認值約束: default
alter table 表名 modify 列名 default ‘值’
外鍵: foreign key
主表建主鍵,子表建外鍵
alter table 表名 add constraint fk_列名 foreign key(外鍵列名) references 主表名(主鍵列名)
添加數據時,先加主表,再加子表
刪除數據時,先刪子表,再刪主表

eggs:

 -- 建表
 create table student
 (
     stuid number(10) not null,
     stuname varchar2(20) not null,
     gender varchar2(2) not null,
     phone varchar2(11) null
 )
  
 create table exam
 (
     stuno number(10) not null,
     subject varchar(30) not null,
     score number(3) not null,
     examdate date not null
 )
 
 
 -- 添加主鍵
 alter table student
  add constraint pk_stuid primary key(stuid);
  
  -- 唯一約束
  alter table student
   add constraint uq_phone unique(phone)
  
  -- 檢查約束
   alter table student
    add constraint ck_gender check(gender='男' or gender='女')
    
 -- 性別默認男
    alter table student modify gender default '男'
 --  刪除默認值
    alter table student modify gender default null

修改表結構

1.約束
刪除:
alter table 表名 drop constraint 約束名;
查看當前用戶所有的約束
select * from user_constraints;
查看某個表中的約束
select * from user_constraints where table_name=’ ';
2.修改表結構
查看錶結構
desc student
增加刪除列
alter table 表名 add 列名 數據類型;
alter table 表名 drop column 列名;
修改數據類型
alter table 表名 modify 列名 新類型;
重命名
alter table 表名 rename column 列名 to 新列名;
alter table 表名 rename to 新表名;
eggs:

增加列
alter table student add email varchar2(30) null;
刪除列
alter table student drop column email;
修改數據類型
alter table student modify phone varchar2(30);
重命名
alter table student rename column gender to sex; 
alter table student rename to 新表名;

數據的增刪改查

1.增加數據
語法: insert into 表名(列名,列名…) values(值,值…)
非number類型的數據使用單引號
值的數量和列的數量一致
非空 且沒有默認值的列必須賦值
簡寫語法:
給表中的每一項都賦值
insert into 表名 values(值,值…)
把查詢得到的結果插入到表中
insert into 表名 select…
注意:
1.1 不能違反約束
1.2 不能超過數據的長度
1.3 先加主表,再加子表

序列: 自動編號
create sequence seq_exam_examid
increment by 1
start with 10000
cache 10
使用
seq_exam_examid.nextval
查看當前序列號:
select seq_exam_examid.currval from dual;

2.數據修改
update 表名 set 列名=值,列名=值 where 條件;

update students set sex='女',phone='15966667777' where stuname='李四';

3.刪除數據

語法: delete from 表名 where 條件;
先刪子表,再刪主表
truncate table 表名 和delete form 表名效果一樣
但truncate table 表名可以清空數據,刪除效率更高,但不可恢復

4 查詢數據
4.1 查詢所有數據
select * from 表
4.2 篩選行
select * from 表 where 條件
篩選列
select 列名,列名 from 表
select 列名,列名 from 表 where 條件;
4.3 列別名
select ename as 員工姓名,sal as 薪水 from emp where deptno=10;
4.4 常量列
select ename as 員工姓名,sal as 薪水, ‘中國’ as 國籍 from emp where deptno=10;
4.5隱藏列
rowid rownum
select emp.* ,rowid from emp;
查詢到的rowid這一列的數據就相當於行的地址,即這一列存儲的位置,拿這個值當條件,可以提高查詢的速度
select emp.* ,rownum from emp;
查詢到的rownum這一列就相當於行號
eggs:

1.查詢員工姓名不帶S的員工信息
  select * from emp where ename not like '%S%';
2.查詢不在10號和20號部門的員工信息
  select * from emp where deptno not in(10,20);
3.查詢10號部門工資3000以上的員工信息
  select * from emp where deptno=10 and sal>=3000;
4.查詢上司是SMITH的員工信息
  select * from emp where mgr=7369;
5. order by 排序
升序 asc
降序 desc
--以部門升序,工資降序排列
 select * from emp order by deptno asc ,sal desc ;
--獲取工資按降序排列的前三個
select * from
(  
  select mytable.*,rownum as rn from
  (
   select * from emp order by sal desc
  )mytable  
)t where rn<=3;

6.聚合函數
    sum()
    avg()
    max()
    min()
    count()
    
    --查詢10號部門的平均工資
    select avg(sal) from emp where deptno=10;
    --查看最高工資
    select max(sal) from emp;
    select * from emp where sal=5100;
    或者:
    select * from emp where sal=(select max(sal) from emp);
    --計算年薪
    select ename,(sal+nvl(comm,o))*13 from emp;
 7. 分組:分類統計   group by 
    --按部門分組
    select  deptno from emp group by deptno;
    分組後可以選擇的列
    A 被分組的列
    B 使用聚合函數統計的列
    --查看部門、最高工資、人數(除了被分組的列可以放在select後面,其他的都要使用聚合函數)
    select  deptno,max(sal),count(*) from emp group by deptno;
    --查看每個職位上有多少人
    select job,count(*) from emp group by job;
    --如果分組之後,還需要對數據進行篩選,要使用having
    select job,count(*) from emp group by job having count(*)>=3;
  where 
  order by
  having 
  group by
  順序
  select ... from ... where ... group by.... having .... order by....
  
  where 條件  分組之前篩選 不能使用聚合函數
  having  條件 分組之後篩選  可以使用聚合函數
  
  --統計工資超過2000的人數至少2個的部門
  select deptno from emp 
  where sal>=2000
  group by deptno
  having count(*)>=2 
  --查詢部門、工作和人數
  select  deptno,job,count(*) from emp group by deptno,job;
  --查看每個部門入職最早的
  select deptno,min(hiredate)  from emp group by deptno

表的權限

測試用戶: scott/tiger
超級管理員 sys / as sysdba
解鎖用戶:alter user scott account unlock;
創建用戶:create user 用戶名 identified by 密碼;
查詢當前登錄的用戶:show user;
登錄超級管理員:conn / as sysdba;
登錄普通用戶:conn 用戶名 identified by 密碼;
查詢所有用戶:select * from all_users;
權限:
給用戶授權
grant 權限名 to 用戶名;
給用戶登錄權限:
grant create session to 用戶;
給用戶登錄、資源的權限:
grant connect,resource to 用戶;
給用戶查看某個表的權限:
grant select on emp to 用戶;
給用戶修改某個表中某一項的權限:
grant update(ename) on emp to 用戶;
給用戶增加某個表中某一項的權限:
grant insert(ename) on emp to 用戶;
收回權限
revoke 權限名 from 用戶名;
授予權限後可以加級聯:
系統權限 with admin option
對象權限 with grant option
例如:grant create session to 用戶 with admin option;
收回系統權限不級聯
收回對象權限級聯
給表空間權限:
grant unlimited tablespace to 用戶;
給創建表空間權限:
grant create tablespace to 用戶;
創建角色:
create role 角色名;
列出系統權限
desc dba_sys_privs;
查看系統權限
select * from dba_sys_privs;
查看對象權限
select * from dba_tab_privs;
查看角色權限
select * from dba_role_privs;

表空間

1.創建表空間
create tablespace xiongsan_tablespace
datafile ‘d:\xiongsan.dbf’
size 10M
2使用表空間
2.1 給用戶指定默認的表空間

create user xiongsi identified by 123456 default tablespace xiongsan_tablespace;
–默認user表空間

2.2 給用戶指定表空間
create table aaaa(…) tablespace xiongsan_tablespace;

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章