一、SQL简介文章源自懂站帝-http://www.sfdkj.com/11987.html
1:什么是SQL?文章源自懂站帝-http://www.sfdkj.com/11987.html
A:SQL指结构化查询语句 B:SQL使我们有能力访问数据库 C:SQL是一种ANSI(美国国家标准化组织)的标准计算机语言文章源自懂站帝-http://www.sfdkj.com/11987.html
2:SQL能做什么?文章源自懂站帝-http://www.sfdkj.com/11987.html
*面向数据库执行查询 *从数据库中取出数据 *向数据库插入新的记录文章源自懂站帝-http://www.sfdkj.com/11987.html
*更新数据库中数据 *从数据库删除记录 *创建数据库 *创建表文章源自懂站帝-http://www.sfdkj.com/11987.html
*创建存储过程 *创建视图 *设置表、存储过程和视图的权限文章源自懂站帝-http://www.sfdkj.com/11987.html
3:RDBMS文章源自懂站帝-http://www.sfdkj.com/11987.html
RDBMS是指关系型数据库管理系统文章源自懂站帝-http://www.sfdkj.com/11987.html
RDBMS是SQL的基础,同样也是所有现代数据库系统的基础,如MS SQL Server、IBM DB2、Oracle、MySQL以及Microsoft Access文章源自懂站帝-http://www.sfdkj.com/11987.html
RDBMS中的数据存储在被称为表的数据库对象中文章源自懂站帝-http://www.sfdkj.com/11987.html
表是相关的数据项的集合,他由列和行组成。文章源自懂站帝-http://www.sfdkj.com/11987.html
二、SQL语法文章源自懂站帝-http://www.sfdkj.com/11987.html
注意:SQL对大小写不敏感!!!文章源自懂站帝-http://www.sfdkj.com/11987.html
1:SQL语句后面的分号文章源自懂站帝-http://www.sfdkj.com/11987.html
某些数据库系统要求在每条SQL命令的末端使用分号。文章源自懂站帝-http://www.sfdkj.com/11987.html
分号是在数据库系统中分隔每条SQL语句的标准方法,这样就可以在服务器的相同请求中执行一条以上的语句。文章源自懂站帝-http://www.sfdkj.com/11987.html
如果使用的是MS Access和SQL Server 2000,则不必在每条SQL语句之后使用分号,不过某些数据库要求必须使用分号。文章源自懂站帝-http://www.sfdkj.com/11987.html
2:SQL DML和DDL文章源自懂站帝-http://www.sfdkj.com/11987.html
可以把SQL分为两个部分:数据操作语言(DML)和数据库定义语言(DDL)文章源自懂站帝-http://www.sfdkj.com/11987.html
SQL(结构化查询语句)适用于执行查询的语法。但是SQL语言也包含用于更新、插入和删除记录的语法。查询和更新构成了SQL的DML部分:select、update、delete、insert into 。 数据库定义语言(DDL)部分使我们有能力创建或删除表格,我们也可以定义索引(键),规定表之间的连接,以及事假表间的约束:文章源自懂站帝-http://www.sfdkj.com/11987.html
Create database、alert database、create table、alert table、drop table、create index、drop index
三、Select文章源自懂站帝-http://www.sfdkj.com/11987.html
User表里面的数据如下文章源自懂站帝-http://www.sfdkj.com/11987.html

文章源自懂站帝-http://www.sfdkj.com/11987.html
查询user表里面的user_name字段和user_age字段的所有数据文章源自懂站帝-http://www.sfdkj.com/11987.html
Select user_name,user_age from user文章源自懂站帝-http://www.sfdkj.com/11987.html

文章源自懂站帝-http://www.sfdkj.com/11987.html
查询user表中所有的字段数据,用 * 表示列的名称文章源自懂站帝-http://www.sfdkj.com/11987.html
Select * from user文章源自懂站帝-http://www.sfdkj.com/11987.html
四、Distinct文章源自懂站帝-http://www.sfdkj.com/11987.html
Distinct选取所有的值的时候不会出现重复的数据文章源自懂站帝-http://www.sfdkj.com/11987.html
用普通的查询,查询所有文章源自懂站帝-http://www.sfdkj.com/11987.html
Select * from user文章源自懂站帝-http://www.sfdkj.com/11987.html

文章源自懂站帝-http://www.sfdkj.com/11987.html
Select distinct user_name,user_age from user文章源自懂站帝-http://www.sfdkj.com/11987.html
注意:不能有user_id,因为两个Mary的user_id不一样,加上就不算相同数据文章源自懂站帝-http://www.sfdkj.com/11987.html
五、Where文章源自懂站帝-http://www.sfdkj.com/11987.html
1:查询user_id等于1 的数据文章源自懂站帝-http://www.sfdkj.com/11987.html
Select * from user where user_id = 1文章源自懂站帝-http://www.sfdkj.com/11987.html

文章源自懂站帝-http://www.sfdkj.com/11987.html
2:查询user_age大于等于12的数据文章源自懂站帝-http://www.sfdkj.com/11987.html
Select * from user where user_age >=12文章源自懂站帝-http://www.sfdkj.com/11987.html

文章源自懂站帝-http://www.sfdkj.com/11987.html
3:查询user_age不等于12的数据文章源自懂站帝-http://www.sfdkj.com/11987.html
Select * from user where user_age <> 12文章源自懂站帝-http://www.sfdkj.com/11987.html
六、AND 和 OR文章源自懂站帝-http://www.sfdkj.com/11987.html
And和or在where子语句中把两个或多个条件结合起来。如果需要两个条件都成立就是用and如果只需要其中一个条件成立就使用or文章源自懂站帝-http://www.sfdkj.com/11987.html
Select * from user where user_name = 'mary' and user_age = 12文章源自懂站帝-http://www.sfdkj.com/11987.html
需要注意的是SQL使用单引号来环绕文本值,如果是数值则不需要引号文章源自懂站帝-http://www.sfdkj.com/11987.html

文章源自懂站帝-http://www.sfdkj.com/11987.html
Select * from user where user_name='mary' or user_age =13文章源自懂站帝-http://www.sfdkj.com/11987.html

文章源自懂站帝-http://www.sfdkj.com/11987.html
结合and和or使用圆括号来组成复杂的表达式文章源自懂站帝-http://www.sfdkj.com/11987.html
Select * from user where (user_name = 'mary' and user_age = 12) or(user_age =13)文章源自懂站帝-http://www.sfdkj.com/11987.html
七、Order by文章源自懂站帝-http://www.sfdkj.com/11987.html
1:对指定列进行升序排列文章源自懂站帝-http://www.sfdkj.com/11987.html
Select * from user order by user_name文章源自懂站帝-http://www.sfdkj.com/11987.html

文章源自懂站帝-http://www.sfdkj.com/11987.html
2:按照user_id逆序排列文章源自懂站帝-http://www.sfdkj.com/11987.html
Select * from user order by user_id DESC文章源自懂站帝-http://www.sfdkj.com/11987.html

文章源自懂站帝-http://www.sfdkj.com/11987.html
2:按照升序排列user_id逆序排列user_age文章源自懂站帝-http://www.sfdkj.com/11987.html
SELECT * FROM user order by user_id ASC,user_age DESC文章源自懂站帝-http://www.sfdkj.com/11987.html

文章源自懂站帝-http://www.sfdkj.com/11987.html
3:按照升序排列user_id逆序排列user_age文章源自懂站帝-http://www.sfdkj.com/11987.html
SELECT * FROM user order by user_age DESC,user_id ASC文章源自懂站帝-http://www.sfdkj.com/11987.html

