date 是日期  
datetime 是日期 时间
sql server 2000 2005中没有date

date是SQL Server 2008新引进的数据类型

note1
数据库:数据的存储仓库;

文件可以作为数据库; ATM
硬盘:

常见的数据库:
MS SQLServer
DB2
MySQL
infomix
Oracle
Access
Foxpro
Sybase
PointBase

数据库:大中小

Access 微小数据库1K---1W条数据的吞吐量;
PointBase 微小数据库; BEA

MySQL :中小型数据库;10W--100W条数据的吞吐量; SUN公司
MS SQLServer :中型数据库 100W--1000W条数据吞吐量;

大型数据库:1000W条以上吞吐量
DB2 :IBM
Oracle : Oracle
Sybase :

自己创建的文件也可以作为数据库,但是安全性问题;
使用数据库软件不但保证数据的安全,还保证查数据等操作的效率;

DB2 | Oracle

所有的数据都遵守国际SQL标准;

只要是标准的SQL语句,在任何符合SQL标准的数据软件中都可以正常的运行;
每一种数据不但遵守SQL标准,还独立拥有自己的方言;
Oracle PL/SQL

Oracle客户端使用常用命令;
l[ist] 显示缓冲区中的命令内容;
LIST

SAVE 保存缓冲区的内容到本地硬盘文件;

clear buffer 清空缓冲区;
cl buff 清空缓冲区;

@ [存储文件的路径] 将外部的文件加载到缓冲区并且执行;

edit 编辑缓冲区的内容;ed

/ 立即执行缓冲区的内容;

help | ? 显示命令的帮助信息;

quit | exit 都是退出当前连接;

注释 "--" "/* */"

DESCRIBE s_dept
DESC [TABLENAME]
浏览数据表结构
C[HANGE] / old/ new
替换操作
C [oldcommand] [newcommand]
CL[EAR] BUFF[ER]
清空缓存
CL BUFF
L[IST] n
查看缓存
L

SAVE filename
存储缓存中的命令到文件
GET filename
获取文件中的命令到缓存中 ,这时候文件还没有执行;
START filename
执行文件中的命令;
@ filename
执行文件
EDIT filename
编辑文件中的命令
SPOOL filename
清除或清空文件中的内容;
EXIT
退出当前SHELL
第一种SQL命令 select
第一条命令
select * from [tablename] 例如:
select * from s_dept;
此命令描述:查询s_dept表中的所有数据以及所有数据列;
第二条命令
select [column1,column2,column3,...] from [tablename]
select dept_id,last_name,manager_id from s_emp;
命令描述:查询s_emp表中的dept_id,last_name,manager_id列中的所有数据。
第三条命令
select [column1 alias1,column2 alias2,column3 alias3,...]
from s_emp;
select dept_id "ID",last_name "NAME",manager_id "M_ID" from s_emp;
命令描述:查询s_emp表中的dept_id,last_name,manager_id列中的所有数据,并且将dept_id列使用"ID",last_name列使用"NAME",manager_id列使用"M_ID"别名代替显示列名
第四条命令
select [distinct] {*,[column1 [alias1]],...} from [tablename]
select distinct name from s_dept
命令描述:查询s_dept表中的name列,显示所有不一样的数据;
第五条命令
进行数学运算 "+" 加 指令
select [column(number)]+[numbers] from [tablename];
select last_name,salary+100 from s_emp;
命令描述:查询s_emp表中的last_name,salary列中所有数据,并将salary中的每条数据加上100;
第六条命令
进行数学运算 "-" 减 指令
select [column(number)]-[numbers] from [tablename]
select last_name,salary-10,commission_pct from s_emp;
命令描述:查询s_emp表中的last_name,salary,commission_pct列中所有数据,并将salary列中的所有数据减去10;

