1.显示系统日期
select sysdate from dual;
2.解锁用户
首先使用管理员登陆
sqlplus sys/xiaoyaozi as sysdba alter user scott account unlock
3.使用PL/SQL输出字符串
SQL> set serveroutput on SQL> begin 2 dbms_output.put_line('这是第一个PL/SQL程序'); 3 end; 4 / 结果: 这是第一个PL/SQL程序
4.Oracle基本数据类型
5.定义表结构
①定义学生表students
SQL> create table students( 2 sid number(5) 3 constraint student_pk primary key, 4 mid number(5), 5 name varchar(10) not null, 6 sex varchar(2) default '男' 7 constraint sex_chk check (sex in ('男','女')), 8 dob date, 9 specialty varchar(10) 10 );
②定义department表
SQL> create table department( 2 did number(5) 3 constraint depart_pk primary key, 4 dname varchar(8) not null, 5 address varchar(30) 6 );
③定义教师表teachers
SQL> create table teachers( 2 tid number(5) 3 constraint teacher_pk primary key, 4 name varchar(8) not null, 5 title varchar(6), 6 hiredate date default sysdate, 7 bonus number(7,2), 8 wage number(7,2), 9 did number(5) 10 constraint teach_fk_depart references department(did) 11 );
⑤定义课程cources表
SQL> create table cources( 2 cid number(5) 3 constraint course_pk primary key, 4 cname varchar(30) not null, 5 chour number(2) 6 );
⑥定义学生成绩stu_grade表
SQL> create table stu_grade( 2 sid number(5) 3 constraint stu_grade_fk_students references students(sid), 4 cid number(5) 5 constraint stu_grade_fk_cource references cources(cid), 6 score number(4,1) 7 );
6,查看表的结构:
describe <table_name> 或 desc <table_name> 例: SQL> desc students; 名称 是否为空? 类型 ----------------------------------------------------- -------- ------------- SID NOT NULL NUMBER(5) MID NUMBER(5) NAME NOT NULL VARCHAR2(10) SEX VARCHAR2(2) DOB DATE SPECIALTY VARCHAR2(10)
7.添加数据
insert into <table_name>[column name] values (value1,value2,......,value);
插入学生表记录: INSERT INTO students VALUES(10101,NULL,'王晓芳', '女', '07-5月-1988','计算机'); INSERT INTO students VALUES(10205,NULL,'李秋枫', '男', '25-11月-1990','自动化'); INSERT INTO students VALUES(10102,10101,'刘春苹', '女', '12-8月-1991','计算机'); INSERT INTO students VALUES(10301,NULL,'高山', '男', '08-10月-1990','机电工程'); INSERT INTO students VALUES(10207,10205,'王刚', '男', '03-4月-1987','自动化'); INSERT INTO students VALUES(10112,10101,'张纯玉', '男', '21-7月-1989','计算机'); INSERT INTO students VALUES(10318,10301,'张冬云', '女', '26-12月-1989','机电工程'); INSERT INTO students VALUES(10103,10101,'王天仪', '男', '26-12月-1989','计算机'); INSERT INTO students VALUES(10201,10205,'赵风雨', '男', '25-10月-1990','自动化'); INSERT INTO students VALUES(10105,10101,'韩刘', '男', '3-8月-1991','计算机'); INSERT INTO students VALUES(10311,10301,'张杨', '男', '08-5月-1990','机电工程'); INSERT INTO students VALUES(10213,10205,'高淼', '男', '11-3月-1987','自动化'); INSERT INTO students VALUES(10212,10205,'欧阳春岚', '女', '12-3月-1989','自动化'); INSERT INTO students VALUES(10314,10301,'赵迪帆', '男', '22-9月-1989','机电工程'); INSERT INTO students VALUES(10312,10301,'白菲菲', '女', '07-5月-1988','机电工程'); INSERT INTO students VALUES(10328,10301,'曾程程', '男', NULL,'机电工程'); INSERT INTO students VALUES(10128,10101,'白昕', '男', NULL, '计算机'); INSERT INTO students VALUES(10228,10205,'林紫寒', '女', NULL, '自动化');
插入department数据: INSERT INTO department VALUES(101,'信息工程','1号教学楼'); INSERT INTO department VALUES(102,'电气工程','2号教学楼'); INSERT INTO department VALUES(103,'机电工程','3号教学楼'); INSERT INTO department VALUES(104,'工商管理','4号教学楼');
插入teachers表数据: INSERT INTO teachers VALUES(10101,'王彤', '教授', '01-9月-1990',1000,3000,101); INSERT INTO teachers VALUES(10104,'孔世杰', '副教授', '06-7月-1994',800,2700,101); INSERT INTO teachers VALUES(10103,'邹人文', '讲师', '21-1月-1996',600,2400,101); INSERT INTO teachers VALUES(10106,'韩冬梅', '助教', '01-8月-2002',500,1800,101); INSERT INTO teachers VALUES(10210,'杨文化', '教授', '03-10月-1989',1000,3100, 102); INSERT INTO teachers VALUES(10206,'崔天', '助教', '05-9月-2000',500,1900, 102); INSERT INTO teachers VALUES(10209,'孙晴碧','讲师', '11-5月-1998',600,2500, 102); INSERT INTO teachers VALUES(10207,'张珂', '讲师', '16-8月-1997',700,2700, 102); INSERT INTO teachers VALUES(10308,'齐沈阳', '高工', '03-10月-1989',1000,3100, 103); INSERT INTO teachers VALUES(10306,'车东日', '助教', '05-9月-2001',500,1900, 103); INSERT INTO teachers VALUES(10309,'臧海涛','工程师', '29-6月-1999',600,2400, 103); INSERT INTO teachers VALUES(10307,'赵昆', '讲师', '18-2月-1996',800,2700, 103); INSERT INTO teachers VALUES(10128,'王晓', NULL,'05-9月-2007',NULL,1000, 101); INSERT INTO teachers VALUES(10328,'张笑', NULL, '29-9月-2007',NULL,1000, 103); INSERT INTO teachers VALUES(10228,'赵天宇', NULL, '18-9月-2007',NULL,1000, 102); INSERT INTO teachers VALUES(11111,'林飞', NULL, '11-10月-2007',NULL,1000, NULL);
插入cources表数据: INSERT INTO cources VALUES(10101,'计算机组成原理',4); INSERT INTO cources VALUES(10201,'自动控制原理',4); INSERT INTO cources VALUES(10301,'工程制图',3); INSERT INTO Cources VALUES(10102,'C++语言程序设计',3); INSERT INTO cources VALUES(10202,'模拟电子技术',4); INSERT INTO cources VALUES(10302,'理论力学',3); INSERT INTO cources VALUES(10103,'离散数学',3); INSERT INTO cources VALUES(10203,'数字电子技术',4); INSERT INTO cources VALUES(10303,'材料力学',3);
插入stu_grade表数据: INSERT INTO stu_grade VALUES(10101,10101,87); INSERT INTO stu_grade VALUES(10101,10201,100); INSERT INTO stu_grade VALUES(10101,10301,79);
7.显示所有数据:
select * from <table_name> 或select <column_name> from <table_name> SQL> select * from stu_grade; SID CID SCORE ---------- ---------- ---------- 10101 10101 87 10101 10201 100 10101 10301 79
8.删除数据:
delete from <table_name> [ where <condition> ]
9.NVL函数:
SQL> select name 姓名,NVL(bonus,0)+wage 薪水 from teachers; 或者: SQL> select name 姓名,NVL2(bonus,bonus+wage,wage) 薪水 from teachers; 或者: (返回第一个非空值) SQL> select name 姓名,COALESCE(bonus+wage,wage) 薪水 from teachers; 姓名 薪水 -------- ---------- 王彤 4000 孔世杰 3500 邹人文 3000 韩冬梅 2300 杨文化 4100 崔天 2400 孙晴碧 3100 张珂 3400 齐沈阳 4100 车东日 2400 臧海涛 3000 赵昆 3500 王晓 1000 张笑 1000 赵天宇 1000 林飞 1000