文章源自懂站帝-http://www.sfdkj.com/11987.html
注意:前面的条件优先级更高!!文章源自懂站帝-http://www.sfdkj.com/11987.html
八、Insert文章源自懂站帝-http://www.sfdkj.com/11987.html
User表文章源自懂站帝-http://www.sfdkj.com/11987.html

文章源自懂站帝-http://www.sfdkj.com/11987.html
插入一行数据 user_id为2 user_name为tom,user_age为12文章源自懂站帝-http://www.sfdkj.com/11987.html
注意:如果每一项都有插入的话就不需要在前面列出列名!!文章源自懂站帝-http://www.sfdkj.com/11987.html
Insert into user values(2,'tom',12)文章源自懂站帝-http://www.sfdkj.com/11987.html

文章源自懂站帝-http://www.sfdkj.com/11987.html
新插入一行数据,只要求user_name为eva文章源自懂站帝-http://www.sfdkj.com/11987.html
Insert into user(user_name) values('eva')文章源自懂站帝-http://www.sfdkj.com/11987.html
注意:因为ID设置为自增,所以user_id不为null文章源自懂站帝-http://www.sfdkj.com/11987.html
九、Update文章源自懂站帝-http://www.sfdkj.com/11987.html
修改user_id为6的数据user_age为14文章源自懂站帝-http://www.sfdkj.com/11987.html
Update user set user_age=14 where user_id=6文章源自懂站帝-http://www.sfdkj.com/11987.html

文章源自懂站帝-http://www.sfdkj.com/11987.html
修改user_id为1的数据user_name为ann,user_age为11文章源自懂站帝-http://www.sfdkj.com/11987.html
Update user set user_name='ann',user_age=11 where user_id=1文章源自懂站帝-http://www.sfdkj.com/11987.html

文章源自懂站帝-http://www.sfdkj.com/11987.html
十、Delete文章源自懂站帝-http://www.sfdkj.com/11987.html
User表中的所有数据信息如下文章源自懂站帝-http://www.sfdkj.com/11987.html

文章源自懂站帝-http://www.sfdkj.com/11987.html
删除user_age为12的数据文章源自懂站帝-http://www.sfdkj.com/11987.html
Delete from user where user_age=12文章源自懂站帝-http://www.sfdkj.com/11987.html

文章源自懂站帝-http://www.sfdkj.com/11987.html
删除表中的所有数据文章源自懂站帝-http://www.sfdkj.com/11987.html
Delete from user文章源自懂站帝-http://www.sfdkj.com/11987.html
第二章 SQL高级教程文章源自懂站帝-http://www.sfdkj.com/11987.html
一、Top文章源自懂站帝-http://www.sfdkj.com/11987.html
Top子句用于返回要返回的记录的数目,但并不是所有的数据库都支持top子句文章源自懂站帝-http://www.sfdkj.com/11987.html
1:SQL Server文章源自懂站帝-http://www.sfdkj.com/11987.html
Select top 5 * from user文章源自懂站帝-http://www.sfdkj.com/11987.html
2:MySQL文章源自懂站帝-http://www.sfdkj.com/11987.html
Select * from user limit 5文章源自懂站帝-http://www.sfdkj.com/11987.html
3:Oracle文章源自懂站帝-http://www.sfdkj.com/11987.html
Select * from user where ROWNUM <= 5文章源自懂站帝-http://www.sfdkj.com/11987.html
二、Like文章源自懂站帝-http://www.sfdkj.com/11987.html
User表的初始数据如下文章源自懂站帝-http://www.sfdkj.com/11987.html

文章源自懂站帝-http://www.sfdkj.com/11987.html
1:找出以li开头的数据文章源自懂站帝-http://www.sfdkj.com/11987.html
Select * from user where user_name like 'li%'文章源自懂站帝-http://www.sfdkj.com/11987.html

文章源自懂站帝-http://www.sfdkj.com/11987.html
2:找出以ry结尾的数据文章源自懂站帝-http://www.sfdkj.com/11987.html
Select * from user where user_name like '%ry'文章源自懂站帝-http://www.sfdkj.com/11987.html

文章源自懂站帝-http://www.sfdkj.com/11987.html
3:找出含有a的数据文章源自懂站帝-http://www.sfdkj.com/11987.html
Select * from user where user_name like '%a%'文章源自懂站帝-http://www.sfdkj.com/11987.html

文章源自懂站帝-http://www.sfdkj.com/11987.html
4:找出第二个字母是a第四个字母是y的数据文章源自懂站帝-http://www.sfdkj.com/11987.html
Select * from user where user_name like '_a_y'文章源自懂站帝-http://www.sfdkj.com/11987.html

文章源自懂站帝-http://www.sfdkj.com/11987.html
三、通配符文章源自懂站帝-http://www.sfdkj.com/11987.html
在搜索数据库中的数据的时候SQL通配符可以替代一个或多个字符。SQL通配符必须与like运算符一起使用文章源自懂站帝-http://www.sfdkj.com/11987.html
1: _ 替代一个字符文章源自懂站帝-http://www.sfdkj.com/11987.html
找出第二个字母是a第四个字母是y的数据文章源自懂站帝-http://www.sfdkj.com/11987.html
Select * from user where user_name like '_a_y'文章源自懂站帝-http://www.sfdkj.com/11987.html

文章源自懂站帝-http://www.sfdkj.com/11987.html
2: % 替代一个或多个字符文章源自懂站帝-http://www.sfdkj.com/11987.html
找出以ry结尾的数据文章源自懂站帝-http://www.sfdkj.com/11987.html
Select * from user where user_name like '%ry'文章源自懂站帝-http://www.sfdkj.com/11987.html

文章源自懂站帝-http://www.sfdkj.com/11987.html
3: [] 字符列中的任意一个单字符文章源自懂站帝-http://www.sfdkj.com/11987.html
找出以a或者l开头的数据文章源自懂站帝-http://www.sfdkj.com/11987.html
Select * from user where user_name like '[al]%'文章源自懂站帝-http://www.sfdkj.com/11987.html
找出不是a或者l开头的数据文章源自懂站帝-http://www.sfdkj.com/11987.html
Select * from user where user_name like '[!al]%'文章源自懂站帝-http://www.sfdkj.com/11987.html
四、In文章源自懂站帝-http://www.sfdkj.com/11987.html
只要数据满足in里面的一个条件就可以了文章源自懂站帝-http://www.sfdkj.com/11987.html
找到user_age是12或者13的数据文章源自懂站帝-http://www.sfdkj.com/11987.html
Select * from user where user_age in (12,13)文章源自懂站帝-http://www.sfdkj.com/11987.html

文章源自懂站帝-http://www.sfdkj.com/11987.html
找到user_name是Harry和Mary的数据文章源自懂站帝-http://www.sfdkj.com/11987.html
Select * from user where user_name IN ('mary','harry')文章源自懂站帝-http://www.sfdkj.com/11987.html

文章源自懂站帝-http://www.sfdkj.com/11987.html
五、Between文章源自懂站帝-http://www.sfdkj.com/11987.html
选取两个值之间的数据文章源自懂站帝-http://www.sfdkj.com/11987.html
查询年龄在12和14之间的数据文章源自懂站帝-http://www.sfdkj.com/11987.html
Select * from user where user_age between 12 and 14文章源自懂站帝-http://www.sfdkj.com/11987.html

