oracle印象之基礎

2021-07-04 00:04:37 字數 3976 閱讀 3153

create table student

(stu_no varchar2(7) not null primary key,-- 學號

stu_name varchar2(20) not null,--姓名

stu_gender varchar2(200) not null,---性別

stu_age number(2) not null,---年齡

stu_seat number(3) not null,---座位

enrolldate date,---入學時間

stu_address varchar2(200) default '位址不詳',---家庭住址

classno varchar(2) not null---班號

);

在建表的過程中,可以在create table的時候就指定表的約束,例如:
create table student

(stu_no varchar2(7) not null,-- 學號

stu_name varchar2(20) not null,--姓名

stu_gender varchar2(200) not null,---性別

stu_age number(2) not null,---年齡

stu_seat number(3) not null,---座位

enrolldate date,---入學時間

stu_address varchar2(200) default '位址不詳',---家庭住址

classno varchar(2) not null---班號

constraint primary key(stu_no)

);

也可以單獨用語句來指定,比如:
alter table student add constraint pk_student primary key(stu_no);

/alter table student add constraint ck_student_stu_gender check (stu_gender='男' or stu_gender='女');

/alter table student add constraint ck_student_stu_age check (stu_age >0 and stu_age <100);

/alter table student add constraint un_stu_name unique(stu_name);

增(插入資料):

在oracle 中 用insert命令來完成資料插入,

語法為:insert into 表名(列1,列2,... )values(值1,值2,...);

其中,列名可以省略,則表示向所有列插入。且值得數量和順序應該與列的數量和順序一一對應。

例如:

insert into student (stu_no,stu_name,stu_gender,stu_age,stu_seat,enrolldate,stu_address,classno)

values ('1','令狐沖','男','28','13',to_date('2015-07-22 15:19:02','yyyy-mm-dd hh24:mi:ss'),'華山','05');

其中,oracle的日期是國際化的,不同的地域安裝的資料庫,它的日期格式可能是不一樣的,為了程式方便插入,可以用to_date函式格式化日期後再輸入。

在oracle中,insert命令還可以將乙個結果集一次性的插入到一張表中,如:

insert into student2 select * from student;
當然,這裡要求結果集中每一列的屬性需要和插入表列的屬性一致。

刪:在oracle中,用delete命令來完成資料的刪除,

語法為: delete from 表名 where 條件

還可以用truncate命令,一次性把表中的所有資料刪除。語法為:truncate table 表名。

delete和truncate命令的區別:

truncate是ddl命令,刪除後不能恢復。而delete是dml語句,刪除後,可以通過日誌檔案恢復。

如果一張表的資料比較多,用truncate比delete速度要快。

改:在oracle中,用update命令來實現資料的修改

語法為: update 表名 set 列=需要修改的值 where 條件

例如:

update student set stu_address='思過崖' where stu_name='令狐沖';
如果需要修改表的屬性,則需要用alter命令,語法為: alter table 表名 modify 列名 屬性;

如:

alter table student modify stu_gender varchar(200);
查:

在oracle中,用select命令來實現資料查詢,

語句為: select 列名 (as 別名) from表名 where 條件 order  by asc (desc);

也可以根據結果集來建立表,語法為: create table 表名 as select 列名 (as 別名) from 表名;

如:

create table student2 as select * from student;
上面只是一般查詢語句,當然還有一些高階的查詢,

① 去重,在oracle中可以用distinct命令來去除重複的資料。語法為:select distinct 列名 from 表名 where條件;

例如:

select distinct stu_name from student where stu_age>20;
② null 操作

在oracle中, 空值不等於0或者空格,是指未賦值,未知或者不可用的值。在sql語句中可以用 is null 或者 is not null 來判斷空值。

③ 在where條件語句中可以用 in 、between...and..、like 來進行條件查詢。

in: 指定值來查詢,如:select * from student where classno in ('1','3','5'); 當然也有 not in 語句;

between...and... 在某乙個區域來查詢,如:select * from student where stu_age between 10 and 20;

like 模糊匹配查詢。如:select * from student where stu_name like '%衝%';

④連線查詢

內連線:inner join... on ...

select * from a a, b b where a.id = b.id;            

select * from a a inner join b b on a.id = b.id;

左外連線 left (out) join ... on... 左邊的表全部展示

select * from a a left join b b on a.id = b.id;            

select * from a a , b b where a.id = b.id(+);

右外連線 right (out)join... on....右邊的表全部展示

select * from a a right join b b on a.id = b.id;            

select * from a a , b b where a.id(+) = b.id;

全連線  full join ... on ....左右表全展示
select * from a a full join b b on a.id = b.id;

廣州之印象

上個星期四到的廣州,就遇上了陰雨綿綿,看來運氣不怎麼樣,而且天氣很潮,到處濕漉漉的,地上要鋪報紙,牆壁上滲著水氣,空氣中瀰漫著發霉的味道,渾身粘粘的不舒服。這是我對廣州的第一印象 潮 還好在廣州有同學,還是小學同學,於是不算寂寞,晚上下班啦就去他們那吃飯,五個人自己做飯吃,還是蠻熱鬧的。同學住在天河...

oracle基礎之安裝

1,安裝資料庫服務,建立乙個資料庫後。oracle10g不會自動建立乙個 可以手動建立 開始 oracle oradb10g home1 configuration and migration tools net configuration assistant 選擇第乙個 建立 一致下一步就可以了。...

oracle 之 基礎操作

刪除存在的表空間及資料 drop tablespace ts yygl including contents and datafiles 若是出現了提示 錯誤 導致無法全部刪除,那麼就執行以下語句可以得到那些表有主鍵相關,再執行就可以 select alter table owner table n...