【笔记】数据库--初级SQL语言

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Aprilxdy/article/details/68944723

数据库

SQL语句

  • 基本类型:
  • create table table_name ( A  D, …… , A D , <完整性约束>,…, ..);
  • insert into table_name value ( xxx, ‘xxx’, … , xxx );
  • delete from table_name; 删除元组,保留关系;
  • drop table table_name;   全部删除;
  • alter table  table_name add A D;在已有关系中增加属性 默认取值为null
  • alter table table_name drop A;   从关系中去掉属性(很多数据库系统不支持)

SQL查询的基本结构

单关系查询:

select  属性 from  table;  例: 找出所有教师所在的系名;

select  distinct  属性 from  table;删除重复;

select  all  属性  from  table显示指明不去除重复;   属性:property

select  name  from  instructorwhere  dept_name = ‘Comp. Sci’ and salary > 70000; 

// 找出所有在computer science系且工资超过70000美元的教师的姓名

where语句中可以使用 and  or  not

多关系查询

select:用于列出查询结果中所需要的属性;

from  查询求值中需要访问的关系列表;

where:作用在from子句中关系的属性上的谓词;

理解顺序:  from。。。 where。。。 select

例:

select name, instructor.dept_name, building

from instructor, department

where instructor.dept_name = department.dept_name;

找出所有教师(instructor)的姓名,以及她们所在系的名称和系所在建筑的名称;

属性名前加上关系名作为前缀,表示该属性来自于哪个关系。

自然连接(natural join)

自然连接运算作用与两个关系,并产生一个关系作为结果。

1对于大学中所有讲授课程的教师,找出它们的姓名以及所讲述的所有课程标识。

select name, course_id

from instructor, teaches

where instructor.ID = teaches.ID;

若用自然连接运算则更简洁:

select name, course_id

from instructor natural join teaches;

2:允许用户指定需要哪些列项 ( joing using语句 )

select name, title

from ( instructor natural join teaches )join course using ( course_id );

附加的基本运算

更名运算:

old-name as new-name as语句既可以出现在select子句中,也可出现在from子句中。

1

select name as instructor_name, course_id

from instructor, teaches

where instructor.ID = teaches.ID;

2:

select T.name, S.course_id

from instructor as T, teachesas S

where T.ID = S.ID;       //from。。。 where。。。 select

字符串运算:

1> SQL使用单引号来标示字符串: ‘Computer’、‘it''s right’ //单引号在字符串内  用两个单引号字符来表示。

/*SQL标准中 字符串的相等运算大小写敏感,但在MySQL和SQL Sever中不区分大小写 */

2> 模式匹配:字符串可以使用  like 操作符来实现模式匹配
     百分号%:匹配任意子串。
     下划线_:匹配任意字符。//大小写敏感
例:
      'Intro%' : 匹配任何以“Intro”开头的字符串;
      '%Comp%': 匹配任何包含“Comp”子串的字符串;
      '___': 匹配只含三个字符的字符串;
      '___%': 匹配至少含三个字符的字符串。

例:找出所在建筑名称中包含子串‘Waston’的所有系名。
select dept_name
from department
where building like '%Waston%';

3> 关于转义字符:
以\为例:
like  'ab\%cd%'   escape '\' : 匹配所有以“ab%cd”开头的字符串,其中escape关键词来定义转义字符;

补充: SQL允许使用not like 比较运算符搜寻不匹配项。一些数据库还提供like运算的辩题,不区分大小写。 similar to

select 子句中的属性说明

1> 星号 ' * ' 表示“所有的属性”:
例:选中instructor的所有属性。

select instructor.*

from instructor, teaches

where instructor.ID = teaches.ID;

补充:形如select * 的select子句表示from子句结果关系的所有属性都被选中。


排列选组的显示次序

1> order by 子句:使查询结果中的元组按排列顺序显示。

例1:按字母顺序列出在Physics系的所有教师。

select name

from instructor

where dept_name = 'Physics'

order by name;

例2:按salary的讲叙列出整个instructor关系;如果有几位教师的工资相同,按姓名升序排列。

select *

from instructor 

order by salary desc, name asc; //desc表示降序, asc表示升序。


where子句谓词
1>比较运算符  between / not between
例:找出工资在90,000美元和100,000美元之间的教师姓名。
select name
from instructor 
where salary between 90000 and 100000;  // where salary <= 100000 and salary >= 90000;

2> 允许用记号(v1, v2, ..., vn)表示一个 v1, v2,...,vn n维元组。
例:

select name, course_id

from instructor, teaches

where ( instructor.ID, dept_name ) = ( teaches.ID, 'Biology' ); //where instructor.ID = teaches.ID and dept_name = 'Biology' ;


集合运算


并运算 union

交运算

差运算


空值

聚集函数

嵌套子查询


数据库的修改

删除、插入、更新



猜你喜欢

转载自blog.csdn.net/Aprilxdy/article/details/68944723
今日推荐