文章源自懂站帝-http://www.sfdkj.com/11987.html
查询字母在Alice和John之间的数据文章源自懂站帝-http://www.sfdkj.com/11987.html
Select * from user where user_name between 'alice' AND'john'文章源自懂站帝-http://www.sfdkj.com/11987.html

文章源自懂站帝-http://www.sfdkj.com/11987.html
六、Aliases文章源自懂站帝-http://www.sfdkj.com/11987.html
指定别名文章源自懂站帝-http://www.sfdkj.com/11987.html
假设我们有两个表分别是user和Room 。我们分别指定他们为u和r。文章源自懂站帝-http://www.sfdkj.com/11987.html
1:不使用别名文章源自懂站帝-http://www.sfdkj.com/11987.html
Select room.room_name,user.user_name,user.user_age from user ,room Where user.user_age=12 and room.room_id = 1文章源自懂站帝-http://www.sfdkj.com/11987.html

文章源自懂站帝-http://www.sfdkj.com/11987.html
2:使用别名文章源自懂站帝-http://www.sfdkj.com/11987.html
使用别名的时候直接将别名跟在后面,不使用as也可以文章源自懂站帝-http://www.sfdkj.com/11987.html
Select r.room_name,u.user_name,u.user_age from user as u,room as r Where u.user_age=12 and r.room_id = 1文章源自懂站帝-http://www.sfdkj.com/11987.html

文章源自懂站帝-http://www.sfdkj.com/11987.html
七、Join文章源自懂站帝-http://www.sfdkj.com/11987.html
数据库中的表可以通过键将彼此联系起来,主键是一个列,在这个列中的每一行的值都是唯一的,在表中,每个主键的值都是唯一的,这样就可以在不重复每个表中的所有数据的情况下,把表间的数据交叉捆绑在一起。文章源自懂站帝-http://www.sfdkj.com/11987.html
以下为表user和表Room的数据文章源自懂站帝-http://www.sfdkj.com/11987.html

文章源自懂站帝-http://www.sfdkj.com/11987.html

文章源自懂站帝-http://www.sfdkj.com/11987.html
1:引用两个表文章源自懂站帝-http://www.sfdkj.com/11987.html
找出在Room of boy相关联的用户信息文章源自懂站帝-http://www.sfdkj.com/11987.html
Select u.user_name,u.user_age,r.room_name from user as u,room as r文章源自懂站帝-http://www.sfdkj.com/11987.html
Where u.room_id = r.room_id and r.room_name='room of boy'文章源自懂站帝-http://www.sfdkj.com/11987.html

文章源自懂站帝-http://www.sfdkj.com/11987.html
2:使用关键字join来连接两张表文章源自懂站帝-http://www.sfdkj.com/11987.html
Select u.user_name,u.user_age,r.room_name文章源自懂站帝-http://www.sfdkj.com/11987.html
from user as u文章源自懂站帝-http://www.sfdkj.com/11987.html
join room as r文章源自懂站帝-http://www.sfdkj.com/11987.html
on u.room_id = r.room_id and r.room_name='room of boy'文章源自懂站帝-http://www.sfdkj.com/11987.html

文章源自懂站帝-http://www.sfdkj.com/11987.html
八、Inner join文章源自懂站帝-http://www.sfdkj.com/11987.html
Inner join 与 join 用法一致文章源自懂站帝-http://www.sfdkj.com/11987.html
Select u.user_name,u.user_age,r.room_name文章源自懂站帝-http://www.sfdkj.com/11987.html
from user as u文章源自懂站帝-http://www.sfdkj.com/11987.html
inner join room as r文章源自懂站帝-http://www.sfdkj.com/11987.html
on u.room_id = r.room_id and r.room_name='room of boy'文章源自懂站帝-http://www.sfdkj.com/11987.html

文章源自懂站帝-http://www.sfdkj.com/11987.html
九、Left join文章源自懂站帝-http://www.sfdkj.com/11987.html
注意:左连接以左边的表为主体,也就是说会列出左边的表中的所有的数据,无论它是否满足条件。文章源自懂站帝-http://www.sfdkj.com/11987.html
1:user在左边文章源自懂站帝-http://www.sfdkj.com/11987.html
Select u.user_name,u.user_age,r.room_name文章源自懂站帝-http://www.sfdkj.com/11987.html
from user as u文章源自懂站帝-http://www.sfdkj.com/11987.html
Left join room as r文章源自懂站帝-http://www.sfdkj.com/11987.html
on u.room_id = r.room_id and r.room_name='room of boy'文章源自懂站帝-http://www.sfdkj.com/11987.html

文章源自懂站帝-http://www.sfdkj.com/11987.html
2:Room在左边文章源自懂站帝-http://www.sfdkj.com/11987.html
Select u.user_name,u.user_age,r.room_name文章源自懂站帝-http://www.sfdkj.com/11987.html
From room as r文章源自懂站帝-http://www.sfdkj.com/11987.html
Left join user as u文章源自懂站帝-http://www.sfdkj.com/11987.html
on u.room_id = r.room_id and r.room_name='room of boy'文章源自懂站帝-http://www.sfdkj.com/11987.html

文章源自懂站帝-http://www.sfdkj.com/11987.html
十、Right join文章源自懂站帝-http://www.sfdkj.com/11987.html
注意:左连接以右边的表为主体,也就是说会列出左边的表中的所有的数据,无论它是否满足条件。文章源自懂站帝-http://www.sfdkj.com/11987.html
1:Room在右边文章源自懂站帝-http://www.sfdkj.com/11987.html
Select u.user_name,u.user_age,r.room_name文章源自懂站帝-http://www.sfdkj.com/11987.html
from user as u文章源自懂站帝-http://www.sfdkj.com/11987.html
Right join room as r文章源自懂站帝-http://www.sfdkj.com/11987.html
on u.room_id = r.room_id and r.room_name='room of boy'文章源自懂站帝-http://www.sfdkj.com/11987.html

文章源自懂站帝-http://www.sfdkj.com/11987.html
2:user在右边文章源自懂站帝-http://www.sfdkj.com/11987.html
Select u.user_name,u.user_age,r.room_name文章源自懂站帝-http://www.sfdkj.com/11987.html
from room as r文章源自懂站帝-http://www.sfdkj.com/11987.html
Right join user as u文章源自懂站帝-http://www.sfdkj.com/11987.html
on u.room_id = r.room_id and r.room_name='room of boy'文章源自懂站帝-http://www.sfdkj.com/11987.html

文章源自懂站帝-http://www.sfdkj.com/11987.html
十一、Full join文章源自懂站帝-http://www.sfdkj.com/11987.html
1:user在左边文章源自懂站帝-http://www.sfdkj.com/11987.html
Select * from user Full join room文章源自懂站帝-http://www.sfdkj.com/11987.html

文章源自懂站帝-http://www.sfdkj.com/11987.html
2:Room在左边文章源自懂站帝-http://www.sfdkj.com/11987.html
Select * From room full join user文章源自懂站帝-http://www.sfdkj.com/11987.html

