– 查看表
select * from myuser;
–創建表
create table myuser(
id number(4) primary key,
mname varchar2(12) not null unique,
sal number(6,2) not null,
birthday date default sysdate
);
–插入資料
insert into myuser values(1,'孫悟空',999.342,sysdate);
– 添加一列 年齡
alter table myuser add age number(3);
– 洗掉一列
alter table myuser drop column age;
– 改變一列的型別(注意型別)
– 1、當欄位沒有資料或者要修改的新型別和原型別兼容時,可以直接modify修改,
– 2、當欄位有資料并用要修改的新型別和原型別不兼容時,要間接新建欄位來轉移,
alter table myuser modify age number(4);
– 查看表中列的型別 (在我們的終端里看 在orcale中是無法運行的)
desc myuser;
– 修改列名
alter table myuser rename column birthday to mybirthday;
–更新 孫悟空的資訊
update myuser
set sal=1000
where mname='孫悟空';
– 創建myuser_b表,復制myuser表中的結構,同時復制myuser表的所有資料
create table myuser_b
as
select * from myuser;
– update 利用另外一張表關聯更新本表資料
update myuser u1
set(sal)=
(select sal from myuser_b u2 where u1.id=u2.id)
where exists (select 1 from myuser_b u2 where u1.id=u2.id);
格式:
-- 創建表 create table 表名 (
列名 型別 主鍵(primary key) 主鍵是唯一的
列名 型別 是否為空 是否可重復(unique)
······
)
-- 插入資料 insert into 表名 values (····) //根據表的類名順序進行賦值
insert into 表名(列名1,列名2,···) values (····) //根據表的類名順序進行賦值
-- 在表中添加 alter into 表名 add 新列名 新列名型別;
-- 在表中洗掉 alter into 表名 drop column 列名;
-- 在表中修改列的型別 alter into 表名 modify 列名 新型別
-- 在表中修改列名 alter into 表名 rename column 列名 to 新列名
-- 修改表明 rename 表名 to 新表名
-- 更新資料 update 表名 set 列名1=值1,列名2=值2,··· where 條件
-- 復制表資料 首先
-- create table 新表
-- as
-- select * from 需要復制的表 (后面可不加where 1=1)
-- 利用另外一張表關聯更新本表資料
-- select 本表1
-- sel (列名)=
-- (select 列名 from 表2 where 表1.欄位=表2.欄位 ) 進行關聯
-- where exists(select 1 from 表2 where 表1.欄位=表2.欄位 )
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/96141.html
標籤:其他
上一篇:Oracle快速入門(觸發器)
