Oracle 資料庫常用 sql 語句筆記

2021-09-25 17:44:42 字數 1407 閱讀 5271

1. 建立表

create table users (    

id int not null primary key,

user_name varchar(50) not null,

age int not null,

*** char(1) not null,

date birthday not null,

addr varchar(100)

)

建立了乙個表,包含了 id, user_name, age, ***,addr 字段, 其中id為主鍵。

如果想設定多個字段組合為主鍵,則可以這樣寫:

create table users (

id int not null,

user_name varchar(50) not null,

age int not null,

*** char(1) not null,

date birthday not null,

addr varchar(100),

constraint user_unique unique(user_name, age, ***, birthday, addr)

)

2. 關於時間

有date 型別和timestamp 型別, 兩者是有區別的

如果設定為date型別,在select 顯示的時候預設不顯示時分秒。而timestamp 是會顯示時分秒的。

date 賦值:

to_date ( '2018-12-20 18:31:34' , 'yyyy-mm-dd hh24:mi:ss' )

可以使用上面的這種方式來設定時間

timestamp賦值:

可以使用to_date賦值,也可以使用sysdate關鍵字直接賦值(當前時間)。

3. 改變表,增減字段

alter table users

add (phone number(11) default '00000000000' not null);

alter table users

drop column phone;

4. 建立序列

oracle 不支援字段自動遞增,而是使用序列的形式。

create sequence user_seq

increment by 1

start with 1

maxvalue 99999

minvalue 1

;

使用方式:

user_seq.nextval

5. 刪除表

drop table table_name;

ORACLE資料庫常用SQL

1.新增乙個表,通過另乙個表的結構和資料 create table product bak as select from product2.如果表存在 insert into product bak select from product 3.同乙個表中,將a欄位的指賦給b欄位 update pro...

Oracle資料庫常用SQL

oracle ora 00984 column not allowed here ora 00984錯誤 列在此處不允許 當資料以char的形式存在時,應加單引號,則插入資料庫就不會出現類似錯誤.oracle實現select的結果集隨機展示 select from tablename order b...

Oracle資料庫常用SQL

oracle資料庫建立例項的過程類似於sql server建立資料庫,oracle乙個例項可以對應多個表空間,乙個表空間對應乙個使用者,根據不同的使用者名稱 密碼登入不同的表空間。因此,建立表空間後,緊接著要建立使用者並為其指定表空間。並授權給該使用者,一般是connect resource dba...