文章源自懂站帝-http://www.sfdkj.com/11987.html
注意:SQL错误码1054表示没有找到对应的字段名;错误码1064表示用户输入的SQL语句有语法错误文章源自懂站帝-http://www.sfdkj.com/11987.html
十二、Union文章源自懂站帝-http://www.sfdkj.com/11987.html
Union操作符用于合并两个或者多个SELECT语句的结果集文章源自懂站帝-http://www.sfdkj.com/11987.html
请注意,UNION内部的select语句必须拥有相同数量的列。列也必须拥有相同的数据类型。同时,每条select语句中的列的顺序必须相同。文章源自懂站帝-http://www.sfdkj.com/11987.html
下面是Room表和color表的数据文章源自懂站帝-http://www.sfdkj.com/11987.html

文章源自懂站帝-http://www.sfdkj.com/11987.html

文章源自懂站帝-http://www.sfdkj.com/11987.html
Select room_name from room文章源自懂站帝-http://www.sfdkj.com/11987.html
Union文章源自懂站帝-http://www.sfdkj.com/11987.html
Select color_name from color文章源自懂站帝-http://www.sfdkj.com/11987.html

文章源自懂站帝-http://www.sfdkj.com/11987.html
默认的union选取不同的值,如果想要有相同的值出现就使用union all文章源自懂站帝-http://www.sfdkj.com/11987.html
Select room_name from room文章源自懂站帝-http://www.sfdkj.com/11987.html
Union all文章源自懂站帝-http://www.sfdkj.com/11987.html
Select color_name from color文章源自懂站帝-http://www.sfdkj.com/11987.html

文章源自懂站帝-http://www.sfdkj.com/11987.html
十三、Create DB文章源自懂站帝-http://www.sfdkj.com/11987.html
创建数据库mysqltest文章源自懂站帝-http://www.sfdkj.com/11987.html
Create database mysqltest文章源自懂站帝-http://www.sfdkj.com/11987.html

文章源自懂站帝-http://www.sfdkj.com/11987.html
十四、Create table文章源自懂站帝-http://www.sfdkj.com/11987.html
Create table sqltest(文章源自懂站帝-http://www.sfdkj.com/11987.html
Id int,文章源自懂站帝-http://www.sfdkj.com/11987.html
Name varchar(45),文章源自懂站帝-http://www.sfdkj.com/11987.html
Age int,文章源自懂站帝-http://www.sfdkj.com/11987.html
Salary float,文章源自懂站帝-http://www.sfdkj.com/11987.html
Time Date,文章源自懂站帝-http://www.sfdkj.com/11987.html
)文章源自懂站帝-http://www.sfdkj.com/11987.html

文章源自懂站帝-http://www.sfdkj.com/11987.html

文章源自懂站帝-http://www.sfdkj.com/11987.html
十五、Constraints文章源自懂站帝-http://www.sfdkj.com/11987.html
SQL约束,用于限制加入表的数据的类型文章源自懂站帝-http://www.sfdkj.com/11987.html
常见约束:not noll、unique、primary key、foreign key、check、default文章源自懂站帝-http://www.sfdkj.com/11987.html
十六、Not null文章源自懂站帝-http://www.sfdkj.com/11987.html
Not null 约束强制列不接受NULL值。Not null 约束强制字段始终包含值,这意味着,如果不向字段添加值,就无法插入新的字段或者更新记录文章源自懂站帝-http://www.sfdkj.com/11987.html
用法,在字段后面加上 not null文章源自懂站帝-http://www.sfdkj.com/11987.html

文章源自懂站帝-http://www.sfdkj.com/11987.html
十七、Unique文章源自懂站帝-http://www.sfdkj.com/11987.html
Unique约束唯一标识数据库中的每一条记录。Primary key约束拥有自动的unique约束。需要注意的是,每个表里面可以拥有多个unique约束,但只能有一个primary key约束文章源自懂站帝-http://www.sfdkj.com/11987.html
1:MySQL用法,unique(字段名)文章源自懂站帝-http://www.sfdkj.com/11987.html

文章源自懂站帝-http://www.sfdkj.com/11987.html
2:SQL Server 、 Oracle 、 MS Access在字段后面加文章源自懂站帝-http://www.sfdkj.com/11987.html

文章源自懂站帝-http://www.sfdkj.com/11987.html
3:命名约束使用constraint文章源自懂站帝-http://www.sfdkj.com/11987.html

文章源自懂站帝-http://www.sfdkj.com/11987.html
4:已经创建了表之后需要添加约束文章源自懂站帝-http://www.sfdkj.com/11987.html
ALTER TABLE sqltest ADD UNIQUE(Age)文章源自懂站帝-http://www.sfdkj.com/11987.html

文章源自懂站帝-http://www.sfdkj.com/11987.html
5:给已经创建了的表添加约束并命名文章源自懂站帝-http://www.sfdkj.com/11987.html
ALTER TABLE sqltest ADD constraint unique_name UNIQUE(Age,salary)文章源自懂站帝-http://www.sfdkj.com/11987.html

文章源自懂站帝-http://www.sfdkj.com/11987.html
6:撤销约束文章源自懂站帝-http://www.sfdkj.com/11987.html
MySQL文章源自懂站帝-http://www.sfdkj.com/11987.html
在没有给约束命名的情况下(上面的age约束)直接使用字段名就可以了文章源自懂站帝-http://www.sfdkj.com/11987.html
ALTER TABLE sqltest DROP INDEX age文章源自懂站帝-http://www.sfdkj.com/11987.html

文章源自懂站帝-http://www.sfdkj.com/11987.html
删除后文章源自懂站帝-http://www.sfdkj.com/11987.html

文章源自懂站帝-http://www.sfdkj.com/11987.html
在约束有名字的情况下,直接使用名字就可以了文章源自懂站帝-http://www.sfdkj.com/11987.html
ALTER table sqltest drop index unique_name文章源自懂站帝-http://www.sfdkj.com/11987.html

文章源自懂站帝-http://www.sfdkj.com/11987.html
删除后文章源自懂站帝-http://www.sfdkj.com/11987.html

文章源自懂站帝-http://www.sfdkj.com/11987.html
SQL Server 、 Oracle 、 MS Access文章源自懂站帝-http://www.sfdkj.com/11987.html
ALTER table 表名 drop constraint 约束名文章源自懂站帝-http://www.sfdkj.com/11987.html
十八、Primary key文章源自懂站帝-http://www.sfdkj.com/11987.html
Primary key约束唯一标识数据库表中的每一条记录,组件必须包含唯一的值。组件列不能包含NULL值。每个表都应该有一个主键,并且每一个表都只能有一个主键文章源自懂站帝-http://www.sfdkj.com/11987.html
1:在MySQL中的用法文章源自懂站帝-http://www.sfdkj.com/11987.html

文章源自懂站帝-http://www.sfdkj.com/11987.html
2:在SQL Server 、 Oracle 和MS Access中的用法文章源自懂站帝-http://www.sfdkj.com/11987.html

文章源自懂站帝-http://www.sfdkj.com/11987.html
3:为已经创建成功的表创建primary key约束文章源自懂站帝-http://www.sfdkj.com/11987.html
Alter table sqltest add primary key(id)文章源自懂站帝-http://www.sfdkj.com/11987.html

文章源自懂站帝-http://www.sfdkj.com/11987.html
4:为已经创建成功的表添加主键约束,以及为多个列定义主键约束文章源自懂站帝-http://www.sfdkj.com/11987.html
Alter table sqltest add constraint pk_name primary key (id,name)文章源自懂站帝-http://www.sfdkj.com/11987.html

文章源自懂站帝-http://www.sfdkj.com/11987.html
5:在MySQL中撤销主键文章源自懂站帝-http://www.sfdkj.com/11987.html
ALTER TABLE sqltest DROP PRIMARY KEY文章源自懂站帝-http://www.sfdkj.com/11987.html

