黑马程序员c语言教程:创建和管理表由刀豆文库小编整理,希望给你工作、学习、生活带来方便,猜你可能喜欢“黑马程序员c语言教程”。
创建和管理表
--创建表/修改表/删除表
新创建一个用户的时,要考虑用户有没有 create table权限 和 表的存储空间(表空间)
CREATE TABLE [schema.]table(column datatype [DEFAULT expr][,...]);
--创建表
create table test1(tid number,tname varchar2(20),hiredate date default sysdate);--插入数据
insert into test1(tid, tname)values(1, 'tom');--查看数据
select * from test1;--两个用户之间表查询
--要有查询权限
--oracle的数据类型
--rowid行地址 伪列 rownum select rowid, empno, ename from emp;rowid相当于一个门牌地址,是一个指针;在索引中存放的就是行地址
--创建表的同时copy数据
create table emp20 as select * from emp where deptno=20;
--创建表 员工号 姓名 月薪 年薪 年收入 部门名称
--新创建的表, 要求 部门表/员工表中取数据, 而且还要计算年收入--使用子查询的方式快速建表
--创建视图的语法和这个语法几乎完全一样
create table empincome as select e.empno, e.ename, e.sal, e.sal*12 annalsal, sal*12+nvl(comm, 0)income, d.dname from emp e, dept d where e.deptno = d.deptno;
--修改表
SQL> desc test1;名称 是否为空? 类型
--------------------------TID NUMBER TNAME VARCHAR2(20)HIREDATE DATE
ALTER TABLE table ADD(column datatype [DEFAULT expr]
[, column datatype]...);
--增加一列
alter table test1 add image blob;
ALTER TABLE table MODIFY(column datatype [DEFAULT expr]
[, column datatype]...);--修改类型长度
alter table test1 modify tname varchar2(40);
ALTER TABLE table DROP column(column);--删除列
alter table test1 drop column image;
ALTER TABLE table_name rename column old_column_name to new_column_name--重命名列名
alter table test1 rename column tname to username;
=====> alter table t4 add tname2 varchar2(40);--增加一列
alter table t4 modify tname2 varchar2(80);--修改列
alter table t4 rename column tname2 to mytname2--修改列的名字
alter table t4 drop column mytname2--删除列
--删除表
drop table test1;--oracle的回收站
--查看回收站 show recyclebin--清空回收站 purge recyclebin;--彻底删除一张表
drop table test1 purge;--直接删除一张表,不通过回收站
--还原表(从回收站中返复原表)
闪回的内容
--关于回收站注意问题
--并不是所有的人都有回收站 管理员没有回收站
SQL> show recyclebin;ORIGINAL NAME RECYCLEBIN NAME OBJECT TYPE DROP TIME-----------------------------------------------------------------------------EMP20 BIN$yB56S7m9QCacFW9wbkk4Ig==$0 TABLE 2014-08-10:22:48:43 SQL> select * from emp20;不可以访问表
SQL> select * from BIN$yB56S7m9QCacFW9wbkk4Ig==$0 不可以访问表
SQL> select * from “BIN$yB56S7m9QCacFW9wbkk4Ig==$0”;可以访问表
结论:通过回收站的名字,查看原来表的内容,需要双引号
有关数据的完整性 约束--有关check约束 create table test2(tid number, tname varchar(20), gender varchar2(4)check(gender in('男', '女')), sal number check(sal > 0));insert into test2 values(1, 'tom', '男', 1000);第 1 行出现错误: ORA-02290: 违反检查约束条件(SCOTT.student_age_min SCOTT.SYS_C005518)定义
--定义约束时,如果没有给约束起名字,oracle系统会自动给它起个名字
--有关primary key 1 主键约束 通过这一列 唯一的确定一行值若定义主键约束 隐含 非空且唯一
--有关 foreign key 外键约束定义 两张表, 一个表的列值 引用了另外一张表的列值 员工表 部门表
子表的外键关联的是父表的主键父表中的数据被子表引用, 则父表相应记录删不掉
若想删掉 级联删除 级联置空
create table teacher(sid number constraint teacher_sid_pk primary key, sname varchar2(20)constraint teacher_sname_notnull not null, email varchar2(64)constraint teacher_email_notnull not null
constraint teacher_email_unique unique,sex number constraint teacher_sex_min_check check(sex in(1, 2)), age number constraint teacher_age check(age>20))
insert into teacher(sid, sname, email, sex, age)values(1, 'tom11', '11@163.com', 1, 21)
insert into student(sid, sname, email, sex, age)values(1, 'tom11', '11@163.com', 1, 20)
综合案例
create table student(sid number constraint student_sid_pk primary key,--定义主键约束
sname varchar2(20)constraint student_name_nonull not null,--定义not null约束
email varchar2(40)constraint student_email_unique unique--定义unique约束
constraint student_email_notnull not null,--定义no null约束
age number constraint student_age_min check(age>20),--定义age检查约束
--定义外键约束
deptno number constraint student_fk references dept(deptno)ON DELETE SET NULL);
insert into student(sid, sname, email, age, deptno)values(1, 'student01', 'stu@126.com', 19, 10)/
create table student(sid number constraint student_sid_pk primary key, sname varchar2(20)constraint student_name_nonull not null, email varchar2(40)constraint student_email_unique unique
constraint student_email_notnull not null, age number constraint student_age_min check(age>20), deptno number constraint student_fk references dept(depntno))
大小写控制函数 大小写控制函数 字符控制函数数字函数ROUND 函数TRUNC 函数MOD 函数......
1 笛卡尔积 部门表 笛卡尔积产生结果: 行数 两个表相乘列数: 行数相加 原因条件等值连接select ****from tab1, tab2where tab1.a = tab2.a1 select count(e.ename)2 from......
--sql structured query language --DML--Data Manipulation Language--数据操作语言 query information (SELECT), add new rows (INSERT), modify existing rows (UPDATE)......
一、选择行1.简单的SELECT 语句SELECT 字段名1 [AS] '字段名1 解释' FROM table; 2.处理NULL NVL函数可把NULL转换成其它类型的符号编程技巧: NVL函数在多条件模糊查询的......
------------------------- --order by的用法--员工信息按照姓名正序排列select * from emp order by ename asc; --员工信息按照倒叙排列select * from emp order by ename......