create table student(
xh number(4), --学号
xm varchar2(20), --姓名
sex char(2), --性别
birthday date, --生日
sal number(7, 2) --薪水
);
--插入数据
--1. 插入整行数据(要求值顺序和列顺序一致)
insert into student values(1, '张三', '男', '01-5月-2005', 10);
--2. 插入指定列
insert into student(xh, xm, sex) values (2,'JOHN', '女');
--3. 插入空值null
insert into student(xh, xm, sex, birthday) values (3, '李四', '男', null);
--注意:查询空值时,不能直接用=号比较,可以使用is null或is not null.
select * from student where birthday is null;
--修改日期的默认格式(临时修改,数据库重启后仍为默认;除非修改注册表)
alter session set nls_date_format = 'yyyy-mm-dd';
--修改数据UPDATE 表名 SET 字段1=值1,字段2=值2 WHERE 条件
update student set sex = '男',birthday = '1984-12-23' where xh = 2;
--删除数据delete, truncate, drop table.
--如果没有where条件,则删除表中所有数据
delete from student where xh = 3;
--创建保存点,可以在delete后回滚回来.
savepoint p1;
delete from student;
rollback to p1;
--使用truncate清空表数据后不能回滚,速度比delete快.
truncate table student;
--drop table则同时删除表结构和表数据,同样不能回滚.
drop table student;
--表查询
--1. 查询所有列
select * from dept;
--2. 查询指定列
select ename, sal, job, deptno from emp;
--3. 如何取消重复行(distinct)
select distinct deptno, job from emp;
--4. 使用算术表达式, 列别名查询
select (sal + nvl(comm, 0)) * 13 as 年薪, ename, comm from emp;
--5. 使用||连接字符串,Where条件筛选数据.
select ename || ' is a ' || job from emp where sal > 3000;
--6. 使用like进行模糊查询(%:表示0到多个字符 _:表示任意单个字符)
select ename, sal from emp where ename like '__O%';
--7. where条件中使用in操作符
select deptno, ename, job from emp where deptno in (10, 20);
--8. 使用逻辑操作符及order by查询
select * from emp where (sal < 1500 or JOB = 'MANAGER') and deptno in(10,20) order by sal;