文章源自懂站帝-http://www.sfdkj.com/11987.html
删除后文章源自懂站帝-http://www.sfdkj.com/11987.html

文章源自懂站帝-http://www.sfdkj.com/11987.html
6:在SQL Server、Oracle、MS Access中撤销主键文章源自懂站帝-http://www.sfdkj.com/11987.html
Alter table 表名 drop constraint 主键名文章源自懂站帝-http://www.sfdkj.com/11987.html
十九、Foreign key文章源自懂站帝-http://www.sfdkj.com/11987.html
所谓的外键,即一个表的外键指向另一个表的主键文章源自懂站帝-http://www.sfdkj.com/11987.html
User表文章源自懂站帝-http://www.sfdkj.com/11987.html

文章源自懂站帝-http://www.sfdkj.com/11987.html
Room表文章源自懂站帝-http://www.sfdkj.com/11987.html

文章源自懂站帝-http://www.sfdkj.com/11987.html
在user表里面room_id列指向Room表里面的id列。Room表里面的id列是主键,user表里面的room_id列是外键。外键约束用于预防破坏表之间的连接动作,外键约束也能防止非法数据插入外键列,因为他必须是他指向的那个表的值之一。文章源自懂站帝-http://www.sfdkj.com/11987.html

文章源自懂站帝-http://www.sfdkj.com/11987.html
二十、Check文章源自懂站帝-http://www.sfdkj.com/11987.html
Check约束用于限制列中的值的范围。如果对单一的列定义check约束,那么改了只允许特定的值。如果对一个表定义check约束,那么此约束会在特定的列中对值进行限制。文章源自懂站帝-http://www.sfdkj.com/11987.html

文章源自懂站帝-http://www.sfdkj.com/11987.html
为已经创建成功的表添加check约束文章源自懂站帝-http://www.sfdkj.com/11987.html
ALTER TABLE USER ADD CHECK (age>10)文章源自懂站帝-http://www.sfdkj.com/11987.html
二十一、Default文章源自懂站帝-http://www.sfdkj.com/11987.html
Default约束用于向列宗插入默认值。如果没有规定其他值,那么就会将默认值添加到所有的新纪录。文章源自懂站帝-http://www.sfdkj.com/11987.html
用法:文章源自懂站帝-http://www.sfdkj.com/11987.html

文章源自懂站帝-http://www.sfdkj.com/11987.html
当表已经存在的时候,添加默认值文章源自懂站帝-http://www.sfdkj.com/11987.html
ALTER TABLE sqltest ALTER NAME SET DEFAULT 'tom'文章源自懂站帝-http://www.sfdkj.com/11987.html

文章源自懂站帝-http://www.sfdkj.com/11987.html

文章源自懂站帝-http://www.sfdkj.com/11987.html
撤销默认值文章源自懂站帝-http://www.sfdkj.com/11987.html

文章源自懂站帝-http://www.sfdkj.com/11987.html

文章源自懂站帝-http://www.sfdkj.com/11987.html
二十二、Create index文章源自懂站帝-http://www.sfdkj.com/11987.html
索引,你可以在表里面创建索引,一边更加快速高效地查询数据。用户无法看见索引,他们只能被用来加速搜索、查询。文章源自懂站帝-http://www.sfdkj.com/11987.html
注意:更新一个包含索引的表需要比更新一个没有索引的表更多的时间,这是索引本身也需要更新,因此,理想的做法是仅仅在常常被搜索的列上面创建索引。文章源自懂站帝-http://www.sfdkj.com/11987.html
1:创建一个索引文章源自懂站帝-http://www.sfdkj.com/11987.html
CREATE INDEX index_name ON color (color_id )文章源自懂站帝-http://www.sfdkj.com/11987.html

文章源自懂站帝-http://www.sfdkj.com/11987.html
2:创建一个独一无二的索引,即两行不能拥有相同的索引值。文章源自懂站帝-http://www.sfdkj.com/11987.html
CREATE UNIQUE INDEX book_index ON book (book_id)文章源自懂站帝-http://www.sfdkj.com/11987.html

文章源自懂站帝-http://www.sfdkj.com/11987.html
3:如果索引不止一个列,你可以在括号中列出这些列的名称,用逗号隔开文章源自懂站帝-http://www.sfdkj.com/11987.html
CREATE INDEX index_bank ON bank (bank_id,bank_name)文章源自懂站帝-http://www.sfdkj.com/11987.html

文章源自懂站帝-http://www.sfdkj.com/11987.html
二十三、Drop文章源自懂站帝-http://www.sfdkj.com/11987.html
通过使用DROP语句,可以删掉索引、表和数据库文章源自懂站帝-http://www.sfdkj.com/11987.html
1:删除索引文章源自懂站帝-http://www.sfdkj.com/11987.html
Drop index index_name on color文章源自懂站帝-http://www.sfdkj.com/11987.html

文章源自懂站帝-http://www.sfdkj.com/11987.html
删除之后文章源自懂站帝-http://www.sfdkj.com/11987.html

文章源自懂站帝-http://www.sfdkj.com/11987.html

文章源自懂站帝-http://www.sfdkj.com/11987.html

文章源自懂站帝-http://www.sfdkj.com/11987.html
2:删除表文章源自懂站帝-http://www.sfdkj.com/11987.html
DROP TABLE colorcopy文章源自懂站帝-http://www.sfdkj.com/11987.html

文章源自懂站帝-http://www.sfdkj.com/11987.html
删除之后文章源自懂站帝-http://www.sfdkj.com/11987.html

文章源自懂站帝-http://www.sfdkj.com/11987.html
3:清空表文章源自懂站帝-http://www.sfdkj.com/11987.html
TRUNCATE TABLE color文章源自懂站帝-http://www.sfdkj.com/11987.html

文章源自懂站帝-http://www.sfdkj.com/11987.html
删除之后文章源自懂站帝-http://www.sfdkj.com/11987.html
4:删除数据库文章源自懂站帝-http://www.sfdkj.com/11987.html
DROP DATABASE mysqltest文章源自懂站帝-http://www.sfdkj.com/11987.html

文章源自懂站帝-http://www.sfdkj.com/11987.html
删除之后文章源自懂站帝-http://www.sfdkj.com/11987.html

文章源自懂站帝-http://www.sfdkj.com/11987.html
二十四、Alert文章源自懂站帝-http://www.sfdkj.com/11987.html

文章源自懂站帝-http://www.sfdkj.com/11987.html
1:添加列文章源自懂站帝-http://www.sfdkj.com/11987.html
Alter table user add salary float文章源自懂站帝-http://www.sfdkj.com/11987.html

文章源自懂站帝-http://www.sfdkj.com/11987.html
2:删除列文章源自懂站帝-http://www.sfdkj.com/11987.html
Alter table user drop column room_id文章源自懂站帝-http://www.sfdkj.com/11987.html

文章源自懂站帝-http://www.sfdkj.com/11987.html
二十五、Increment文章源自懂站帝-http://www.sfdkj.com/11987.html
定义主键自增文章源自懂站帝-http://www.sfdkj.com/11987.html