第七条命令
进行数学运算 "*" 乘 指令
select [column(number)]*[number] from [tablename]
select last_name,salary*1.08,commission_pct from s_emp;
命令描述:查询s_emp表中的last_name,salary,commission_pct列中所有数据,并将salary列中的所有数据乘上1.08;
第八条命令
进行数学运算 "/" 除 指令
select [column(number)]/[number] from [tablename]
select last_name,salary/0.8,commission_pct from s_emp;
命令描述:查询s_emp表中的last_name,salary,commission_pct列中所有数据,并将salary列中的所有数据除上0.8;
第九条命令
进行数学运算之四则运算 "+,-,*,/",运算法则与数学当中的运算法则一致。运算级别也是同样的;
select ([column(number)]+[number])*[number]/[number]-[number] from [tablename]
select last_name,salary+(salary*commission_pct)+(200-100)+(salary/100) "SALAERY" from s_emp;
命令描述:查询s_emp表中的last_name,salary列中的所有数据,并将salary列中的所有数据加上(salary*commission_pct)加上(200-100)加上(salary/100)后的结果并以"SALARY"的别名显示出来;
第十条命令
将几列的数据合并显示使用连接符号 "||";
select [column1]||[column2]||[column3]... [alias] from [tablename]
select last_name||' '||first_name "Employees" from s_emp;
命令描述:查询s_emp表中的last_name和first_name列中的所有数据并且将两列的数据合并成一列以别名"Employees"显示
select last_name||' '||first_name||','||title "Employees" from s_emp;
命令描述:查询s_emp表中的last_name和first_name和title列中的所有数据合并成一列以别名"Employees"显示
第十一条命令
NVL 函数
NVL([column],[default])
注意:column是什么类型,default数据也要是同样类型
select salary+salary*nvl(commission_pct/100,0) from s_emp;
命令描述:查询s_emp表中的salary列中的所有数据,并且将有销售业绩的金额加入进去。
第十二条命令
设置Oracle的显示格式
COL[UMN] [{column|alias} [option]]
CLE[AR] Clear any column format
FOR[MAT] format --- Changes the display of the column using a format model.
HEA[DING] text --- Sets the column heading
JUS[TIFY] {left|center|right} -- Aligns the column heading to be left, center, or right.
COLUMN last_name HEADING 'Employee|Name' FORMAT A15
COLUMN salary JUSTIFY LEFT FORMAT $99,990.00
COLUMN start_date FORMAT A8 NULL 'Not hired'
column [columnname] clear
column clear;
column 显示所有的显示约束;

第十三条命令
Order by 排序参数
格式:
select [columns...]
from [tablename]   
order by {[columns..]|[condition(条件)] [up|down]}desc asc

select last_name||' '||first_name "Names",dept_id,
from s_emp
order by "Names"

select last_name "Employees" ,start_date
from s_emp

order by "Employees" desc
命令描述:查询s_emp表的last_name,start_date列中的所有数据并且将使用Employees别名显示出按照降序排列

select last_name "Employess",start_date
from s_emp
order by start_date;

select last_name,salary*12
from s_emp
order by 2;

select last_name,dept_id,salary
from s_emp
order by dept_id,salary desc

第十四条命令
where 条件查询
格式:
select [columns...]
from [tablenames...]
{where [条件] | order by [参数]}

select last_name,dept_id,salary
from s_emp
where dept_id=42

select first_name,last_name,title
from s_emp
where last_name='Magee'

select first_name,last_name,title
from s_emp
where last_name = 'Magee'
order by title

同样的where条件查询可以添加不同的查询格式来进行数据查询可以使用一些数学运算符:
"=" 等于,
">" 大于,
">=" 大于等于,
"<" 小于,
"<=" 小于等于,
"<>" 不等于。

select first_name,last_name,title,salary*12
from s_emp
where last_name <> 'Magee'
order by title

select first_name,last_name,title,salary*12 "SALARY"
from s_emp
where salary*12 > 10000
order by title

select first_name,last_name,title,salary*12 "SALARY"
from s_emp
where salary*12 >= 10000
order by SALARY

select first_name,last_name,title,salary*12 "SALARY"
from s_emp
where salary*12 < 10000
order by SALARY

select first_name,last_name,title,salary*12 "SALARY"
from s_emp
where salary*12 <= 10000
order by SALARY

select first_name,last_name,title
from s_emp
where salary > 1000

逻辑运算符:
"and" 与关系(并且)
"or"  或关系(或者)
"not" 非关系(不是)

比较运算符:
between ... and ...

select {*|columns,...} from [tablename]
where [column] between 条件1 and 条件2

in([list])
like
is null

以上的逻辑运算符和比较运算符是可以互相嵌套使用
如下:

select first_name||' '||last_name "Employees",start_date
from s_emp
where start_date between '09-may-91' and '17-jun-91'
order by "Employees"

select id "Numbers",name,region_id
from s_dept
where region_id in(1,3)
order by "Numbers"

like应用 "%","_"

