sql 語句系列(插入系列)[八百章之第四章]

複製數據到另一個表

這個不解釋,只是自我整理。mysql

insert EMP_EAST (DEPTNO,DNAME,LOC)
select DEPTNO,DNAME,LOC
from DEPT
where LOC in ('NEW YORK','BOSTON')

複製表的定義

說白了就是複製表的結構,pass 平臺用的挺多的,須要動態建立表。sql

select * into dept_2
from DEPT
where 1=0

介紹一下mysql的:code

create table dept_2
as
select *
from dept
where 1=0

禁止插入特定的列

好比說有一張表,你只容許它插入某些固定的行,其餘行不讓插入。table

不少人會以爲這個需求很奇怪,不讓插入那麼要其餘列幹啥呢?其餘列可能就是默認值了。class

實現這種需求也特別簡單,採用視圖的方式,原理就是更新視圖那麼對應的表也會更新。原理

create view new_emps as
select empno,ename,job
from EMP

而後插入視圖,就能夠了。select

insert into new_emps(empno,ename,job)
values(1,'xxx','xxx')
相關文章
相關標籤/搜索