文章源自懂站帝-http://www.sfdkj.com/11987.html
二十六、View文章源自懂站帝-http://www.sfdkj.com/11987.html
视图,一种基于SQL语句的结果集可视化表。视图包含行和列,就像一个真实的表。视图中的字段来自一个或多个数据库中的真实的表中的字段,我们可以向视图添加SQL函数、where以及join语句,我们提交数据,然后这些来自某个单一的表。需要注意的是,数据库中的结构和设计不会受到视图的函数、where或join语句的影响文章源自懂站帝-http://www.sfdkj.com/11987.html
1:创建一个视图,字段来自user表和Room表文章源自懂站帝-http://www.sfdkj.com/11987.html
CREATE VIEW view_test AS文章源自懂站帝-http://www.sfdkj.com/11987.html
SELECT user.user_name,user.user_age,room.room_name文章源自懂站帝-http://www.sfdkj.com/11987.html
FROM USER,room文章源自懂站帝-http://www.sfdkj.com/11987.html
WHERE user.user_age>10文章源自懂站帝-http://www.sfdkj.com/11987.html
2:查询视图文章源自懂站帝-http://www.sfdkj.com/11987.html
Select * from view_test文章源自懂站帝-http://www.sfdkj.com/11987.html

文章源自懂站帝-http://www.sfdkj.com/11987.html
3:撤销视图文章源自懂站帝-http://www.sfdkj.com/11987.html
DROP VIEW view_test文章源自懂站帝-http://www.sfdkj.com/11987.html
二十七、Date文章源自懂站帝-http://www.sfdkj.com/11987.html

文章源自懂站帝-http://www.sfdkj.com/11987.html
二十八、Nulls文章源自懂站帝-http://www.sfdkj.com/11987.html
默认的,表的列可以存放NULL值。如果表里面的某个列是可选的,那么我们可以在不想改列添加值的情况下插入记录或者更新记录,这意味着该字段以NULL值保存。注意,NULL和0是不等价的,不能进行比较。文章源自懂站帝-http://www.sfdkj.com/11987.html

文章源自懂站帝-http://www.sfdkj.com/11987.html
1:查询NULL值文章源自懂站帝-http://www.sfdkj.com/11987.html
select * from user where salary is null文章源自懂站帝-http://www.sfdkj.com/11987.html

文章源自懂站帝-http://www.sfdkj.com/11987.html
2:查询非NULL值文章源自懂站帝-http://www.sfdkj.com/11987.html
select * from user where salary is not null文章源自懂站帝-http://www.sfdkj.com/11987.html

文章源自懂站帝-http://www.sfdkj.com/11987.html
二十九、数据类型文章源自懂站帝-http://www.sfdkj.com/11987.html
MySQL主要有三种类型:文本、数字、日期文章源自懂站帝-http://www.sfdkj.com/11987.html

文章源自懂站帝-http://www.sfdkj.com/11987.html

文章源自懂站帝-http://www.sfdkj.com/11987.html

文章源自懂站帝-http://www.sfdkj.com/11987.html

文章源自懂站帝-http://www.sfdkj.com/11987.html

文章源自懂站帝-http://www.sfdkj.com/11987.html

文章源自懂站帝-http://www.sfdkj.com/11987.html

文章源自懂站帝-http://www.sfdkj.com/11987.html

文章源自懂站帝-http://www.sfdkj.com/11987.html
三十、服务器文章源自懂站帝-http://www.sfdkj.com/11987.html

文章源自懂站帝-http://www.sfdkj.com/11987.html
第三章 SQL函数文章源自懂站帝-http://www.sfdkj.com/11987.html
一、SQL functions文章源自懂站帝-http://www.sfdkj.com/11987.html
在SQL当中,基本的函数类型和种类有若干种,函数的基本类型是:文章源自懂站帝-http://www.sfdkj.com/11987.html
合计函数(Aggregate function)和 Scalar函数文章源自懂站帝-http://www.sfdkj.com/11987.html
Aggregate 函数,函数操作面向一系列的值,并返回一个单一的值。文章源自懂站帝-http://www.sfdkj.com/11987.html
Scalar 函数,操作面向某个单一的值,并返回基于输入值的一个单一的值。文章源自懂站帝-http://www.sfdkj.com/11987.html
二、Avg()文章源自懂站帝-http://www.sfdkj.com/11987.html
求平均年龄文章源自懂站帝-http://www.sfdkj.com/11987.html
Select avg(user_age) from user文章源自懂站帝-http://www.sfdkj.com/11987.html

文章源自懂站帝-http://www.sfdkj.com/11987.html
求大于平均年龄的用户文章源自懂站帝-http://www.sfdkj.com/11987.html
Select * from user where user_age>(Select avg(user_age) from user)文章源自懂站帝-http://www.sfdkj.com/11987.html

文章源自懂站帝-http://www.sfdkj.com/11987.html
三、Count()文章源自懂站帝-http://www.sfdkj.com/11987.html
返回列的值的数目(不包含NULL)文章源自懂站帝-http://www.sfdkj.com/11987.html
注意,可以使用as来给count()取一个别名文章源自懂站帝-http://www.sfdkj.com/11987.html

文章源自懂站帝-http://www.sfdkj.com/11987.html
Select count(user_id) from user文章源自懂站帝-http://www.sfdkj.com/11987.html

文章源自懂站帝-http://www.sfdkj.com/11987.html
Select count(salary) from user文章源自懂站帝-http://www.sfdkj.com/11987.html

文章源自懂站帝-http://www.sfdkj.com/11987.html
返回值不同的有多少文章源自懂站帝-http://www.sfdkj.com/11987.html
Select count(distinct user_name) from user文章源自懂站帝-http://www.sfdkj.com/11987.html

文章源自懂站帝-http://www.sfdkj.com/11987.html
查询所有列文章源自懂站帝-http://www.sfdkj.com/11987.html
Select count(*) from user文章源自懂站帝-http://www.sfdkj.com/11987.html

文章源自懂站帝-http://www.sfdkj.com/11987.html
四、Max()文章源自懂站帝-http://www.sfdkj.com/11987.html
返回最大值,NULL不包括在计算中文章源自懂站帝-http://www.sfdkj.com/11987.html

文章源自懂站帝-http://www.sfdkj.com/11987.html
Select max(price) as max_price from commodity文章源自懂站帝-http://www.sfdkj.com/11987.html

文章源自懂站帝-http://www.sfdkj.com/11987.html
五、Min()文章源自懂站帝-http://www.sfdkj.com/11987.html
返回最小值,NULL不包括在计算中文章源自懂站帝-http://www.sfdkj.com/11987.html

文章源自懂站帝-http://www.sfdkj.com/11987.html
Select min(salary) poor_man from user文章源自懂站帝-http://www.sfdkj.com/11987.html

文章源自懂站帝-http://www.sfdkj.com/11987.html
六、Sum()文章源自懂站帝-http://www.sfdkj.com/11987.html
返回该列值的总额文章源自懂站帝-http://www.sfdkj.com/11987.html

文章源自懂站帝-http://www.sfdkj.com/11987.html
Select sum(salary) from user文章源自懂站帝-http://www.sfdkj.com/11987.html

文章源自懂站帝-http://www.sfdkj.com/11987.html
七、Group By文章源自懂站帝-http://www.sfdkj.com/11987.html
用于结合合计函数,根据一个或多个列对结果集进行分组文章源自懂站帝-http://www.sfdkj.com/11987.html

文章源自懂站帝-http://www.sfdkj.com/11987.html
SELECT cname,SUM(price) FROM commodity GROUP BY cname文章源自懂站帝-http://www.sfdkj.com/11987.html