select last_name||' '||first_name Employees
from s_emp
where last_name like '%M%'
order by Employees

select last_name||' '||first_name Employees
from s_emp
where last_name like 'M%' and first_name like 'E%'
order by Employees

select last_name||' '||first_name Employees
from s_emp
where last_name like '_M%'
and first_name like '_E%'
order by Employees

select last_name,start_date
from s_emp
where start_date like '%91'
order by start_date

select last_name
from s_emp
where last_name like '_a%'
order by id

select table_name
from user_tables
where table_name like 'S\_%'escape'\'
order by table_name

is null 表示:是空的,is not null 表示:不是空的.
select id,name,credit_rating
from s_customer
where sales_rep_id is null
order by name desc

select id,name,credit_rating
from s_customer
where sales_rep_id is not null
order by name desc

and用法
select last_name,salary,dept_id,title
from s_emp
where dept_id=41
and title='Stock Clerk'
order by last_name desc

逻辑运算符综合使用
select last_name,salary,dept_id
from s_emp
where salary>=1000
and dept_id=44
or dept_id=42
order by last_name

select last_name,salary,dept_id
from s_emp
where salary>=1000
and
(dept_id=44 or dept_id=42)
order by last_name

s总结
select [distinct]{*,column[alias],...}
from [tablename]
[where condition(s)]
[order by {column,expr,alias} [asc|desc]];

note2

functions 函数
简单列函数,复杂分组函数
简单函数:
LOWER 转换成小写如: TOM => tom
UPPER 转换成大写如: tom => TOM
INITCAP 转换成单词形式大写如: tom => Tom
CONCAT 连接字符串如:"Good" "Bye" => "GoodBye" ||
SUBSTR 返回子字符串,
LENGTH 返回字符串的长度
NVL 转换为null的值

以上的单值函数可以用在select中,也可以用在where条件中;

format格式:
lower('SQL Course') 如:
查询不到:
select last_name,first_name
from s_emp
where last_name='patel'
可以查到
select last_name,first_name
from s_emp
where lower(last_name)='patel'

upper('SQL Course') 如:
查询不到:
select last_name,first_name
from s_emp
where last_name='PATEL'
可以查询到:
select last_name,first_name
from s_emp
where upper(last_name)='PATEL'

select substr('String',1,3) from dual;
substr('String',1,3) ==> Str
1,起始位置;3,从起始位置截取的个数;
length('String')     ==> 6

ROUND  数据的四舍五入
TRUNC  数据的取整

round(45.9778,2) ==> 45.98
round(45.9523,2) ==> 45.95
round(45.73,0)   ==> 46
round(45.89,-1)  ==> 50
trunc(45.9778,2) ==> 45.97
trunc(45.9523,2) ==> 45.95
trunc(45.73)     ==> 45
trunc(45.89,-1)  ==> 40

日期函数
SYSDATE 获取系统中日期时间
select sysdate from dual;
dual代表的是系统中的假表;

计算两个时间之间的差值并且利用月份作为计算的单位
months_between([date1],[date2])
如:select months_between('01-sep-08','01-sep-07') Result
   from dual

计算一段月份后的日期
add_months([date],[num])
如:select add_months('01-jan-07',6) Result from dual

计算下一个星期几的具体日期
next_day([date],[day|num])
如:select next_day('01-sep-08','friday') "DATE",
    next_day('01-sep-08',6) "DAY" from dual

获取每个月份的最后一个日期
last_day([date])
如:select last_day('01-sep-08') "DATE" from dual;

计算日期
round([date],[parameter]) 保留相关日期
trunc([date],[parameter])
如:
select round(sysdate,'year') from dual;
select round(sysdate,'month') from dual;
select round(sysdate,'day') from dual;
select trunc(sysdate,'year') from dual;
select trunc(sysdate,'month') from dual;
select trunc(sysdate,'day') from dual;

类型转换
TO_CHAR   将其他的类型的数据转换成字符串或字符类型显示
TO_NUMBER 将字符串类型的数字转换成数字类型显示
TO_DATE   将字符串类型的日期转换成日期类型,或者将日期类型转换成所需要的格式类型;

TO_CHAR([PARAMETER])如:
SELECT TO_CHAR(SYSDATE) FROM DUAL;
select last_name,to_char(start_date,'fmDdspth "of" Month YYYY fmHH:MI:SS AM') HIERDATE
FROM s_emp
where start_date like '%91';

