建立資料表空間
create tablespace l_ldatafile 'D:luo.DBF'size 5mautoextend on next 4mmaxsize 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,validatedfrom user_constraintswhere 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:用於對查詢結果的限制