標籤:完全 資料 刪除 _id 欄位 uid 外鍵 之間 drop
select * from stuent;
alter table student add stu_card char(18)
--主鍵約束:非空,唯一 primary key
alter table student add constraint
pk_stuid primary key(stu_id)
--檢查約束 check ck_xxx
alter table student add constraint
ck_stusex check(stu_sex=‘男‘ or stu_sex=‘女‘)
--唯一約束:不重複(只能允許一個為空白) unique
alter table student add constraint
up_stucard unique(stu_card)
--預設約束 default
alter table student modify
stu_birth default sysdate
--非空約束 not all
alter table student modify(stu_name not null)
-主外鍵約束
alter table stu_score add constanint fk_stuid
foreign key(stu_id) references student(stu_id);
create table stu_score(
stu_id number,
stu_score number(5,1)
);
--刪除表 (先刪除外鍵表,再刪除主鍵表)
drop table student;
1.每張表必須有主鍵,且為原子列(每個欄位不可再分)
2.非主鍵列完全依賴與主鍵列
3.非主鍵列不能依賴與非主鍵列
DML語句:
--增加資料 insert into table_name
insert into student
(stu_id,stu_name,stu_birth,stu_card)
values
(1,‘張三丰‘,‘1-10月-1990‘,‘123‘);
insert into student
values(2,‘梅超風‘,default,‘123456‘,‘男‘);
select * from student;
commit;
--修改資料 update table_name set 欄位名=值;
update student set stu_sex = ‘女‘ where stu_id = 2;
--刪除資料 delete 表明
delete from student where stu_id = 1;
--查詢資料 select
select
from
group by
having
order by
select * from emp
select * from dept
--複製表
create table emp_temp as select * from emp;
select * from emp_temp
--查詢員工的編號,姓名,工作崗位
select empno,ename,job from emp_temp;
--查詢20部門的員工資訊
select * from emp_temp where deptno = 20;
select empno as 員工編號,ename 員工姓名
from emp_temp;
--查詢員工工資大於3000的員工資訊
select * from emp_temp where sal > 3000;
--查詢不是30部門的員工資訊
select * from emp_temp where deptno <> 30;
--查詢20部門的員工資訊,以下列格式顯示
XXX的薪水是XXX
selsect ename || ‘的薪水是;’|| sal
from emp_temp;
--查詢所有員工的月收入
select empno,ename,sal,comm,
sal+nvl(comm,0) 月收入
from emp_temp;
--模糊查詢
select * from emp_temp
where ename like ‘%A%‘; %是可出現可不出現的,可出現多個
--查詢第二個字元是A的,_是預留位置,一定出現的
select * from emp_temp
where ename like ‘_A%‘;
--查詢員工的薪水在3000-5000之間的[]
select * from emp_temp
where sal between 3000 and 5000;
不在3000和5000的
select * from emp_temp
where sal not between 3000 and 5000;
--in
selsect * from emp_temp
where deptno not in (20,30);
--查詢那些員工沒有獎金
select * from emp_temp
where comm is null;
select * from emp_temp
where comm is not null
Oracle資料庫學習筆記3