select 'Order '||to_char(id)||' was filled for a total of'|| to_char(total,'fm$9,999,999')
from s_ord
where date_shipped='21-SEP-92'

TO_NUMBER(char)如:
SELECT TO_NUMBER('345.98') FROM DUAL;
SELECT TO_NUMBER('345.98')*12 FROM DUAL;
TO_DATE([DATE],FORMATE)如:
SELECT TO_DATE('2008-07-18','YYYY-MM-DD') FROM DUAL;
SELECT TO_DATE('2008-18-08','YYYY-DD-MM') FROM DUAL;
select to_date('10 September 1992','dd Month YYYY') from dual;

综合使用function
 F3(F2(F1([PARAM],FMT1),FMT2),FMT3)

SELECT LAST_NAME,NVL(TO_CHAR(manager_id),'No Manager')
FROM s_emp
WHERE manager_id is null;

select id,
to_char(
    next_day(
        add_months(date_ordered,6),
    'friday'),
'fmDay,Month ddth,YYYY')
"New 6 Month Review"
from s_ord
order by date_ordered;

数据表的连接
jion {all jion | left jion | right jion}
jion方式
1.Equijoin 相等连接
2.non-equijoin 不等连接
3.Outer join 外连接
4.Self join 自连接

以下是相等连接
简单的连接格式
SELECT table.column,table.column
FROM table1,table2
WHERE table1.column1 = table2.column2;

select s_emp.last_name,s_dept.name
from s_emp,s_dept
where s_emp.dept_id = s_dept.id;

表别名
select t1.column,t2.column
from table1 t1,table2 t2
where t1.column = t2.column

select c.name "CustomerName",c.region_id "Region ID",
       r.name "RegionName"
from   s_customer c,s_region r
where  c.region_id = r.id

不等连接
不使用等号来连接计两个或两个以上的表
例如:
        使用>=、<、between ……
select e.ename,e.job,e.sal,s.grade
from emp e,salgrade s
where e.sal between s.losal and s.hisal;

select e.name,s.userid,s.id,e.id
from s_dept e,s_emp s
where e.id >= s.id ;e.id;

外连接
对于两个表,若记录不能一一连接,而又不想漏掉
数据,可使用外连接:这里也就是左连接和右连接的区别
但是也要注意如果连接错误就会造成笛卡儿乘积过大反而
不利于有效的查询操作。
SELECT table.column, table.column
FROM table1,table2
WHERE  table1.column=table2.column(+)

SELECT    e.last_name, e.id, c.name
FROM    s_emp e, s_customer c
WHERE    e.id = c.sales_rep_id(+)
ORDER BY e.id;

SELECT    e.last_name, e.id, c.name
FROM    s_emp e, s_customer c
WHERE    e.id (+) = c.sales_rep_id
ORDER BY e.id

对于某些情况,必须要有表自身的连接,这是可利用
表别名的作用,完全看作两个不同的表:
SELECT T1.column,T1.column+T2.column
FROM table T1,table T2
WHERE [conditions]

SELECT    worker.last_name||' works for '||manager.last_name
FROM    s_emp worker, s_emp manager
WHERE    worker.manager_id = manager.id;

组函数 group by
SELECT column, group_function
FROM  table
[WHERE condition]
[GROUP BY group_by_expr]
[HAVING group_condition]
[ORDER BY column……]

*GROUP BY:  对列进行分组
*HAVING: 条件限制组查询

常用组函数
AVG (DISTINCT|ALL|n) 求平均数
COUNT (DISTINCT|ALL|expr|*) 求总数
MAX (DISTINCT|ALL|expr) 求最大值
MIN (DISTINCT|ALL|expr) 求最小值
SUM (DISTINCT|ALL|n)    求和

SELECT    AVG(salary), MAX(salary),MIN(salary), SUM(salary)
FROM    s_emp
WHERE    UPPER(title) LIKE 'SALES%';

查找姓名最短和最长的数据
SELECT    MIN(last_name), MAX(last_name)
FROM    s_emp;

查询员工姓氏最短,姓氏长度最短,姓氏最长和姓氏长度最长的数据
SELECT MIN(length(last_name)),min(last_name), MAX(length(last_name)),max(last_name)
FROM    s_emp;

SELECT    COUNT(*)
FROM    s_emp
WHERE    dept_id = 31