文章源自懂站帝-http://www.sfdkj.com/11987.html
八、Having文章源自懂站帝-http://www.sfdkj.com/11987.html
在SQL中增加having子句的原因是where不能与合计函数一起使用。用法和where 一样文章源自懂站帝-http://www.sfdkj.com/11987.html
SELECT cname,SUM(price) FROM commodity文章源自懂站帝-http://www.sfdkj.com/11987.html
GROUP BY cname文章源自懂站帝-http://www.sfdkj.com/11987.html
HAVING SUM(price)>20文章源自懂站帝-http://www.sfdkj.com/11987.html

文章源自懂站帝-http://www.sfdkj.com/11987.html
九、Ucase()文章源自懂站帝-http://www.sfdkj.com/11987.html
把函数字段的值转化为大写文章源自懂站帝-http://www.sfdkj.com/11987.html

文章源自懂站帝-http://www.sfdkj.com/11987.html
SELECT UCASE(user_name) FROM user文章源自懂站帝-http://www.sfdkj.com/11987.html

文章源自懂站帝-http://www.sfdkj.com/11987.html
十、Lcase()文章源自懂站帝-http://www.sfdkj.com/11987.html
将函数字段转化为小写文章源自懂站帝-http://www.sfdkj.com/11987.html

文章源自懂站帝-http://www.sfdkj.com/11987.html
Select lcase(user_name) from user文章源自懂站帝-http://www.sfdkj.com/11987.html

文章源自懂站帝-http://www.sfdkj.com/11987.html
十一、Mid()文章源自懂站帝-http://www.sfdkj.com/11987.html
从文本字段中提取字符文章源自懂站帝-http://www.sfdkj.com/11987.html

文章源自懂站帝-http://www.sfdkj.com/11987.html
Select mid(user_name,2,2) from user文章源自懂站帝-http://www.sfdkj.com/11987.html

文章源自懂站帝-http://www.sfdkj.com/11987.html
十二、Round()文章源自懂站帝-http://www.sfdkj.com/11987.html
Round函数把数值字段舍入为指定的小数位数文章源自懂站帝-http://www.sfdkj.com/11987.html

文章源自懂站帝-http://www.sfdkj.com/11987.html
Select round(salary,2) from user文章源自懂站帝-http://www.sfdkj.com/11987.html

文章源自懂站帝-http://www.sfdkj.com/11987.html
十三、Now()文章源自懂站帝-http://www.sfdkj.com/11987.html
返回当前时间文章源自懂站帝-http://www.sfdkj.com/11987.html
SELECT NOW() FROM user文章源自懂站帝-http://www.sfdkj.com/11987.html

