第二章 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;


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值