Oracle数据库常用脚本命令(二)
--创建学生信息表
create table student(
sid number(8,0),
name varchar2(20),
sex char(2),
birthday date,
address varchar2(50)
);
--修改表结构,为学生信息表添加主键约束
alter table student
add constraint sid_pk primary key(sid);
--创建表时添加主键约束
create table student(
sid number(8,0) primary key,
name varchar2(20),
sex char(2),
birthday date,
address varchar2(50)
);
create table student(
sid number(8,0),
name varchar2(20),
sex char(2),
birthday date,
address varchar2(50),
constraint sid_pk primary key(sid)
);
--创建学生信息表
create table student(
sid number(8,0) primary key,
name varchar2(20),
sex char(2),
birthday date,
address varchar2(50)
);
--修改表结构,添加非空约束
ALTER TABLE student MODIFY (name varchar2(20) NOT NULL);
--创建表时添加非空约束
create table student(
sid number(8,0),
name varchar2(20) not null,
sex char(2) constraint nn_sex not null,
birthday date,
address varchar2(50),
constraint sid_pk primary key(sid)
);
--删除非空约束的方式
ALTER TABLE student MODIFY sex char(2) NULL;
--禁用主键约束(无效化约束)
ALTER TABLE student DISABLE CONSTRAINT sid_pk;
--激活主键约束
ALTER TABLE student ENABLE CONSTRAINT sid_pk;
--删除主键约束
ALTER TABLE student DROP CONSTRAINT sid_pk;
ALTER TABLE student DROP PRIMARY KEY;
--创建表时添加唯一性约束
--create table student (
-- sid number(8,0),
-- name varchar2(20),
-- sex char(2),
-- birthday date,
-- address varchar2(50),
-- email varchar2(50) unique,
-- cardid varchar2(18),
-- constraint uk_cardid unique(cardid)
--);
--创建学生信息表
--create table student (
-- sid number(8,0),
-- name varchar2(20),
-- sex char(2),
-- birthday date,
-- address varchar2(50),
-- email varchar2(50) unique,
-- cardid varchar2(18)
--);
--更改表结构,添加唯一性约束
--alter table student add constraint uk_student_cardid unique(cardid);
--禁用约束
alter table student disable constraint uk_student_cardid;
--彻底删除
alter table student drop constraint uk_student_cardid;
--create table student (
-- sid number(8,0),
-- name varchar2(20),
-- sex char(2),
-- birthday date,
-- address varchar2(50)
--);
--修改表添加检查约束的方式
--alter table student add constraint ck_sex check(sex='男' or sex='女');
--创建表时设置检查约束的方式:列级约束
--create table student (
-- sid number(8,0),
-- name varchar2(20),
-- sex char(2) check(sex='男' or sex='女'),
-- birthday date,
-- address varchar2(50)
--);
--创建表时设置检查约束的方式:表级约束
--create table student (
-- sid number(8,0),
-- name varchar2(20),
-- sex char(2),
-- birthday date,
-- address varchar2(50),
-- constraint ck_sex check(sex='男' or sex='女')
--);
--禁用约束
--alter table student disable constraint ck_sex;
--彻底删除
alter table student drop constraint ck_sex;
--主表
--create table department(
-- depid varchar2(10) primary key,
-- depname varchar2(30)
--);
--从表
--create table student (
-- sid number(8,0),
-- name varchar2(20),
-- sex char(2),
-- birthday date,
-- address varchar2(50),
-- depid varchar2(10)
--);
--修改表时添加约束
--alter table student add constraint fk_depid foreign key(depid) references department(depid) on delete cascade;
--创建列级约束
--create table student (
-- sid number(8,0),
-- name varchar2(20),
-- sex char(2),
-- birthday date,
-- address varchar2(50),
-- depid varchar2(10) references department(depid)
--);
--创建表级约束
--create table student (
-- sid number(8,0),
-- name varchar2(20),
-- sex char(2),
-- birthday date,
-- address varchar2(50),
-- depid varchar2(10),
-- constraint fk_depid foreign key(depid) references department(depid) on delete cascade
--);
--禁用约束
--alter table student disable constraint fk_depid;
--彻底删除
alter table student drop constraint fk_depid;
修改表结构:
--添加列
--alter table student add tel varchar2(11);
--修改列
--alter table student modify tel number(11,0);
--删除列
--alter table student drop column tel;
--修改列名
--alter table student rename column gender to sex;
--修改表名
--rename studentinfo to student;
--删除表
--truncate
--truncate table emp_bak;
--drop
--drop table emp_bak;
--insert(添加数据)
--insert into student (sid,name,sex)values(20010001,'张珊珊','女');
--insert into student(sid,name,sex,address,birthday) values(20010002,'王五','男','北京市昌平区',to_date('19820909','YYYYMMDD'));
--insert into student values(20010003,'张三','女',to_date('19830808','YYYYMMDD'),'北京市昌平区');
--select(查询数据)
--select * from student;
--select sid,name from student;
--update(更新数据)
select * from student;
--update student set address = '北京市昌平区' where sid = 20010001;
--update student set address='北京市海淀区';
--delete(删除数据)
--delete from student where sid = 20010001;
delete from student;
--update emp_bak set sal=6000 where empno=7369;
--演示事务的三个语句(commit,savepoint,rollback)的作用
--delete from emp_bak where empno=7369;
--commit;
update emp_bak set sal=5555 where empno=7499;
savepoint a;
update emp_bak set sal=6666 where empno=7499;
savepoint b;
select * from emp_bak;
rollback to a;
rollback;
--查看当前用户下的用户信息
select * from user_users;
--当前用户有权访问的所有用户的基本信息
select * from all_users;
--访问数据库所有用户的用户信息(要求具有dba权限的用户使用此数据字典)
select * from dba_users;
--去除重复行
--select distinct deptno, job from emp;
select * from emp;
--设置简体中文的日期格式
alter session set nls_language = 'SIMPLIFIED CHINESE';
select empno,ename,hiredate from emp;
--设置美国英语的日期格式
alter session set nls_language = 'AMERICAN';
select empno,ename,hiredate from emp;
--设置特定格式
alter session set nls_date_format='YYYY/MM/DD';
select empno,ename,hiredate from emp;
-select * from emp;
-- 算术运算符的演示
--select empno,ename,sal,sal*12 from emp;
--
--select empno, ename, sal*12+sal/2 from emp;
--
--select empno, ename, 200+sal*12 from emp;
--
--select empno, ename, (sal+100)*12 from emp;
--
--select empno, ename, sal, comm, sal+comm from emp;
--默认值的演示
--create table student(
-- sid number(8,0) primary key,
-- name varchar2(10),
-- sex char(2) default '男',
-- age number(2,0) default 20,
-- address varchar2(50)
--);
--insert into student(sid,name) values (20010001,'张三');
select * from student;
insert into student values(20010002,'王五',default,21,'北京市昌平区');
commit;
--列的别名
select empno "雇员编号", ename "雇员名", sal*12 "年收入" from emp;
select empno "雇员编号", ename "雇员名", sal*12 年收入 from emp;
select empno "雇员编号", ename "雇员名", sal*12 as 年收入 from emp;
--连接符
select ename||'的月工资是:'||sal||'岗位是:'||job as 雇员职位信息 from emp;
select ename||5 from emp;
--数字值的情况
select * from emp where deptno=20;
--字符值
select * from emp where job='MANAGER';
--日期值
select * from emp where hiredate = '02-4月-81';
--比较运算
select * from emp where sal = 3000;
select * from emp where sal >= 3000;
select * from emp where sal < 3000;
select * from emp where sal <> 3000;
select * from emp where sal != 3000;
select * from emp where sal>1000 and sal<3000;
select * from emp where sal<1000 or sal>3000;
--范围查询between..and
select empno, ename, sal from emp where sal between 1500 and 3000;
--in
select empno,ename, job from emp where job in ('SALESMAN','MANAGER','CLERK');
--模糊查询like
select * from emp where ename like 'J%';
select * from emp where ename like '_AR%';
select * from emp;
select * from emp where ename like 'G\_%' escape '\';
--is null
select empno, ename, sal, comm from emp where comm is not null;
--and
select empno, ename, job, deptno from emp where job='MANAGER' and deptno=10;
--or
select empno, ename, job, sal from emp where job='MANAGER' or sal>2000;
--not
select empno, ename,job from emp where job not in ('CLERK','SALESMAN','MANAGER');
--混合
select empno,ename, job, sal from emp where (sal>2000 or deptno=30) and job not in ('PRESIDENT','MANAGER');
--单列排序
select ename, comm from emp order by comm asc;
--使用列的别名排序
select empno, ename, sal*12 年收入 from emp order by 年收入 desc;
--多列排序
select empno,ename, deptno, sal from emp order by deptno asc, sal desc;