Oracle總結

創建表空間

create tablespace l_l
datafile 'D:luo.DBF'
size 5m
autoextend on next 4m
maxsize unlimited ;

創建用戶默認表空間

create user luo identified by 123456 default tablespace l_l ;

修改用戶默認空間

alter user luo default tablespace ts_hp;

修改密碼

alter user luo identified by 123;

刪除用戶

drop user luo [cascade];

授會話權

(create session,create table,create index,create view,create sequence,create trriger)各種權限

grant create session to luo;

分配角色(dba,connect,resource)

grant resource to luo;

回收權限

revoke create session from luo;

回收權限

revoke resource from luo;

切換用戶名

conn system/123456;

創建表

create table users(id number);

添加數據

insert into users values(1);

查詢

select*from users;

刪除表

drop table users;

創建表

create table students(
        id number,
        name varchar2(10),
        sex char(2),
        birthday date,
        score number(3,1),
        resume clob);

添加

alter table students add(class_id number);

修改

alter table students modify(name varchar(30));

刪除

alter table student drop column score;
alter table students drop(score);
alter table students drop(score,id);

修改表名

rename students to stu;

修改列名

alter table stu rename column resume to intro;

查看錶結構

desc stu;

添加數據

insert into stu values();

約束

create table class(id number primary key);
create table stu1 ( id number references class(id),
                    name varchar2(10) not null,
                    age number unique,
                    add varchar2(20)check(add in('洛陽','焦作')));

增加 not null

alter table stu modify name not null;

添加或修改

alter table stu add constraint pk_stu primary key (id);

刪除約束

alter table stu drop constraint 約束名稱
alter table stu drop constraint primary key cascade;

約束命名規範

NN_表名_列名:非空
UN_表名_列名:唯一
FK_表名_列名:外鍵
CK_表名_列名:條件
PK_表名:主鍵

顯示約束信息

select constraint_name,constraint_type,status,validated
from user_constraints
where table_name = 'class';

update

語法:update 表名 set 列名 = 表達式[,列名=表達式,…][where 條件];

  update stu set S_name = "qq" where S_id = 1002;
  --注意:沒有where條件。將修改所有行
  update stu set f_ship = 10 where f_ship is null;

delete

語法:delete from 表名 [where 條件表達式];
delete from stu where S_id = 1002;–刪除一行
–注意:沒有where條件,刪除所有記錄;
delete from 表名;刪除數據,還有結構
delete table 表名;刪除結構和數據
trancate table 表名;無法找回

設置保存點

savepoint 保存點名稱
回滾
rollback to 保存點名稱
配合delete使用的,找回delete刪除的數據,而trancate無法找回

查詢

select*from stu;–查詢所有
select S_name,S_id from stu;–查詢特定的列

select distinct S_name from stu ;– 刪除重複數據
select S_name,S_id from stu where S_n = “qq”;–查詢特定條件列

使用算術表達式

用null計算所有都爲空,用nvl(字段,0),nvl函數用於處理null問題
用as如果加引號必須加”“可以只用as不加引號,as不能給表起別名

如何連接字符串(||)

在查詢的時候希望把多列連接成一列返回就用||

to_date
to_char
and
between and

如何使用like操作符

%:任意0個或多個字符
_:表示任意單個字符

order by 排序字句

asc :升序,默認。
desc :降序

可以使用列的別名排序

分組查詢

用到的函數max、min、avg、sum、count。

group by和having

group by:用於對查詢的結果分組
having:用於對查詢結果的限制

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章