oracle基本SQL語句

2021-06-17 17:49:29 字數 1931 閱讀 3053

進入介面:  sqlplus   使用者名稱/資料庫密碼@資料庫名稱   如: sqlplus  pwsys/password@test

新增字段: alter table tablenameadd (columnname  varchar2(30));

修改字段型別大小:   alter table tablename modify columnname varchar2(300);

字段重新命名:alter table tablenamerename column namea to nameb;

刪除字段:alter tabledrop column tablename;

刪除表: 1.delete from tablename;(刪除所有記錄,表結構還在,寫日誌,可以恢復的,速度快)

2.truncate  table tablename;(刪除表中的所有記錄,表結構還在,不寫日誌,無法找回刪除的記錄,速度快)

3.drop table  tablename;(刪除表的結構和資料)

4.drop table tablenamepurge;(會把表直接刪除,不放進**站)

級聯刪除:主表a 主鍵a_id  子表 b 主鍵 b_id  外來鍵a_id  

alter tablepwsys.b add foreign key (a_id)  referencespwsys.a (a_id)on delete cascade;

置空: alter table pwsys.b add foreign key (a_id)  references pwsys.a (a_id) on delete set null;

新建序列

create sequence  pwsys.a_seq

minvalue 1

maxvalue 9999999999

start with 1

increment by 1

cache 20;

自增主鍵觸發器:

create or replace trigger pwsys.a

before insert  on a

for each row

declare

numrows integer;  

begin

/* create unique identity */

if pw.identity then

select pwsys.a_seq.nextval into numrows from dual;

:new.a_id := numrows;

end if;

end;/

遞迴查詢: vld_site 為單位表,1級單位, 共有n級單位,如:如何查詢某3級單位下的所有單位

表結構設計

create table t2

如:如何查某3級單位下的所有單位? 查詢語句如下(假如該單位id為10)  

select vld_site_id from  vld_site 

start with  vld_site_id=10

connect by prior vld_site_id=vld_parent_site_id

(prior 實現遞迴查詢)

反過來:如何查詢某基層單位所屬的

二、**單位

select vld_site_id from  vld_site 

where hierarchy_level=2

start with vld_site_id=5233700

connect by prior vld_parent_site_id=vld_site_id(把遞迴條件反過來)

Oracle 基本SQL語句

新增乙個表 create table testuser id int primary key name varchar 20 address varchar 20 設定序列號 標識列 第一步 建立sequence create sequence s country id increment by 1...

SQL 基本語句

在查詢分析器中執行如下語句 sp password null,teracypwd sa 把sa的密碼設為 teracypwd 執行成功後有 command s completed successfully.ok insert into 表名稱 values 值1,值2,insert into per...

sql基本語句

sql常用命令 資料的增刪改查 增加資料 插入資料 insert into 表名 字段 字段 values 值,值,值.按需要字段填寫 insert into 表名 values 值,值,值.插入全部字段,自動增長列不寫 刪除資料 記得有外來鍵先刪除主鍵表裡的相應內容 刪除表裡的內容 delete ...