1. 程式人生 > >oracle建立表相關語句

oracle建立表相關語句

--建立使用者
create user han identified by han default tablespace
use Temporary TABLESPACE Temp;
grant connect,resource,dba to han; //授予使用者han開發人員的權利

--------------------對錶的操作--------------------------
--建立表
create table classes(
       id number(9) not null primary key,
       classname varchar2(40) not null
)      
--查詢表
select * from classes;

--刪除表
drop table students;

--修改表的名稱
rename alist_table_copy to alist_table;

--顯示錶結構
describe test --不對沒查到

-----------------------對欄位的操作-----------------------------------
--增加列
alter table test add address varchar2(40);

--刪除列
alter table test drop column address;

--修改列的名稱
alter table test modify address addresses varchar(40;

--修改列的屬性
alter table test modi

create table test1(
       id number(9) primary key not null,
       name varchar2(34)
      )
rename test2 to test;

--建立自增的序列
create sequence class_seq increment by 1 start with 1 MAXVALUE 999999 NOCYCLE NOCACHE;

select class_seq.currval from dual

--插入資料
insert into classes values(class_seq.nextval,'軟體一班')
commit;

--更新資料
update stu_account set username='aaa' where count_id=2;
commit;

--建立唯一索引
create unique index username on stu_account(username);   --唯一索引不能插入相同的資料

--行鎖 在新開啟的對話中不能對此行進行操作
select * from stu_account t where t.count_id=2 for update; --行鎖


--alter table stuinfo modify sty_id to stu_id;

alter table students drop constraint class_fk;
alter table students add constraint class_fk foreign key (class_id) references classes(id);--外來鍵約束
alter table stuinfo add constraint stu_fk foreign key (stu_id) references students(id) ON DELETE CASCADE;--外來鍵約束,級聯刪除

alter table stuinfo drop constant stu_fk;   

insert into students values(stu_seq.nextval,'張三',1,sysdate);

insert into stuinfo values(stu_seq.currval,'威海');

select * from stuinfo;

create table zhuce(
       zc_id number(9) not null primary key,
       stu_id number(9) not null,
       zhucetime date default sysdate

)

create table feiyong (
       fy_id number(9) not null primary key,
       stu_id number(9) not null,
       mx_id number(9) not null,
       yijiao number(7,2) not null default 0,
       qianfei number(7,2) not null
       
)


create talbe fymingxi(
       mx_id number(9) not null primary key,
       feiyong number(7,2) not null,     //共7位數字,小數後有兩位
       class_id number(9) not null
}

create table card(
       card_id number(9) primary key,
       stu_id number(9) not null,
       money number(7,2) not null default 0,
       status number(1) not null default 0   --0表可用,1表掛失
)

--連結串列查詢

select c.classname||'_'||s.stu_name as 班級_姓名,si.address from classes c,students s , stuinfo si where c.id=s.class_id and s.id=si.stu_id; 
insert into students values(stu_seq.nextval,'李四',1,sysdate);
insert into stuinfo values(stu_seq.currval,'南京');

--函式
select rownum,id,stu_name from students t order by id asc;


--中間表實現多對多關聯
--(1   1, 1   n,n 1,n n )


--1 n的描述   1的表不作處理   n的表有1表的欄位
--1 1的描述   主外來鍵關聯
--n n的描述 中間表實現多對多關聯

create table course(
         course_id number(9) not null,
         couser_name varchar2(40) not null
)
alter table course to couse;
create table stu_couse(
       stu_couse_id number(9) primary key,
       stu_id number(9) not null,
       couse_id number(9) not null

)

create unique index stu_couse_unq on stu_couse(stu_id,couse_id); --唯一學生
create sequence stu_couse_seq increment by 1 start with 1 MAXVALUE 999999 NOCYCLE NOCACHE;


create sequence couses_seq increment by 1 start with 1 MAXVALUE 999999 NOCYCLE NOCACHE;
insert into course values(couses_seq.nextval,'計算機原理');
insert into course values(couses_seq.nextval,'編譯原理');
insert into course values(couses_seq.nextval,'資料庫原理');
insert into course values(couses_seq.nextval,'資料結構');
insert into course values(couses_seq.nextval,'計算機基礎');
insert into course values(couses_seq.nextval,'C語言初步');
commit;

insert into stu_couse values(stu_couse_seq.nextval,1,1);
insert into stu_couse values(stu_couse_seq.nextval,1,3);
insert into stu_couse values(stu_couse_seq.nextval,1,5);
insert into stu_couse values(stu_couse_seq.nextval,1,5);

insert into stu_couse values(stu_couse_seq.nextval,2,1);
commit;
select * from stu_couse;
select * from course;

--select s.stu_name,sc.couse_id, c.couser_name from students s,course c,stu_couse sc where stu_id=1

--select couse_id from stu_couse where stu_id=1

select cl.classname,s.stu_name,c.couser_name from stu_couse sc, students s,course c,classes cl where s.id=sc.stu_id and sc.couse_id=c.course_id and s.class_id=cl.id and s.id=1;

--班級——姓名
select c.classname,s.stu_name from students s,classes c where s.class_id=c.id and s.id=2;

select * from students s where s.id=2
--班級——姓名——課程

select cl.classname,s.stu_name,c.couse_name from stu_couse sc,students s,classes cl,couse c where sc.stu_id=s.id and sc.couse_id=c.couse_id and s.id=26;


--sql 語句的寫法,現寫出關聯到的表,然後寫出要查詢的欄位,第三 寫出關聯條件   ,記住在寫關聯到的表時先寫資料多的表,這樣有助於提高sql的效率

select c.couser_name,s.stu_name from stu_couse sc,students s,course c where c.course_id=1 and c.course_id=sc.couse_id and sc.stu_id=s.id;

select s.stu_name from students s,stu_couse sc where s.id=sc.stu_id group by s.id,s.stu_name;


select c.classname,count(sc.couse_id) from stu_couse sc,students s,classes c where s.class_id=c.id and s.id=sc.stu_id group by c.classname;

select s.stu_name, count(sc.couse_id) from stu_couse sc,students s,classes cl where s.id=sc.stu_id group by s.id,s.stu_name having count(sc.stu_couse_id)>3;
班級 學生 選課數量
select cl.classname,count(sc.stu_couse_id) from stu_couse sc,students s,classes cl where s.id=sc.stu_id and s.class_id=cl.id group by cl.classname;


--班級 學生 選課數量
select cl.classname,s.stu_name,count(sc.stu_couse_id) from stu_couse sc,students s,classes cl where s.id=sc.stu_id and s.class_id=cl.id group by s.stu_name;

select cl.classname,s.stu_name,count(sc.stu_couse_id) from stu_couse sc ,students s,classes cl where sc.stu_id=s.id and s.class_id=cl.id group by s.id;

select cl.classname,s.stu_name,count(sc.stu_couse_id) from stu_couse sc,students s,classes cl where sc.stu_id=s.id and s.class_id=cl.id group by s.stu_name;
--班級 學生 所選課程id 所選課程名稱


--建立試圖 目的把表聯合起來 然後看成一個表,在與其他的聯合進行查詢 
create view xsxk as select cl.classname, s.stu_name,c.couse_id, c.couse_name from stu_couse sc,students s,classes cl,couse c where sc.stu_id=s.id and sc.couse_id=c.couse_id and s.class_id=cl.id;

select * from xsxk


create view classstu as select s.id,c.classname,s.stu_name from students s,classes c where c.id=s.class_id;
drop view classstu; --刪除檢視
select * from classstu;
create view stu_couse_view as select s.id ,c.couse_name from stu_couse sc,students s,couse c where s.id=sc.stu_id and sc.couse_id=c.couse_id;
select * from stu_couse_view;
create view csc as select cs.classname,cs.stu_name,scv.couse_name from classstu cs,stu_couse_view scv where cs.id=scv.id;
select * from csc;


select * from classes cross join students; --全連線,相當於select * from classes,students;

select * from classes cl left join students s on cl.id=s.class_id; --左連線 不管左表有沒有 都顯示出來
select * from classes cl right join students s on cl.id=s.class_id; --右連線
select * from classes cl full join students s on cl.id=s.class_id; --全連線


insert into classes values(class_seq.nextval,'軟體四班');

create table sales(
       nian varchar2(4),
       yeji number(5)
       
);
insert into sales values('2001',200);
insert into sales values('2002',300);
insert into sales values('2003',400);
insert into sales values('2004',500);
commit;
select * from sales;
drop table sale;


select s1.nian,sum(s2.yeji) from sales s1,sales s2 where s1.nian>=s2.nian group by s1.nian order by s1.nian desc;

select s1.nian,sum(s2.yeji) from sales s1,sales s2 where s1.nian>=s2.nian group by s1.nian;

s
       年       年業績總和 
       2001     200
       2002     500
       2003     900
       2004     1400


create table test1(
       t_id number(4)
);

create table org(
       org_id number(9) not null primary key,
       org_name varchar2(40) not null,
       parent_id number(9)
);

create sequence org_seq increment by 1 start with 1 MAXVALUE 999999 NOCYCLE NOCACHE;
drop sequence org_seq;
insert into org values(1,'華建集團',0);
insert into org values(2,'華建集團一分公司',1);
insert into org values(3,'華建集團二分公司',1);
insert into org values(4,'華建集團財務部',1);
insert into org values(5,'華建集團工程部',1);
insert into org values(6,'華建集團一分公司財務處',2);
insert into org values(7,'華建集團一分公司工程處',2);

select * from org;
--不正確 不能實現迴圈
select b.org_id , b.org_name ,b.parent_id from org a,org b where a.org_id=7 and a.parent_id=b.org_id;
select * from org connect by prior parent_id=org_id start with org_id=7 order by org_id;
select * from org connect by prior org_id=parent_id start with org_id=1 order by org_id;

create table chengji(
       cj_id number(9) not null primary key,
       stu_cou_id number(9) not null,
       fen number(4,1)
       
);
insert into chengji values(1,1,62);
insert into chengji values(2,2,90);
insert into chengji values(3,3,85);
insert into chengji values(4,4,45);
insert into chengji values(5,5,68);
insert into chengji values(6,6,87);
commit;
select * from chengji;
select * from stu_couse;
--在oracle 中好像不適用 alter table chengji change stu_cou_id stu_couse_id;alter table shop_jb change price1 price double;

學生姓名   平均分
select s.stu_name,avg(cj.fen) from stu_couse sc,chengji cj,students s where s.id=sc.stu_id and sc.stu_couse_id=cj.stu_couse_id group by s.id,s.stu_name;
select s.stu_name from students s,stu_couse sc,chengji cj where s.id=sc.stu_id and sc.stu_couse_id=cj.stu_couse_id group by s.id,s.stu_name;
select s.stu_name,cj.fen from students s,stu_couse sc,chengji cj where s.id=sc.stu_id and sc.stu_couse_id=cj.stu_couse_id and cj.fen>60;

學生姓名   科目   成績
select s.stu_name,c.couse_name,cj.fen from stu_couse sc,students s,couse c,chengji cj where sc.stu_id=s.id and sc.couse_id=c.couse_id and sc.stu_couse_id=cj.stu_couse_id and cj.fen>60 order by=;

select * from stu_couse;

--集合運算
--選擇了課程3的學生   union 選擇了課程5的學生   並集
--選擇了課程3 或者 選擇了課程5的學生
select s.stu_name from students s,couse c,stu_couse sc where s.id=sc.stu_id and sc.couse_id=c.couse_id and c.couse_id=3
union
select s.stu_name from students s,couse c,stu_couse sc where s.id=sc.stu_id and sc.couse_id=c.couse_id and c.couse_id=5

--選擇了課程3,5,2 的學生 intersect 選擇課程1,2,4的學生    交集
--求選擇了課程 2 並且 選擇了課程 3 的學生   交集
select s.stu_name from students s,couse c,stu_couse sc where s.id=sc.stu_id and sc.couse_id=c.couse_id and c.couse_id=2
intersect
select s.stu_name from students s,couse c,stu_couse sc where s.id=sc.stu_id and sc.couse_id=c.couse_id and c.couse_id=3;


--選擇了課程3,5,8的學生   minus 選擇了課程1,7,8的學生   --差集
-- 求所有課程的成績都大於 60 的學生 差集
select distinct(s.stu_name) from stu_couse sc,students s,couse c,chengji cj where sc.stu_id=s.id and sc.couse_id=c.couse_id and sc.stu_couse_id=cj.stu_couse_id and cj.fen>60
minus
select distinct(s.stu_name) from stu_couse sc,students s,couse c,chengji cj where sc.stu_id=s.id and sc.couse_id=c.couse_id and sc.stu_couse_id=cj.stu_couse_id and cj.fen<60;

 from http://blog.163.com/jian__1216/blog/static/1712491512010820103733340/

相關推薦

oracle建立相關語句

--建立使用者 create user han identified by han default tablespace use Temporary TABLESPACE Temp; grant connect,resource,dba to han; //授予使用者han開發人員的權利 -------

oracle 建立sql語句,主鍵,自增長

在建立表之前請必須瞭解一下兩點 1,oracle 沒有identity,所以需要自己用squence 和 trigger 配合使用 實現 自增長。 2,oracle中如果使用關鍵字,需要用 雙引號引起了。所以下面例子中出現的雙引號可以理解成轉義的。 /    在執行多個

oracle 建立create table語句

1.建立一個表2,且表2的結構和資料和表1一樣(表1提前建立): create table 表2 as select * from 表1; 2.建立一個表2,且表2的結構和表1一樣,但是沒有資料: create table 表2 as select * from 表1

Oracle建立空間、使用者,使用者授權語句解析

建立表空間               create tablespace(temporary tablespace) indx --建立名字叫indx的表空間(臨時表空間) dataf

Oracle建立語句(Create table)語法詳解及示例

8. 較複雜的建立表例子SQL> CREATE TABLE s_emp(id NUMBER(7)CONSTRAINT s_emp_id_pk PRIMARY KEY,last_name VARCHAR2(25)CONSTRAINT s_emp_last_name_nn NOT NULL,first_na

Oracle建立空間和用戶

creat 增長 pac files tor initial ide efault start Oracle建立表空間和用戶 建立表空間和用戶的步驟: 用戶 建立:create user 用戶名 identified by

ORACLE----多查詢語句練習

sco tag lead num creat cor 多表數據查詢 pda 查詢 一.建表 1.建立兩張表CLASSINFO,STUDENTINFO. --建表CLASSINFO;CREATE TABLE CLASSINFO ( CLASSID NUMBER(2) P

oracle建立空間及使用者,匯出匯入資料備份

轉載:https://blog.csdn.net/ludongshun2016/article/details/52849784?utm_source=copy  建立表空間及使用者 //建立臨時表空間   create temporary tablespace salt

Oracle建立空間和

    本人使用的操作資料庫的IDE事datagrip(與強大的IDEA,WEBSTORM同屬捷克的JetBrains公司),用慣了idea和webstorm的同志們會覺得畫面很親切     下面介紹一下建立表空間,建立操作使用者,以及為使用者分配許可權的操作: &

oracle建立空間 建立使用者 建立

分享一下我老師大神的人工智慧教程!零基礎,通俗易懂!http://blog.csdn.net/jiangjunshow 也歡迎大家轉載本篇文章。分享知識,造福人民,實現我們中華民族偉大復興!        

oracle建立空間、使用者、許可權 oracle 建立臨時空間/空間,使用者及授權

原連結:https://www.cnblogs.com/wxm-bk/p/6510654.html oracle 建立臨時表空間/表空間,使用者及授權   1:建立臨時表空間 create temporary tablespace user_temp tempfile

ORACLE 建立(11g)

CREATE TABLE語句 create table [schema.] 表名 (column datatype [DEFAULT expr][, ...]); 在插入的過程中,為列指定一個預設值 ...hire_date DATE DEFAULT SYSDATE,... 預設值必須滿足列的資料型別

Oracle建立空間、、一級使用者授權

---恢復內容開始--- 1. 安裝:   百度一下你就知道 2. sqlplus登入/sqlplus命令登入   在安裝Oracle時,你需要記住設定的“全域性資料庫名”(預設為orcl) 和 口令,在以兩種方式登入時:   使用者名稱: sys(超級使用者==sysdba) / system(管

Oracle建立空間建立使用者和使用者授權

今天要建立一個Oracle使用者,然後發現sql不太記得了,然後只能再去找找資料,發現這樣效率並不是很高,所以記錄成部落格,有需要就直接從部落格複製。 下面是我簡單整理的,有需要可以參考。 --建立表空間 CREATE TABLESPACE WORKFLOW01 LOGGING DATAFILE 'D:\or

oracle 建立空間詳解、例項

建立臨時表空間----->建立表空間---->建立使用者指定表空間----->授權     --查詢表空間 select * from dba_tablespaces --查詢表空間路徑 select * from dba_data_file

Oracle建立分割槽----範圍分割槽

建立分割槽是資料庫優化的一種手段,範圍分割槽是表分割槽的一種。      建立範圍分割槽的關鍵字是"RANGE",建立該分割槽後,其中的資料可以根據分割槽鍵值指定的範圍進行分佈,當資料在範圍內均勻分佈時,效能最好。 例如我們選擇一個日期作為分割槽鍵,分割槽“AUG-201

oracle安裝第二彈--oracle建立空間及使用者和密碼

----20180929 By skyfans 第一彈我們進行的oracle基礎服務的安裝及監聽配置,已經可以實現oralce的開機自啟動,那麼系統如何連線oracle資料庫呢?這裡我們講述使用者的建立、表空間建立及指令碼的匯入,完成此步驟後,我們已經可以進行o

oracle建立空間

1、建立表空間、使用者、授權 --/*第1步:建立臨時表空間 */   create temporary tablespace yt_temp  tempfile 'D:\oracle\namespace\yt_temp.dbf'  size 50m  autoextend

Oracle建立空間、新增使用者

--查詢表空間 SELECT tablespace_name, file_id, file_name, round(bytes / (1024 * 1024), 0) total_space FROM dba_data_files ORDER BY tablespace_name; --建立

Oracle建立空間和使用者及授權並匯入資料學習筆記

第一步:連線資料庫 通過oracle自帶的客戶端工具sqlplus,以管理員角色連線資料庫 第二步:建立表空間 1.臨時表空間 create temporary tablespace 臨時表空間名 tempfile '表空間存放路徑\臨時表空間名.dbf' size