查找31部门的平均工资是多少?
select avg(salary) from s_emp where dept_id = 31;
select sum(salary)/count(*) from s_emp where dept_id = 31;
最大工资是多少?
select max(salary) from s_emp where dept_id = 31;
工资总和是多少?
select sum(salary) from s_emp where dept_id = 31;
最小工资是多少?
select min(salary) from s_emp where dept_id = 31;
总共多少人?
select count(salary) from s_emp where dept_id = 31;
查询部门名称是"sales"的有多少个?
select count(*) from s_dept where lower(name) = 'sales';

SELECT    dept_id , COUNT(*) "Number"
FROM    s_emp
GROUP BY    dept_id

SELECT    credit_rating, COUNT(*) "# Cust"
FROM    s_customer
GROUP BY    credit_rating

SELECT    title, SUM(salary)  PAYROLL
FROM    s_emp
WHERE    title NOT LIKE 'VP%'
GROUP BY    title
ORDER BY    SUM(salary);

SELECT    title, SUM(salary) PAYROLL
FROM    s_emp
WHERE    title LIKE 'VP%'
GROUP BY    title
ORDER BY    SUM(salary);

SELECT    title, MAX(salary)
FROM    s_emp
GROUP BY    title;

如果在查询中使用到分组统计函数并且结果又不是单一的列那么就必须要有group by的分组函数进行数据的分组统计
例如:
SELECT    region_id, COUNT(name)
FROM    s_dept;

如果在查询中使用到分组函数有多个列的结果就必须对没有使用分组函数的列进行group by分组
SELECT    dept_id, title, COUNT(*)
FROM    s_emp
GROUP BY    dept_id,title;

如果需要对分组后的结果进行条件式查询就不能使用where条件进行分组函数查询。如下:
SELECT    dept_id, AVG(salary)
FROM    s_emp
WHERE    AVG(salary) > 2000
GROUP BY    dept_id;

正确:
SELECT    dept_id, AVG(salary)
FROM    s_emp
GROUP BY    dept_id;
HAVING AVG(salary) > 2000

HAVING 的语法运用

COLUMN     ”ANNUAL SALARY” FORMAT $99,999.99

SELECT    title, 12 * AVG(salary) "ANNUAL SALARY",
COUNT(*)     "NUMBER OF EMPLOYEES"
FROM     s_emp  
GROUP BY    title  
HAVING     COUNT(*) > 2;

SELECT    title, SUM(salary) PAYROLL
FROM    s_emp
WHERE    title NOT LIKE 'VP%'
GROUP BY    title
HAVING    SUM(salary) > 5000
ORDER BY    SUM(salary);

单独使用HAVING函数的select
SELECT    dept_id
FROM    s_emp
GROUP BY    dept_id
HAVING    SUM(salary) > 4000

单一的select全部使用查询
SELECT    column, group_function
FROM    table
[WHERE    condition]
[GROUP BY    group_by_expression]
[HAVING    group_condition]
[ORDER BY    column];

子查询
子查询根据其表现就可知,查询中的查询。
为什么要有查询中的查询?表中的关联,需要其它表中的某个或者多个字段作为一种查询的要素或者条件。

如果两张表可以使用子查询进行查找,那么必然两张表存在着某种联系.

例如:查询姓名为'Biri'这个人所在 部门 所有人的姓名职位。
select dept_id from s_emp where last_name = 'Biri';
select last_name,title
from s_emp
where dept_id =
(select dept_id
from s_emp
where last_name='Biri');
select last_name,title
from s_emp
where dept_id =  
(select dept_id
from s_emp
where last_name = 'Biri');
/*这里查询出来的数据只能是一条记录,如果是多条记录就会出现异常*/
要求:查询姓氏'SMITH'这个人所在职位名称 的所有人的姓氏;
s_emp  title

select title
from s_emp
where upper(last_name)='SMITH'

select last_name ,title
from s_emp
where title = ?

select last_name,title
from s_emp
where title =
(select title
from s_emp
where upper(last_name) = 'SMITH')

查询小于平均月薪的 人的姓氏,职位,月薪;

select last_name,title,salary
from s_emp
where salary < (
select avg(salary)
from s_emp);

子查询中注意子查询的返回列表,与主查询中的一些关系、运算符匹配;
例如错误的子查询:

select last_name,first_name,title
from s_emp
where dept_id=
(select id
from s_dept
where name = 'Finance'
or region_id=2
);
这里会提示子查询中的结果不只一条记录不能匹配。

