第二章 SQL數據庫操作和查詢

一、Oracle數據類型



1、char(10):當數據小於10個字節時自動添加空格補齊,

2、varchar(10):數據小於10個字節時oralce自動判斷識別數據的字節大小

3、number(4,2) :數據必須總長度是4位,小數點後爲2位的數字(不包括小數點)

注意:小數點強制先佔兩位,再看整數位,小數點後超出範圍四捨五入,整數不可以

4、date():儲存日期和時間 如果添加日期沒有時間自動補齊12:00:00



create table student
(
 sid int primary key,
 sname char(10),
 birth varchar(30),
 class int,
);
alter table student modify birth date;


insert into student (sid,birth)values ('13',to_date('2014-7-16','yyyy-mm-dd'));
insert into student(sid,sname)values(9,1)
select * from student


select sysdate,systimestamp from dual;


--default在Oracle是一個值


create table infos
(
 stuid varchar2(7)not null,  --學號
 stuname varchar2(10)not null, --姓名
 gender varchar2(2) not null, -- 性別
 age number(2) not null, --年齡 number爲整數不能有小數點
 seat number(2) not null, --座號
 enrolldate date, --入學時間
 stuaddeess varchar2(50) default'地址不詳', --住址
 classno varchar2(4) not null --班號   =學期序號+班級序號
)
select * from infos
-- 添加主鍵約束
alter table infos add constraint pk_infos primary key(stuid);
-- 添加列約束性別約束爲男女
alter table infos add constraint ck_infos_gender check(gender in('男','女'));
-- 約束座號爲0到50
alter table infos add constraint ck_infos_seat check(seat>=0 and seat<=50);
-- 約束年齡0到100
alter table infos add constraint ck_infos_age check(age between 0 and 100 );
-- 約束 班號
alter table infos add constraint ck_infodfss_classno check((classno between 1 and 1999) or (classno between 1 and 2999));
--注意:如果varchar2()類型中寫數字 最大值不超過10;
--注意:如果varchar2() number()類型中寫數字並取between值時只能默認最小的那個無視最大值但是插入時則按定義的類型和約束插入


create table scores
(
 id number ,
 term varchar2(2), --學期S1或S2
 stuid varchar2(7)not null, -- 學號
 examno varchar2(7)not null, -- 考號 E+班號+序號
 writtenscore number(4,1) not null, -- 筆試成績
 labscore number(4,1) not null --機試成績
)


alter table scores
add constraint ck_score_term check(term='s1' or term='s2')
/
alter table scores
add constraint fk_scores_ifos_stuid foreign key(stuid) references infos(stuid)
/
-- 緩存區就是儲存在內存條裏還未寫入數據庫或者說硬盤的數據 commit;結尾可以將他寫入進去


--向student表中插入一個常量結果集
insert into student 
select '100106','盧俊義',to_date('2009-8-9 8:00:10','yyyy-mm-dd hh24:mi:ss'),2 from dual;


--向student表中插入一個select集






insert into student values
(15,'sd',to_date('2009-8-9 8:00:10','yyyy-mm-dd hh24:mi:ss'),2);


-- 複製表
create table studentab as select * from student;




-- truncate和delete不同之處是 truncate刪除了數據永遠無法找回 刪除速度比delete快


-- 查詢員工基本工資高於兩千的人
select ename,sal,(emp.sal*12+2000) from emp where sal>2000
select *,(emp.sal*12+2000) from emp where sal>2000
--注意:這裏加*無效 *不能和常量在一起出現


-- 字符串聯接
select (ename||'is a'||job)from emp


-- 查看消除重複行
select distinct deptno from emp;


-- 查詢空值
select * from emp where comm is null;
--非空 not null


-- 查出j開頭s結尾的行
select * from emp where Ename like 'J%S';
--注意:數據區分大小寫 列名不區分


-- 補集(返回兩個集合之間所有不相同的數據)
select deptno from dept
minus
select deptno from emp;


-- 並集(返回兩個集合所有的數據並且不能重複)
insert into dept
select 51,'公安部','臺灣'from dual
union
select 61,'研發部','西安'from dual;


-- 交集(返回兩個集合相同的數據 並且不重複)


--內聯接
select e.ename,e.job,e.sal,d.dname from emp e  join dept d on e.deptno=d.deptno where e.sal>2000;
--注意:內聯 on後面可以加 where 只能出現一次 ON是聯接表 where是從已近聯接的表中挑選自己想要的數據




-- +左right +右left
select e.ename,e.job,e.sal,d.dname from emp e  , dept d where e.deptno(+)=d.deptno;


-- 外連接
select e.ename,e.job,e.sal,d.dname from emp e right join dept d on e.deptno=d.deptno;


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