文章源自懂站帝-http://www.sfdkj.com/11987.html
示例:文章源自懂站帝-http://www.sfdkj.com/11987.html
--查找emp表文章源自懂站帝-http://www.sfdkj.com/11987.html
select * from emp;文章源自懂站帝-http://www.sfdkj.com/11987.html
--查找emp表的sal文章源自懂站帝-http://www.sfdkj.com/11987.html
select a.SAL from emp a;文章源自懂站帝-http://www.sfdkj.com/11987.html
--查找emp表的ename文章源自懂站帝-http://www.sfdkj.com/11987.html
select a.ename from emp a;文章源自懂站帝-http://www.sfdkj.com/11987.html
--emp表的sal*10文章源自懂站帝-http://www.sfdkj.com/11987.html
select a.SAL*10 from emp a;文章源自懂站帝-http://www.sfdkj.com/11987.html
--emp表的sal的平均值文章源自懂站帝-http://www.sfdkj.com/11987.html
select avg(a.sal) from emp a;文章源自懂站帝-http://www.sfdkj.com/11987.html
--emp表的sal的总和文章源自懂站帝-http://www.sfdkj.com/11987.html
select sum(a.sal) from emp a;文章源自懂站帝-http://www.sfdkj.com/11987.html
--emp表的sal的max文章源自懂站帝-http://www.sfdkj.com/11987.html
select max(a.sal) from emp a;文章源自懂站帝-http://www.sfdkj.com/11987.html
--emp表的sal的min文章源自懂站帝-http://www.sfdkj.com/11987.html
select min(a.sal) from emp a;文章源自懂站帝-http://www.sfdkj.com/11987.html
--emp表中sal<1000的信息文章源自懂站帝-http://www.sfdkj.com/11987.html
select * from emp where sal<1000;文章源自懂站帝-http://www.sfdkj.com/11987.html
--ename中含有A的信息文章源自懂站帝-http://www.sfdkj.com/11987.html
select ename from emp where ename like'%A%';文章源自懂站帝-http://www.sfdkj.com/11987.html
--emp中ename不含有A的信息文章源自懂站帝-http://www.sfdkj.com/11987.html
select * from emp where ename not like'%A%';文章源自懂站帝-http://www.sfdkj.com/11987.html
--查询系统时间文章源自懂站帝-http://www.sfdkj.com/11987.html
select sysdate from dual;文章源自懂站帝-http://www.sfdkj.com/11987.html
--计算薪资小于5000的员工的工资总和文章源自懂站帝-http://www.sfdkj.com/11987.html
select sum(sal) from emp where sal<5000 ;文章源自懂站帝-http://www.sfdkj.com/11987.html
--计算工资不高于平均工资的工资总和文章源自懂站帝-http://www.sfdkj.com/11987.html
select sum(sal) from emp where sal<(select avg(sal) from emp);文章源自懂站帝-http://www.sfdkj.com/11987.html
--计算工资小于4000的员工的平均工资文章源自懂站帝-http://www.sfdkj.com/11987.html
select avg((select sum(sal) from emp where sal<4000)) from emp;文章源自懂站帝-http://www.sfdkj.com/11987.html
--查询薪水低于100000的员工姓名和sal文章源自懂站帝-http://www.sfdkj.com/11987.html
select ename,sal from emp where sal<100000;文章源自懂站帝-http://www.sfdkj.com/11987.html
--计算20号部门的员工的最高工资和最低工资文章源自懂站帝-http://www.sfdkj.com/11987.html
select max(sal),min(sal) from emp where deptno=20;文章源自懂站帝-http://www.sfdkj.com/11987.html
--查询工资大于1000,并且是20号部门的员工的全部信息文章源自懂站帝-http://www.sfdkj.com/11987.html
select * from emp where sal>1000 and deptno=20;文章源自懂站帝-http://www.sfdkj.com/11987.html
--求最高工资的员工的员工姓名和部门名称文章源自懂站帝-http://www.sfdkj.com/11987.html
select ename,deptno,sal from emp where sal=(select max(sal) from emp);文章源自懂站帝-http://www.sfdkj.com/11987.html
--将员工薪水小于5000且部门平均薪水大于1000的部门标号列出,按部门平均薪水降序排序文章源自懂站帝-http://www.sfdkj.com/11987.html
select deptno from emp where sal<5000 group by deptno having avg(sal) >1000;文章源自懂站帝-http://www.sfdkj.com/11987.html
select sal from emp order by sal desc;文章源自懂站帝-http://www.sfdkj.com/11987.html
Order by *** desc文章源自懂站帝-http://www.sfdkj.com/11987.html
--查找表emp文章源自懂站帝-http://www.sfdkj.com/11987.html
select * from emp ;文章源自懂站帝-http://www.sfdkj.com/11987.html
--根据用户名“Smiths”查找他所在的部门文章源自懂站帝-http://www.sfdkj.com/11987.html
select deptno from emp where ename='SMITH';文章源自懂站帝-http://www.sfdkj.com/11987.html
--查询每个部门的最高工资的人员名称文章源自懂站帝-http://www.sfdkj.com/11987.html
select e.ename,e.deptno,e.sal from (select deptno as did ,max(sal) as m from emp group by deptno ) s,emp e,dept d where e.sal=s.m and s.did=e.deptno and d.deptno=e.deptno;文章源自懂站帝-http://www.sfdkj.com/11987.html
--查询“Jones”之后第一个进入公司的人文章源自懂站帝-http://www.sfdkj.com/11987.html
select * from emp where hiredate=(select min(hiredate) from emp where hiredate>(select hiredate from emp where ename='JONES')) ;文章源自懂站帝-http://www.sfdkj.com/11987.html
--5.查找工资最高的部门名称和工资最低的部门名称及工资文章源自懂站帝-http://www.sfdkj.com/11987.html
select d.dname,e.sal from emp e,dept d where e.deptno=d.deptno and sal=(select max(m) from (select deptno,max(sal) as m from emp e group by deptno) s)文章源自懂站帝-http://www.sfdkj.com/11987.html
union文章源自懂站帝-http://www.sfdkj.com/11987.html
select d.dname,e.sal from emp e,dept d where e.deptno=d.deptno and sal=(select min(m) from (select deptno,min(sal) as m from emp e group by deptno) s)文章源自懂站帝-http://www.sfdkj.com/11987.html
--创建表文章源自懂站帝-http://www.sfdkj.com/11987.html
create table student(文章源自懂站帝-http://www.sfdkj.com/11987.html
StudentId number (6),--学号文章源自懂站帝-http://www.sfdkj.com/11987.html
LoginPwd varchar(20),--密码文章源自懂站帝-http://www.sfdkj.com/11987.html
StudentName varchar(50),--姓名文章源自懂站帝-http://www.sfdkj.com/11987.html
Sex char(2),--性别文章源自懂站帝-http://www.sfdkj.com/11987.html
Gradeld Number(6),--所在年级文章源自懂站帝-http://www.sfdkj.com/11987.html
Phone number(15),--联系电话文章源自懂站帝-http://www.sfdkj.com/11987.html
Address varchar2(255),--现住址文章源自懂站帝-http://www.sfdkj.com/11987.html
BornDate Date,--出生日期文章源自懂站帝-http://www.sfdkj.com/11987.html
Emile varchar2(50)--电子邮件文章源自懂站帝-http://www.sfdkj.com/11987.html
);文章源自懂站帝-http://www.sfdkj.com/11987.html
--添加数据文章源自懂站帝-http://www.sfdkj.com/11987.html
insert into student values(1001,'123456','赵六','男',1507,120,'北京','23-5月-1995','@10422');文章源自懂站帝-http://www.sfdkj.com/11987.html
insert into student values(1002,'123456','王五','女',1507,110,'北京','23-5月-1995','@10422');文章源自懂站帝-http://www.sfdkj.com/11987.html
insert into student values(1003,'123456','张三','男',1507,120,'北京','23-5月-1995','@10422');文章源自懂站帝-http://www.sfdkj.com/11987.html
insert into student values(1004,'123456','李四','女',1507,110,'北京','23-5月-1995','@10422');文章源自懂站帝-http://www.sfdkj.com/11987.html
--提交文章源自懂站帝-http://www.sfdkj.com/11987.html
commit;文章源自懂站帝-http://www.sfdkj.com/11987.html
--查询此表文章源自懂站帝-http://www.sfdkj.com/11987.html
select * from student;文章源自懂站帝-http://www.sfdkj.com/11987.html
--根据条件修改文章源自懂站帝-http://www.sfdkj.com/11987.html
update student set studentname='孙七',loginpwd='666666' where studentid=1001;文章源自懂站帝-http://www.sfdkj.com/11987.html
select * from student;文章源自懂站帝-http://www.sfdkj.com/11987.html
--根据条件删除文章源自懂站帝-http://www.sfdkj.com/11987.html
delete from student where studentid=1002;文章源自懂站帝-http://www.sfdkj.com/11987.html
select * from student;文章源自懂站帝-http://www.sfdkj.com/11987.html
--增加字段文章源自懂站帝-http://www.sfdkj.com/11987.html
ALTER TABLE 表名称 ADD(列名称 数据类型 [DEFAULT 默认值],列名称 数据类型 [DEFAULT 默认值],…)文章源自懂站帝-http://www.sfdkj.com/11987.html
--删除此表文章源自懂站帝-http://www.sfdkj.com/11987.html
drop table student;文章源自懂站帝-http://www.sfdkj.com/11987.html
--B卷 创建表空间文章源自懂站帝-http://www.sfdkj.com/11987.html
create tablespace mytestspace datafile 'e:week3.dbf' size 2M;文章源自懂站帝-http://www.sfdkj.com/11987.html
--创建用户文章源自懂站帝-http://www.sfdkj.com/11987.html
create user zhangsan identified by zhangsan default tablespace mytestspace文章源自懂站帝-http://www.sfdkj.com/11987.html
temporary tablespace temp;文章源自懂站帝-http://www.sfdkj.com/11987.html
--创建角色并授权文章源自懂站帝-http://www.sfdkj.com/11987.html
create role fang;文章源自懂站帝-http://www.sfdkj.com/11987.html
grant create table to fang;文章源自懂站帝-http://www.sfdkj.com/11987.html
grant fang to zhangsan;文章源自懂站帝-http://www.sfdkj.com/11987.html
grant dba to zhangsan;文章源自懂站帝-http://www.sfdkj.com/11987.html
--创建表文章源自懂站帝-http://www.sfdkj.com/11987.html
create table teacher (文章源自懂站帝-http://www.sfdkj.com/11987.html
tid number primary key,文章源自懂站帝-http://www.sfdkj.com/11987.html
tname varchar2(50),文章源自懂站帝-http://www.sfdkj.com/11987.html
tdate date,文章源自懂站帝-http://www.sfdkj.com/11987.html
address varchar2(100)文章源自懂站帝-http://www.sfdkj.com/11987.html
);文章源自懂站帝-http://www.sfdkj.com/11987.html
select * from teacher;文章源自懂站帝-http://www.sfdkj.com/11987.html
--创建序列文章源自懂站帝-http://www.sfdkj.com/11987.html
create sequence teachers_sequence minvalue 1 maxvalue 100 (最小值,最大值)cycle (循环)increment by 1(步长值) start with 1(从1开始);文章源自懂站帝-http://www.sfdkj.com/11987.html
insert into teacher values(teachers_sequence.nextval,'小李','01-1月-1999','北京');文章源自懂站帝-http://www.sfdkj.com/11987.html
insert into teacher values(teachers_sequence.nextval,'小张',to_date('1982-1-1','yyyy-mm-dd'),'北京');文章源自懂站帝-http://www.sfdkj.com/11987.html

文章源自懂站帝-http://www.sfdkj.com/11987.html
说明:所有的面试题目都不是一成不变的,特别是像一线大厂,上面的少量学习内容只是给大家一个借鉴作用,最主要的是给自己增加知识的储备,有备无患。最后给大家分享Spring系列的学习笔记和面试题,包含spring面试题、spring cloud面试题、spring boot面试题、spring教程笔记、spring boot教程笔记、最新阿里巴巴开发手册(63页PDF总结)、2022年Java面试手册。文章源自懂站帝-http://www.sfdkj.com/11987.html