利用组函数和组函数条件查询
查询
    部门的平均工资
大于
    32这个部门平均工资
的所有数据。

select dept_id,avg(salary)
from s_emp
group by dept_id
having avg(salary)>(
select avg(salary)
from s_emp
where dept_id=32);

编写一个子查询的具体格式
select  selectlist ,(subquery)
from table (subquery)
where (subquery)
group by
having (subquery)
order by

select e.last_name,d.name,e.salary
from s_emp e,s_dept d
where e.dept_id = d.id
and e.salary = (
select max(salary)
from s_emp
where dept_id = e.dept_id
group by dept_id
)

select d.name "DEPT" ,
decode(e.sex,'male',count(sex)) "MaleNum",
decode(e.sex,'female',count(sex)) "FemaleNum"
from t_emp e,t_dept d
where e.dept_id = d.id
group by sex,name;

select s_emp.last_name||' '||s_emp.first_name "Name",
s_dept.name "DeptName",
s_emp.title "Title",
s_emp.salary "Salary"
from s_emp,s_dept    
where  s_emp.dept_id in
(
select dept_id from s_emp
group by dept_id
having sum(salary)>4500
)
AND
s_dept.ID in
(
select dept_id from s_emp
group by dept_id
having sum(salary)>4500
)
order by "DeptName"

创建视图授权
SQL> conn sys/change_on_install as sysdba;
已连接。
SQL> GRANT CREATE VIEW TO scott;

授权成功。

SQL> conn scott/tiger;
已连接。
SQL> create view emp20 as select * from emp where deptno=20;

视图已创建。

note3

创建约束
约束方式:
unique  唯一约束
not null 非空约束
primary key 主键约束
foreign key 外键约束

约束创建格式:
column [CONSTRAINT constraint_name] constraint_type
column,...
  [CONSTRAINT constraint_name] constraint_type
  (column, ...),

创建非空约束:
CREATE TABLE    friend...
  phone    VARCHAR2(15) NOT NULL,...
  last_name    VARCHAR2(25)
    CONSTRAINT friend_last_name_nn NOT NULL,...

创建唯一约束:
方式1:
phone    VARCHAR2(10)
      CONSTRAINT s_emp_phone_uk UNIQUE,...
方式2:
phone    VARCHAR2(10),
    CONSTRAINT s_emp_phone_uk UNIQUE (phone)

创建主键约束:
方式1:
id number(11)
    constraint s_emp_id_PK primary key,
方式2:
id number(11)
    primary key,
方式3:
id number(11),
    constraint s_emp_id_PK primary key (id)

创建外键约束:
方式1:
dept_id number(11)
    constraint s_emp_dept_id_FK references s_dept(id),
方式2:
dept_id number(11),
name varchar2(40),
    constraint s_emp_dept_id_FK FOREIGN KEY(dept_id)
    references s_dept(id)

复制表结构:
create table [tablename]
[column(,column...)]
as [subquery]

例如根据部门id=41的结果创建表
CREATE TABLE     emp_41
AS
SELECT    id, last_name, userid, start_date
FROM        s_emp
WHERE    dept_id = 41;

拷贝一张数据表不能将元数据表中的所有约束拷贝,只能拷贝not null;

CREATE TABLE [schema.]table
        (column datatype [column_constraint], ...
          [table_constraint]);

update student set sex='F'
where classroom_id in (
    select id from classroom c
    where name = ?
);

修改表字段
添加:
ALTER TABLE table
ADD    (column datatype [DEFAULT expr][NOT NULL]
            [, column datatype]...);

alter table testtab
add(
    column1,
    column2,
    ...
)
删除:
ALTER TABLE table
DROP        (column
            [, column] ...);
修改:
ALTER TABLE    table
MODIFY    (column datatype [DEFAULT expr][NOT NULL]
                [, column datatype]...);

alter table student
modify (id varchar2(40)) --OK

name varchar2
alter table student
modify (name number(20))--NO

添加约束
ALTER TABLE    table
   ADD [CONSTRAINT constraint] type (column);

添加主键约束:
alter table [tablename]
    add CONSTRAINT [constraint_name] type (column);

添加外键约束
alter table [tablename]
    add CONSTRAINT [constraint_name]
    foreign key ([tablename.column])
    references [tablename2](column);

