1. 程式人生 > >建立表空間——建立使用者——建立表物件

建立表空間——建立使用者——建立表物件

建立表空間——建立使用者——建立表物件

--建立表空間
create tablespace abc表空間名
datafile 'c:\...'表空間儲存路徑
size 20M
autoextend on;
--刪除表空間
drop tablespace abc表空間名 including contents and datafiles;

-------------------------------------------------------------------

--建立使用者
create user java12使用者名稱
identified by java12密碼
default tablespace abc預設表空間名


temporary tablespace temp; --臨時表空間

--給使用者許可權
grant dba to java12 with admin option;--dba許可權(高階)
grant connect to java12;--連線許可權
--回收使用者許可權
revoke dba from java12;

--使用者加鎖
alter user java12 account lock;
--使用者解鎖
alter user java12 account unlock;

-------------------------------------------------------------------

/*
建立表

create table 自定義表名(
    列名1 對應的資料型別,
    列名2 對應的資料型別,
    列名3 對應的資料型別,
    ...
);
*/
create table tb_urse(
    username varchar2(20),
    sex char(2),
    age int
);

-------------------------------------------------------------------

--columns 屬性名(列)

--row 記錄的資料,屬性值(行)
--給表名或者屬性名加註解、說明性文字
comment on table tb_user is '使用者表';
comment on column tb_user.username is '使用者名稱';


/*在現有表中新增屬性(列)
alter table 表名 add(
    新列名1 對應的資料型別,
    新列名2 對應的資料型別,
    ...
);
*/
alter table tb_user add(
    address varchar(50),
    phone varchar(11)
)
/*修改現有表中的屬性名(列名)
alter table 表名 rename column 舊列名 to 新列名;
*/
alter table tb_user rename column phone to tel;
/*修改現有表中的列對應的資料型別
alter table 表名 modify(列名 新的資料型別);
注意:同資料型別間可以轉換,不同資料型別之間轉換時對應資料必須為空
*/
alter table tb_user modify(address varchar2(80));
/*刪除現有表中的列
alter table 表名 drop(列名1,列名2);
*/
alter table tb_user drop(address,tel);

-------------------------------------------------------------------

--修改表名
rename tb_user to tb_person;
--截斷表:保留表結構,只刪除表資料
truncate table tb_person;
--刪除表:表結構和表資料一起,全部刪除
drop table tb_user;

-------------------------------------------------------------------

/*
新插入一條資料
insert into 表名(列名1,列名2,...)
values('屬性值1','屬性值2',...)
*/
insert into tb_user(username, sex, age)
values('張三','男',18);

-------------------------------------------------------------------

--Delete 資料刪除語句
--1.刪除表中所有資料
delete from tb_user;
--2.刪除表中 username = '張三' 的使用者資料
delete from tb_user
where username = '張三';
--3.刪除 姓名 = '李四' 並且 性別 = '男' 的使用者資料
delete from tb_user
where username = '李四'
and sex = '男';
--4.刪除 姓名 = '王五' 或者 性別 = '男' 的使用者資料
delete from tb_user
where username = '王五'
or sex = '男';

-------------------------------------------------------------------

--Update 資料更新、修改語句
--1.將所有使用者的年齡改為30(慎用)
update tb_user set age = 30;
--2.修改 username = '趙六' 的年齡為18
update tb_user
set age = 18
where username = '趙六';
--3.將username = '趙六' 的 sex 改為 男,age 改為 19
update tb_user
set sex = '男',age = 19,
where username = '趙六';
--4.將username = '趙六' 並且 sex = '男' 的使用者 age 改為 22
update tb_user
set age = 22
where username = '趙六'
and sex = '男';
--5.將username = '趙六' 或者 sex = '男' 的使用者 age 改為 22
update tb_user
set age = 22
where username = '趙六'
or sex = '男';

-------------------------------------------------------------------

/*
資料提交
oracle 資料庫需要手動提交資料事務
MySQL 資料庫會自動提交資料事務
*/
--提交資料
commit;
--回滾資料
rollback;

-------------------------------------------------------------------

關鍵單詞
create    建立
grant    賦予、給予
revoke    回收

delete    表資料刪除
drop    表結構刪除

update    表資料更新、修改
alter    表結構更改

約束
檢查約束 check
非空約束 not null
唯一約束 unique
主鍵     primary key
外來鍵     foreign key    references 外表名(列名)