系統(tǒng)用戶:
1.sys system(sys權(quán)限最高)
2.sysman(操作企業(yè)管理器) 密碼是安裝的時(shí)候輸入的密碼
3.scott(默認(rèn)是tiger)
oracle中用戶,權(quán)限,角色
用戶:創(chuàng)建/修改/刪除 用戶
創(chuàng)建用戶:? ? CREATE USER name
IDENTIFIED BY password
[ACCOUNT LOCK|UNLOCK]
[PASSWORD EXPIRE];(口令立刻過(guò)期)
修改用戶:? ? ALTER USER name
IDENTIFIED BY password
[ACCOUNT LOCK|UNLOCK]
[PASSWORD EXPIRE];(口令立刻過(guò)期)
刪除用戶:DDROP USER name [CASCADE];
具體操作:
CREATE USER test
IDENTIFIED BY test
ACCOUNT LOCK
PASSWORD EXPIRE;
grant create session to test;(用戶授權(quán))
ALTER USER test ACCOUNT UNLOCK;(解鎖)
登錄
1.使用system用戶登錄
system/password@server
2.使用sys用戶登錄
connect sys/password as sysdba
查看登錄用戶
1.show user命令
2.dba_users數(shù)據(jù)字典(數(shù)據(jù)字典是數(shù)據(jù)庫(kù)提供的表,用于查看數(shù)據(jù)庫(kù)的信息)
desc dba_users; (查看數(shù)據(jù)字典里包含哪些字段)
結(jié)果包含如下一些字段:USERNAME? ? NOTNULL VARCHAR2(30)
USER_ID? ? ? NOTNULL NUMBER
PASSWORD? ? ? ? ? ? VARCHAR2(30)
...
在數(shù)據(jù)字典中包含哪些用戶?select username from dba_users;
啟用scott用戶
alter user username account unlock (解鎖)
alter user scott? ccount unlock;(用戶已更改0
使用scott用戶登錄SQL Plus
conn scott/tiger
修改scott密碼
SQL>? conn /as sysdba
SQL>? alter user scott identified by tiger;
SQL>? conn scott/tiger
SQL>? select * from tab;
表空間:
理解表空間:數(shù)據(jù)庫(kù)與表空間(一個(gè)數(shù)據(jù)庫(kù)有多個(gè)表空間)
表空間與數(shù)據(jù)文件
表空間的分類:永久表空間(表,視圖,存儲(chǔ)過(guò)程)
臨時(shí)表空間(數(shù)據(jù)庫(kù)操作當(dāng)中,中間執(zhí)行的過(guò)程,執(zhí)行結(jié)束后會(huì)被釋放)
undo表空間(保存事務(wù)所修改的舊值,可以對(duì)數(shù)據(jù)進(jìn)行回滾即撤銷操作)
查看用戶的表空間:
1.dba_tablespaces(系統(tǒng)用戶表空間) user_tablespaces(普通用戶表空間) 數(shù)據(jù)字典
步驟:conn system/abc...123
select tablespace_name from dba_tablespaces;
結(jié)果為:system? ? (存放sys系統(tǒng)表空間)
sysaux? ? (作為一個(gè)example的輔佐表空間)
undotbs1 (存儲(chǔ)撤銷的表空間)
temp? ? ? (臨時(shí)表空間)
users
exmple
步驟:desc user_tablespaces;
select tablespace_name from user_tablespaces;
2.dba_users user_usres數(shù)據(jù)字典
conn sys/abc...123 as sysdba
desc dba_users;
查看system默認(rèn)的表空間和臨時(shí)表空間是什么?
select defult_tablespace,temporary_tablespce from dba_users where username='SYSTEM';
結(jié)果是:SYSTEM? TEMP(學(xué)會(huì)如何更改默認(rèn)表空間和臨時(shí)表空間,方便數(shù)據(jù)的備份及恢復(fù))
設(shè)置用戶默認(rèn)或臨時(shí)表空間:
ALTER USER usernme DEFAULT|TEMPORARY TABLESPACE tablespace_name
alter user system default tablespace user;
創(chuàng)建表空間:
create [temporary] tablespace tablespace_name tempfile|datafile 'xx.dbf' size xx
創(chuàng)建永久表空間:
create tablespace test1_tablespace datafile 'textfile.dbf' size 10m;
創(chuàng)建臨時(shí)表空間:
create temporary tablespace temptest1_tablespace tempfile 'tempfile1.dbf' size 10m;
通過(guò)數(shù)據(jù)字典查看表空間位置:
通過(guò)數(shù)據(jù)字典查看創(chuàng)建的永久表空間:
desc dba_data_files;(返回的字段有file_name tablespace_name...)
查看表空間所在的文件位置:
select file_name from dba_data_files where tablespce_name='TEST1_TABLESPACE';
查看臨時(shí)表空間:desc dba_temp_files;
查看表空間所在的文件位置:
select file_name from dba_temp_files where tablespce_name='TEMPTEST1_TABLESPACE';
修改表空間:
1.修改表空間的狀態(tài)
a.設(shè)置聯(lián)機(jī)或脫機(jī)狀態(tài):alter tablespace tablespace_name online|offline;
創(chuàng)建完一個(gè)表空間之后,默認(rèn)狀態(tài)是聯(lián)機(jī)狀態(tài);
具體操作:
alter tablespace test1_tablespace offline;
desc dba_tablespaces;數(shù)據(jù)字典查看status
select status from dba_tablesaces where tablespace_name='TEST1_TABLESPACE';結(jié)果為:status:offline
再設(shè)置回聯(lián)機(jī)狀態(tài):alter tablespace test1_tablespace online;
select status from dba_tablespaces where tablespace_name='TEST1_TABLESPACE';結(jié)果為status:online
b.設(shè)置只讀或可讀寫狀態(tài):alter tablespace tablespace_name read only|read write;
默認(rèn)情況下是可讀寫的,聯(lián)機(jī)狀態(tài)才可以更改讀寫性
具體操作:
alter tablespace test1_tablespace read only;
select status from dba_tablespaces where tablespace_name='TEST1_TABLESPACE';結(jié)果為:status:online
2.修改數(shù)據(jù)文件
a.增加數(shù)據(jù)文件:
alter tablespace tablespace_name add datafile 'xx.dbf' size xx;
具體操作:向表空間test1里添加數(shù)據(jù)文件
alter tablespace test1_tablespace add datafile 'text2_file.dbf' size 10m;
select file_name from dba_files where tablespace_name='TEST_TABLESPACE';查看新增的文件位置
b.刪除數(shù)據(jù)文件:
alter tablespace tablespace_name drop datafile 'filename.dbf'
不能刪除表空間里面的第一個(gè)數(shù)據(jù)文件,如果要?jiǎng)h除的話需要把整個(gè)表空間刪掉。
具體操作:刪除上面新增的text2_file.dbf
alter tablespace test1_tblespace drop datafile'text2_file.dbf';
select file_name from dba_files where tablespace_name='TEST_TABLESPACE';查看文件位置發(fā)現(xiàn)text2已經(jīng)刪除
刪除表空間:
drop tablespace tablespace_name [INCLUEDING CONTENTS]
如若僅刪除表空間,不刪除數(shù)據(jù)文件:則不需要加 [INCLUDING CONTENTS]
具體操作:刪除表空間test1
drop tablespace test1_tablespace including contents;刪除表空間成功
操作表:
1.認(rèn)識(shí)表
a.表都存放在表空間里,是數(shù)據(jù)庫(kù)基本存儲(chǔ)單位,是一個(gè)二維結(jié)構(gòu),行(記錄)和列(域和字段)
b.約定:1.每一列數(shù)據(jù)必須具有相同的數(shù)據(jù)類型
2.列名唯一
3.每行數(shù)據(jù)的唯一性(重復(fù)的行會(huì)造成表中數(shù)據(jù)的冗余)
2.數(shù)據(jù)類型
a.字符型:
char(n),nchar(n)是固定長(zhǎng)度的,nchar是按照unique格式存放數(shù)據(jù),char的n最大值是2000,而nchar的n最大值是1000;一般用nchar存儲(chǔ)漢子。不夠會(huì)自動(dòng)補(bǔ)上空格。
varchar2(n),nvarchar2(n),varchar2(n)最大值是4000,nvarchar2(n)最大值是2000.不夠不會(huì)補(bǔ)上空格,可以節(jié)省空間。
b.數(shù)值型
number(p,s) p代表有效數(shù)字,s代表小數(shù)點(diǎn)后的位數(shù);例如:number(5,2) 有效數(shù)字5位,保留2位小數(shù),如123.45s
float(n) 主要存儲(chǔ)二進(jìn)制數(shù)據(jù)(能表示二進(jìn)制的數(shù)據(jù)位數(shù)是1-126位),二進(jìn)制數(shù)轉(zhuǎn)換成十進(jìn)制需要乘以0.30103才能得到。所以在oracle中使用number比較多
c.日期型
data date類型表示范圍:公元前4712年1月1日到公元9999年12月31日
可以精確到秒。
timestamp時(shí)間戳類型,能精確到小數(shù)秒
d.其他
blob:能存放4g的數(shù)據(jù)(二進(jìn)制)
clob:能存放4g的數(shù)據(jù)(字符串類型)
3.管理表
創(chuàng)建表:
基本語(yǔ)法:
create table table_name
(
column_name datatype,...
)在同一個(gè)用戶下,所有的表名要是唯一的
創(chuàng)建用戶信息表:
所需字段:? 編號(hào)? ? 用戶名 密碼? 郵箱? 注冊(cè)時(shí)間
字段的類型:number? ? varchar2(n)? ? ? ? date
create table userinfo
(id number(6,0),
username varchar2(20),
userpwd varchar2(20),
email varchar2(30),
regdate date);
查看表的結(jié)構(gòu)與查看數(shù)據(jù)字典的方法一樣:desc userinfo
修改表:(修改表的結(jié)構(gòu),而不是數(shù)據(jù))
1.添加字段(列或者域):
如果一個(gè)表已經(jīng)創(chuàng)建好,還想再添加一些字段:
alter table table_name add column_name datatype;凡是對(duì)表進(jìn)行修改都是alter table
具體操作:向useinfo表里添加一個(gè)備注字段
alter table usreinfo add remarks varchars(500);
2.更改字段的數(shù)據(jù)類型:
alter table table_name modify column_name datatype;
操作:修改數(shù)據(jù)類型的長(zhǎng)度,更換數(shù)據(jù)類型
alter table userinfo modify remarks varchar2(400);注意:如果在一個(gè)表當(dāng)中已經(jīng)存在了數(shù)據(jù),則不能修改,只有在數(shù)據(jù)為空的情況下才能進(jìn)行更改)。
alter table userinfo modify userpwd number(6,0);修改密碼字段的數(shù)據(jù)類型
3.刪除字段:
alter table table_name drop column column_name;
具體操作:alter table userinfo drop column remarks;
4.修改字段名:
alter table table_name rename column column_name to new_column_name;
具體操作:alter table useinfo rename column email to new_email;
5.修改表名:
rename table_name to new_table_name;
rename userinfo tp new_userinfo;
4.刪除表:
truncate table table_name;(刪除表當(dāng)中的全部數(shù)據(jù),也叫截?cái)啾恚萪elete速度快)
truncate table new_userinfo;刪除表里的數(shù)據(jù),表的結(jié)構(gòu)仍然存在。
drop table table_name;刪除整張表的結(jié)構(gòu):
drop table new_userinfo;刪除表不存在了。
操作表中的數(shù)據(jù):
1.添加數(shù)據(jù):
insert語(yǔ)句:
insert into table_name
(column1,column2,...)
values(value1,value2,...)
操作實(shí)例:
1.向表中所有字段添加值:
insert into userinfo values(1,'xxx','123','xxx@126.com',sysdate);向表中插入一條數(shù)據(jù),sysdate函數(shù)獲取當(dāng)前系統(tǒng)時(shí)間。
查看該條數(shù)據(jù):select * from userinfo;
2.向表中指定字段添加值:
insert into userinfo(id,username,userpwd) values(2,'yyy','123');
查看數(shù)據(jù):select username,userpwd from userinfo;
3.向表中添加默認(rèn)值:
a.創(chuàng)建表時(shí)就設(shè)置;create table userinfo1(id number(6,0),regdate date default sysdate);
插入數(shù)據(jù)時(shí):insert into userinfo1 (id) values(1);則regdate字段會(huì)自動(dòng)補(bǔ)上。
查詢:select * from userinfo1;
修改userinfo表email字段
b.已經(jīng)創(chuàng)建好表,后來(lái)再修改成default
更改字段的數(shù)據(jù)類型:alter table userinfo modify email default '無(wú)';
再向表中插入數(shù)據(jù):insert into userinfo (id) values(2);
查看:select email from useinfo;
注意:字符型加單引號(hào);如果在創(chuàng)建表市,某些字段不能為空時(shí)添加值時(shí)需不為空。
2.復(fù)制表數(shù)據(jù):
1.在建表時(shí)復(fù)制:
create table table_new as select column1,...|*from table_old;
create table userinfo_new as select * from userinfo;全部復(fù)制
create table userinfo_new1 as select id,username from userfino;復(fù)制部分
2.在添加時(shí)復(fù)制:
insert into table_new [(column1,..)] select column1,...| * from table_old;
表已經(jīng)存在,在添加數(shù)據(jù)時(shí)復(fù)制數(shù)據(jù):
insert into userinfo_new select * from userinfo;復(fù)制userinfo表的全部數(shù)據(jù)
查看:select if from userinfo_new;
inert into uerinfo_new(id,username) select id,username from userinfo;復(fù)制userinfo表的部分?jǐn)?shù)據(jù)
3.修改數(shù)據(jù):
update語(yǔ)句:
update table_name set column1=value1,...[where cinditions];修改的條件,如果不加where condition則會(huì)給所有的行更改
操作實(shí)例:
無(wú)條件的更新:
update userinfo set userpwd='111111';一個(gè)字段的更改
select userpwd from userinfo;
update userinfo set userpwd='111',email='111@126.com';多個(gè)字段的更改
select userpwd,email from userinfo;
有條件的更新:
用戶名是xxx的密碼改為:123456
update userinfo set userpwd='123456' where username='xxx';
select userpwd from userinfo;
4.刪除數(shù)據(jù):
以行為單位來(lái)進(jìn)行刪除。
delete from table_name; truncate table截?cái)啾?和delete from table_name效果一樣
操作實(shí)例:
無(wú)條件的刪除:
先創(chuàng)建刪除表:create table testdel1 as select * from userinfo;
刪除表;delete from testdel1;
查看:select * from testdel1;結(jié)果:未選定行,刪除成功。
有條件的刪除:刪除用戶名是yyy的用戶:
先查看yyy用戶是否存在:select username from userinfo;
刪除用戶名yyy:detele from userinfo where username='yyy';
約束:數(shù)據(jù)類型是控制輸入的值的類型,約束是控制你輸入的值要滿足你設(shè)定好的一些要求的。
1.約束的作用:
1.定義規(guī)則
2.確保完整性
2.oracle中的5個(gè)約束:
1.非空約束
1.在創(chuàng)建表時(shí)設(shè)置非空約束:
create table table_name (column_name datatype not null,...);
create table userinfo_1(id number(6,0),username varchar2(20) not null, userpwd varchar2(20) not null);
2.在修改表時(shí)添加非空約束:
alter table table_name modify column_name dtatype not null;
給userinfo添加非空約束:alter table userinfo modify username varchar2(20) no tnull;報(bào)錯(cuò),因?yàn)楸韮?nèi)有數(shù)據(jù)。
此時(shí)需要把表內(nèi)數(shù)據(jù)刪除才能添加約束:delete from userinfo;
再執(zhí)行: alter table table_name modify column_name dtatype not null;添加約束,就會(huì)成功。
3.在修改表時(shí)去除非空約束:
alter table table_name modify column_name datatype null;
alter table table_name modify column_name dtatype null;
2.主鍵約束
作用:確保表當(dāng)中每一行數(shù)據(jù)的唯一性(非空,唯一)一張表里只能設(shè)置一個(gè)主鍵約束,但是這個(gè)主鍵約束可以由多個(gè)字段構(gòu)成(聯(lián)合主鍵或復(fù)合主鍵)
1.在創(chuàng)建表的時(shí)候設(shè)置主鍵約束:
列集:
create table table_name ( column_name datatype primary key,...)
create table userinfo_p
(id number(6,0) primary key,
username varchar2(20),
userpwd varchar2(20)
); 主鍵約束默認(rèn)是not null;
表集:
聯(lián)合主鍵:(在所有的字段寫完后,再寫此句約束)表集約束
constraint constraint_name primary key(column_name1,...)
create table userinfo_p1
(id number(6,0),
username varchar2(20),
userpwd varchar2(20),
constraint pk_idusername primary key(id,username));
結(jié)果表userinfo_p1的id 和username都為not null.
desc user_constraints;通過(guò)這個(gè)數(shù)據(jù)字典來(lái)查詢約束的名字,類型等
例如查約束的名字:
select constraint_name from user_constraints where table_name=' userinfo_p1';
查詢到的主鍵約束名字為:pk_idusername
之前給userinfo_p的id字段設(shè)置主鍵約束但并未設(shè)置名字,而是由系統(tǒng)生成的, select constraint_name from user_constraints where table_name=' userinfo_p';結(jié)果為:SYS_C0010836
2.在修改表的時(shí)候添加主鍵約束:
ADD CONSTRAINT constraint_name primary key(column_name1,...);
alter table userinfo
add constraint pk_id primary key(id);
如果表當(dāng)中已經(jīng)存在值了,值必須得是唯一的,不能為空值;最好的在添加主鍵時(shí)表內(nèi)沒(méi)有值。
select constraint_name from user_constraints where table_name=' userinfo';結(jié)果:pk_id
3.更改約束的名稱:
rename constraint old_name to new_name
alter table userinfo
rename constraint pk_id to new_pk_id;
可以更改主鍵約束的名字,也可以更改任何一個(gè)約束的名字。
4.刪除主鍵約束:
1.禁用|啟用:
disable|enable constraint constraint_name
操作:alter table userinfo
disable constriant new_pk_id;
查看禁用的約束:
select constraint_name,status from user_constraints where table_name='USERINFO';
2.drop constraint constraint_name完全刪除約束
操作:alter table userinfo
drop constraint new_pk_id;
3.drop primary key[cascade],由于每張表只有一個(gè)主鍵約束,cascade是表存在及聯(lián)約束時(shí),刪除其余表對(duì)這表的外鍵約束
3.外鍵約束
1.在創(chuàng)建表時(shí)設(shè)置外鍵約束:
列集:
create table table1
(column_name datatype references
table2(column_name),...);
table2是主表,table1是從表;設(shè)置外鍵約束時(shí),主表的字段必須是主鍵,主從表中相應(yīng)的字段必須是同一個(gè)數(shù)據(jù)類型;從表中外鍵字段的值必須來(lái)自主表中的相應(yīng)字段的值,或者為null的值
操作:
1.先創(chuàng)建主表:(用戶類型表)
create table typeinfo
(typeid varchar2(10) primary key,
typename varchar2(20));
2.創(chuàng)建從表:
create table userinfo_f
(id varchar2(10) primary key,
username varchar2(20),
typeid_new varchar2(10) references typeinfo(typeid));
3.驗(yàn)證向從表當(dāng)中的typeid的來(lái)源要么是主表當(dāng)中的值要么是空值。
insert into typeinfo values(1,1);
insert into userinfo_f(id,typeid_new) values(1,2)報(bào)錯(cuò)
insert into userinfo_f(id,typeid_new) values(1,1); ok了
插入空值insert into userinfo_f(id,typeid_new) values(2,null); ok了
表集:
關(guān)鍵字? ? ? ? ? ? ? ? ? ? 外鍵約束關(guān)鍵字
constraint constraint_name foreign key(column_name) references table_name(column_name) [ON DELETE CASCADE]及聯(lián)刪除:主表當(dāng)中的數(shù)據(jù)刪除之后,從表當(dāng)中使用了這條數(shù)據(jù)的字段也會(huì)被刪除。確保了主從表數(shù)據(jù)的完整性。約束的名字是唯一的
create table userinfo_f2
(id varchar2(10) primary key,
username varchar2(20),
typeid_new varchar2(10),
constraint fk_typeid_new foreign key(typeid_new) references typeinfo(typeid));
操作:
create table userinfo_f3
(id varchar2(10) primary key,
username varchar2(20),
typeid_new varchar2(10),
constraint fk_typeid_new1 foreign key(typeid_new) references typeinfo(typeid) on delete cascade);
2.在修改表時(shí)添加外鍵約束:
add constraint constraint_name foreign key(column_name) references table_name(column_name) [on delete cascade]
操作:
1.創(chuàng)建表:create table userinfo_f4
(id varchar2(10) primary key,
username varchar2(20),
typeid_new varchar2(10));
2.更改表:
add constraint fk_typeid_alter foreign key(typeid_new)references typeinfo(typeid);
3.刪除外鍵約束:
1.禁用|啟用:
disable|enable constraint constraint_name
因?yàn)橥浟送怄I約束的名字,所以先查找外鍵約束的名字:
select constraint_name,constraint_type,status from user_constraints where table_name='USERINFO_F4';
結(jié)果:一個(gè)P,主鍵約束;一個(gè)R,外鍵約束,名字為:FK_TYPEID_ALTER
操作:alter table userinfo
disable constraint FK_TYPEID_ALTER;
驗(yàn)證是否禁用:
select constraint_name,status from user_constraints where table_name='USERINFO_F4';
2.徹底刪除:
drop constraint constraint_name完全刪除約束
操作:alter table userinfo
drop constraint FK_TYPEID_ALTER;
4.唯一約束
作用:保證字段值的唯一性
主鍵約束作用是保證值的唯一性;唯一約束和主鍵約束的區(qū)別:主鍵字段必須是非空的,而唯一約束則可以為空值;每張表的主鍵約束只能有一個(gè),而唯一約束卻可以有多個(gè)。
1.在創(chuàng)建表時(shí)設(shè)置唯一約束:
列集:
create table table_name
(column_name datatype UNIQUE,...)
操作:create table userinfo_u
(id varchar2(10) primary key,
username varchar2(20) unique,
userpwd varchar2(20));
表集:
constraint constraint_name unique(column_name),這句話放在所有字段定義完成之后。
要將多個(gè)字段設(shè)置成唯一的約束,需要寫多個(gè)唯一約束語(yǔ)句。
操作:create table userinfo_u1
(id varchar2(10) primary key,
username varchar2(20),
constraint un_username unique(username));
2.在修改表時(shí)添加唯一約束:
add constraint constraint_name unique(column_name);
操作:1.創(chuàng)建表:create table userinfo_u2
(id varchar2(10) primary key,
username varchar2(20));
2.添加唯一約束:
alter table userinfo_u2 add constraint un_username_new unique(username);
3.刪除唯一約束:
1.暫時(shí)禁用:
disable|enable constraint constraint_name
查看約束的名字,類型,狀態(tài):
select constraint_name,constrint_type,status from user_constraints where table_name=' userinfo_ u2';
結(jié)果為: 一個(gè)u 一個(gè)p.
alter table userinfo_u2
disable constraint UN_USENAME_NEW;
2.徹底刪除:
drop constraint constraint_name
5.檢查約束
作用:讓表中的數(shù)據(jù)更具有實(shí)際意義
員工個(gè)人信息:年齡,工資,電話號(hào)碼
50? ? 1000? 134XXXXX
1.在創(chuàng)建表時(shí)設(shè)置檢查約束:
在一個(gè)表當(dāng)中也可以有多個(gè)。
列集:
create table table_name (column_name datatype check(expressions),...)
操作:create table userinfo_c
(id varchar2(10) primary key,
username varchar2(20),
salary number(5,0) check(salary>0));
這樣就不能往里插負(fù)數(shù)的工資數(shù)據(jù)了。
表集:
constraint constraint_name check(expressions);通常以ck,chk開頭
操作:
create table userinfo_c1
(id varchar2(10) primary key,
username varchar2(20),
salary number(5,0),
constraint ck_salary check(salary>0));
2.在修改表時(shí)添加檢查約束:
add constraint constraint_name check(expressions);
操作:create table userinfo_c3
(id varchar2(10) primary key,
username varchar2(20),
salary number(5,0));
alter table userinfo_c3
add constraint ck_salary_new check(salary>0);
3.刪除檢查約束:
1.暫時(shí)禁用:
disable|enable constraint constraint_name
2.徹底刪除:
drop constrint constraint_name
約束小結(jié):1.只有主鍵約束在每張表里面只能有一個(gè),可以由多個(gè)字段構(gòu)成。
2.外鍵約束時(shí)唯一一個(gè)涉及兩張表之間關(guān)系的約束
3.在創(chuàng)建表時(shí)設(shè)置約束:非空約束只能在列級(jí)設(shè)置,不能在表級(jí)設(shè)置,并且設(shè)置非空約束時(shí),非空約束沒(méi)有名字。
4.在修改表時(shí)添加約束:在修改表的時(shí)候添加非空約束實(shí)際上用的是:修改字段的語(yǔ)句,為:alter table table_name modify column_name datatype not null;
5.更改約束的名稱:非空約束沒(méi)有名字,不能使用該語(yǔ)句
1.不知道約束名稱時(shí),可以更加數(shù)據(jù)字典來(lái)查看:user_constraints
2.rename constraint old_name to new_name 此語(yǔ)句就是更改約束名稱的語(yǔ)句
6.刪除約束:1.alter table table_name modify column_name datatype null;
2.禁用:disable | enable constraint constraint_name(放在alter table之后出現(xiàn))
3.徹底刪除:drop constraint constraint_name
4.刪除主鍵約束:drop primary key
查詢:
1.基本查詢語(yǔ)句:
select [distinct] column_name1,...|* from table_name [where conditions]
2.在SQL*PLUS中設(shè)置格式:設(shè)置查詢結(jié)果的顯示格式
1.更改查詢后結(jié)果的字段名:column column_name heading new_name; (column可以簡(jiǎn)寫成col)
操作:設(shè)置新的字段名:col username heading 用戶名;
2.column column_name format dataformat; (注意:字符類型只能設(shè)置顯示的長(zhǎng)度)
操作:把用戶名字段的數(shù)值長(zhǎng)度改為10:
col username format a10;(a代表字符型,10代表10個(gè)長(zhǎng)度)
把工資字段設(shè)置為顯示一位小數(shù):
col salary format 9999.9;(9代表一個(gè)數(shù)值)
把工資格式改為美元符號(hào)開頭:
col salary format $9999.9;
3.清除設(shè)置的格式:col column_name clear;
3.查詢表中的所有字段及指定字段:
1.查詢所有字段:select * from table_name;
操作:select * from users;
2.查詢部分字段:select column1_name,column2_name from table_name;
4.給字段設(shè)置別名? :(注意:是針對(duì)查詢結(jié)果進(jìn)行的,并沒(méi)有更改字段的名字)
可以一次為多個(gè)字段設(shè)置別名
select column_name as new_name,... from table_name;(注意:as可以省略,用空格隔開原來(lái)的字段名和新名字即可)
操作:select id as 編號(hào),username as 用戶名,salary 工資 from users;
通過(guò)distinct去除重復(fù)的用戶名:
select distinct username as 用戶名 from users;
5.運(yùn)算符和表達(dá)式:
表達(dá)式=操作數(shù)+運(yùn)算符
操作數(shù):變量,常量,字段;
運(yùn)算符:算術(shù)運(yùn)算符:+ - * /
比較運(yùn)算符:>, >=, <, <=, =, <>
邏輯運(yùn)算符:and or not (not是非)
6.在select語(yǔ)句中使用運(yùn)算符:(只影響查詢結(jié)果,不影響實(shí)際表中的數(shù)據(jù),要真的改表中的數(shù)據(jù),需要使用update語(yǔ)句)
使用算術(shù)運(yùn)算符:select id,username,salary+200 from users;(給工資加200元)
使用比較運(yùn)算符:select username from users where salary>800;(查詢工資超過(guò)800元的人)
使用邏輯運(yùn)算符:select username from users where salary>800 and salary<>1500;(查詢工資高于800,但不等于1500的人);
7.帶條件的查詢:
1.單一條件查詢: select salary from users where username='aaa';
2.多條件的查詢: 使用邏輯運(yùn)算符連接語(yǔ)句 select * from users where username='aaa' or salary>2000;
select * from users where username='aaa' or (salary>800 and salary<=2000);
邏輯運(yùn)算符的有限性:not > and > or; 比較運(yùn)算符 >邏輯運(yùn)算符
select * from users where not (username='aaa');
8.模糊查詢:
like
通配符的使用:_ %(一個(gè)_只能代表一個(gè)字符,%可以代表0到多個(gè)任意字符)
操作:查找以a開頭的用戶:select * from users where username like 'a%';
查找含有a的用戶名:select * from users where username like '%a%';
9.范圍查詢:
between...and(從什么值到什么值)
操作:查詢工資從800到2000的用戶:select * from users where salary between 800 and 2000;(是閉合區(qū)間)
in/not in (后面的值不是范圍,而是一個(gè)具體的值)查詢的值等于什么,不等于什么。
select * from users where username in ('aaa','bbb');
10.對(duì)查詢結(jié)果排序:
select... from...[where...] order by column1 desc/asc,...
操作:select * from users order by username desc,salary asc;(用戶名)
11.case...when語(yǔ)句使用:
1. case column_name
when calue1 then result1,...
[else result] end
操作:select username, case username when 'aaa' then '計(jì)算機(jī)部門'
when 'bbb' then '市場(chǎng)部門' else '其他部門' end as 部門
from users;
2. case
when column_name=value1
then result1,...[else result] end
操作: select username,case when username='aaa' then '計(jì)算機(jī)部門'
when username='bbb' then '市場(chǎng)部' else '其他部門' end as 部門
from users;
select username,case when salary<800 then '工資低'
when salary>5000 then '工資高' end as 工資水平
from users;
12.decode函數(shù)的使用:
decode (column_name,value1,result1,...,defaultvalue)
操作: select username, decode(username,'aaa','計(jì)算機(jī)部門','bbb','市場(chǎng)部門','其他')as 部門
from users;
總結(jié):
1.用戶表空間:
1.如何查看登錄用戶:show user命令 dba_users數(shù)據(jù)字典
2.啟用scoot用戶
3.如何查看某個(gè)用戶的默認(rèn)表空間和臨時(shí)表空間
4.表空間管理:創(chuàng)建,修改,刪除表空間
2.表與約束:
1.數(shù)據(jù)類型:char(n) nchar(n) varchar2(n) nvarchar2(n) ;數(shù)值型:number(p,s) float(n) ;日期型:date,timestamp ;其他類型:blob,clob
2.對(duì)表的創(chuàng)建,修改,刪除
3.對(duì)表中數(shù)據(jù)的操作:添加數(shù)據(jù)insert 修改數(shù)據(jù)update 刪除數(shù)據(jù):delete
4.5個(gè)約束:非空約束,主鍵約束,外鍵約束,唯一約束,檢查約束
5.查詢語(yǔ)句:查詢所有字段和指定字段;在查詢語(yǔ)句中使用運(yùn)算符和表達(dá)式;在查詢語(yǔ)句中加入where;范圍查詢;模糊查詢:like關(guān)鍵字,通配符;case...when語(yǔ)句和Decode函數(shù)