create table pktable (
id number(10) primary key,
fkid number(10)
)

create table fktable(
id number(10) primary key
)

alter table pktable
    ADD constraint tb_pk_fk
    foreign key (fkid)
    references fktable(id);

例如:
ALTER TABLE        s_emp
  2  ADD CONSTRAINT    s_emp_manager_id_fk
  3  FOREIGN KEY (manager_id)
  4  REFERENCES     s_emp(id);

desc all_constraints;
select   *   from   all_constraints
where table_name=upper('')
where lower(table_name) = ''

删除约束
ALTER TABLE            [tablename]
    DROP CONSTRAINT        [constraint_name]

ALTER TABLE        [tablename]
  2  DROP                 PRIMARY KEY CASCADE;

禁用主键约束
ALTER TABLE        
  2  DISABLE CONSTRAINT        [s_emp_id_pk] CASCADE;

启用主健约束
ALTER TABLE            s_emp
  2  ENABLE CONSTRAINT    s_emp_id_pk;

修改表名:
RENAME [oldname] TO [newname];

清空表 DDL
TRUNCATE TABLE [tablename];

创建视图
CREATE [OR REPLACE] [FORCE|NOFORCE] VIEW view
  [(alias[, alias]...)]
AS subquery
[WITH CHECK OPTION [CONSTRAINT constraint]]
[WITH READ ONLY]

CREATE VIEW     empvu45
    AS SELECT     id, last_name, title
    FROM    s_emp
    WHERE    dept_id = 45;

CREATE VIEW view_salvu41
   AS SELECT    id, first_name FIRST,
   last_name LAST, salary MONTHLY_SALARY
   FROM    s_emp
   WHERE    dept_id = 41;

CREATE OR REPLACE VIEW empvu45
      (id_number, employee, job)
    AS SELECT    id, last_name, title
    FROM    s_emp
    WHERE    dept_id = 45

CREATE VIEW dept_sum_vu
      (name, minsal, maxsal, avgsal)
    AS SELECT    d.name, MIN(e.salary),
      MAX(e.salary), AVG(e.salary)
    FROM    s_emp e, s_dept d
    WHERE    e.dept_id = d.id
    GROUP BY     d.name;

删除视图
DROP VIEW empvu45;

T-SQL中“ with check option”作用:
限定的是,数据的改变,不能超出WITH CHECK OPTION所约束的范围。
在DELETE时,不管有不有这个WITH CHECK OPTION,子查询中的记录是都可以删除的,子查询以外的记录是不会变化的。

而INSERT和UPDATE时,WITH CHECK OPTION作用就比较明显了。如果修改的数据,不符合WITH CHECK OPTION的限制,
是会报错,不被修改的。

T-sql 中的  with read only
只是查看,不做做更新。速度上会加快,不需要加锁

每次的增长幅度: incrtement by  长度

数据库课程总结(ORACLE)相关推荐

  1. oracle 慕课课程_“慕课”在Oracle数据库课程中的应用探讨

    [摘 要] 在分析近年来集宁师范学院Oracle数据库课程教学现状."慕课"教学模式优势的基础上,探讨了通过在线平台引入"慕课"进行混合式教学的教学方法,实现翻 ...

  2. oracle学生考勤,Oracle数据库课程设计――学生考勤系统的Oracle实现1

    Oracle数据库课程设计――学生考勤系统的Oracle实现1 辽宁工程技术大学 Oracle数据库课程设计报告 学生考勤系统 姓 名: XXXXX 班 级: 计SJ08-1班 学 号: 完成日期: ...

  3. oracle学生信息管理系统课程设计,数据库课程设计-学生信息管理系统的设计与实现.doc...

    数据库课程设计-学生信息管理系统的设计与实现 2011-2012课程设计II 学生信息管理系统的设计与实现 一 设计内容 建立一个简单的在校学生信息查询系统,可以让使用者查询到学生的一些简单的个人信息 ...

  4. 14c语言课程设计题目,2011级数据库课程设计任务书

    2011级数据库课程设计任务书 [设计目的] 数据库课程设计是在学生系统地学习了<数据库系统原理>课程后,按照关系型数据库 的基本原理,综合运用所学的知识,设计开发一个小型的数据库管理信息 ...

  5. Oracle数据库教程(Oracle备份、恢复、升级、迁移)视频教程

    Oracle数据库教程(Oracle备份.恢复.升级.迁移)视频教程 风哥Oracle备份恢复与迁移升级专题包括:Oracle备份恢复基础.用户模式的备份恢复.RMAN备份恢复.Flashback闪回 ...

  6. 视频教程-Oracle12数据库管理/DBA/数据库工程师培训-Oracle

    Oracle12数据库管理/DBA/数据库工程师培训 郑老师拥有超过二十年的IT行业经验,技术总监/培训师/系统工程师/项目经理.精通系统工程的很多领域:数据库(Oracle).操作系统Unix(So ...

  7. Oracle数据库培训视频教程 oracle工程师培训视频教程

    该课程是Oracle数据库系统工程师培训课程 Oracle Database,又名Oracle RDBMS,或简称Oracle.是甲骨文公司的一款关系数据库管理系统.到目前仍在数据库市场上占有主要份额 ...

  8. 视频教程-oracle数据库快速入门-Oracle

    oracle数据库快速入门 十年项目开发经验,主要从事java相关的开发,熟悉各种mvc开发框架. 王振伟 ¥21.00 立即订阅 扫码下载「CSDN程序员学院APP」,1000+技术好课免费看 AP ...

  9. Javaweb 第5天 mysql 数据库课程

    MySQL数据库课程 两日大纲 ● 数据库的概念.MySQL快速入门.SQL语言简介 ● 数据库操作.表操作.数据记录操作.数据类型和约束 ● 查询 ● 多表关系.多表连接查询 ● 视图 ● 数据备份 ...

  10. 学生成绩管理系统mysql课程设计_数据库课程设计(极其简单的学生成绩管理系统)...

    这个是我大三上学期的时候刚开始学习数据库课程的时候做的一个很稀烂的课程设计的源代码,当时刚刚开始学习Java和Oracle,而且当时就花了今年元旦3天假,做的这个C/S程序中没有加入触发器和存储过程等 ...

最新文章

  1. Android 探究 LayoutInflater setFactory
  2. 老友会 | 情怀与时光不期而遇的深情大趴(现场快讯)
  3. java rabbitmq topic_java rabbitmq 发送消息是topic模式, 消费者 怎么消费多个不同名字的队列?...
  4. CoordinatorLayout 使用综述系列(二)与AppBarLayout结合上下联动效果
  5. 中概股信任危机?美证监会主席直言不要购买中概股股票
  6. C#时间的味道——任时光匆匆我只在乎你
  7. 计算机应用基础任务式教程 素材,计算机应用基础任务化教程教学大纲
  8. Vue项目实战——实现GitHub搜索案例(学以致用,两小时带你巩固和强化Vue知识点)
  9. deepin,windows10双系统安装教程
  10. kde下gwenview启动慢,甚至几十秒才能启动
  11. 计算机用户名携带中文路径,Win10 User下的中文用户名改成英文路径操作方法
  12. APS系统是什么?APS系统是什么意思?
  13. 简单数据类型的转换和条件控制语句(if else)的使用
  14. 长波红外线灯的全球与中国市场2022-2028年:技术、参与者、趋势、市场规模及占有率研究报告
  15. 前端面试官常问的问题
  16. 利用python进行数据分析——透视表与交叉表
  17. 圣诞节,深圳街头有点冷清了~
  18. linux查找代码cd文件夹,Linux fing cd 查找文件/文件夹并进入目录命令
  19. 学习分享——基于深度学习的NILM负荷分解(一)对DL的看法准备工作
  20. 3、频域无芯片RFID标签原理

热门文章

  1. S君 被父母毁掉的一生 r瓴
  2. 绝了!Spring事务是如何传播的?快来收藏!
  3. GE IC系列PLC IC687RCM711 GE IC687BEM744 IC697CFR782 IC697CFR28 IC687BEM713IC687BEM731 IC687BEM744-EB
  4. 太阳宇宙线:太阳质子模型
  5. cocos2d-x 3.0游戏实例学习笔记《卡牌塔防》第三步---编辑器(2)---更方便很多其它操作更像编辑器...
  6. 虚拟机可以当成服务器吗,虚拟机不仅仅可以用于部署服务器功能
  7. 杨米尔斯理论讲了什么
  8. C++中的库文件导入与导出
  9. delphi FastReport fr3使用注意点
  10. 奖客富翁系统代码C语言,木马代码-c语言木马代码,最简单的,我保证不做违法的 – 手机爱问...