⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sql数据库完全手册√五行网络.htm

📁 平时搜集的数据库操作文档
💻 HTM
📖 第 1 页 / 共 4 页
字号:
                  NULL;<BR>  &nbsp;&nbsp;&nbsp; 
                  [,UNIQUE]将列按照其规定的顺序进行排列,如不指定排列顺序,则按列的定义顺序排列;<BR>  &nbsp;&nbsp;&nbsp; 
                  [PRIMARY&nbsp;&nbsp; 
                  KEY]用于指定表的主键(即关系中的主属性),实体完整性约束条件规定:主键必须是唯一的,非空的;<BR>  &nbsp;&nbsp;&nbsp; 
                  [,FOREIGN&nbsp; KEY&nbsp; (列名[,列名]......)&nbsp; 
                  REFERENCE&lt;表名&gt;(列名[,列名]......)]是用于指定外键参照完整性约束条件,FOREIGN&nbsp; 
                  KEY指定相关列为外键,其参照对象为另外一个表的指定列,即使用REFERENCE引入的外表中的列,当不指定外表列名时,系统将默认其列名与参照键的列名相同,要注意的是:使用外键时必须使用参照,另外数据的外键参照完整性约束条件规定:外键的值要么与相对应的主键相同,要么为空值(具体由实现系统不同而异)<BR>  &nbsp;&nbsp;&nbsp; 
                  [,CHECK]用于使用指定条件对存入表中的数据进行检查,以确定其合法性,提高数据的安全性。<BR>  &nbsp;&nbsp;&nbsp; 
                  例:要建立一个学生情况表(student)<BR>  &nbsp;&nbsp;&nbsp; CREATE&nbsp; 
                  TABLE&nbsp; student&nbsp; <A 
                  href="file://创/">file://创/</A>建基本表student<BR>  &nbsp;&nbsp;&nbsp; 
                  (st_class&nbsp; CHAR(8),// 
                  定义列st_class班级,数据类型为8位定长字符串<BR>  &nbsp;&nbsp;&nbsp; st_no&nbsp; 
                  CHAR(10)&nbsp; NOT&nbsp; 
                  NULL,//定义列st_no学号,类型为10位定长字符串,非空<BR>  &nbsp;&nbsp;&nbsp; 
                  st_name&nbsp; CHAR(8) NOT&nbsp; 
                  NULL,//定义列st_name姓名,类型为8位定长字符串,非空<BR>  &nbsp;&nbsp;&nbsp; 
                  st_sex 
                  CHAR(2),//定义列st_sex性别,类型为2位定长字符串<BR>  &nbsp;&nbsp;&nbsp; 
                  st_age SMALLINT,//定义列st_age年龄,类型为短整型<BR>  &nbsp;&nbsp;&nbsp; 
                  PRIMARY&nbsp; KEY 
                  (st_no))//定义st_no学号为主键。<BR>  &nbsp;&nbsp;&nbsp; 
                  例:要建立课程设置表(subject)<BR>  &nbsp;&nbsp;&nbsp; CREATE&nbsp; 
                  TABLE&nbsp; subject//创建基本表subject<BR>  &nbsp;&nbsp;&nbsp; 
                  (su_no&nbsp; CHAR(4) NOT&nbsp; NULL,// 
                  定义列su_no课号,类型为4位定长字符串,非空<BR>  &nbsp;&nbsp;&nbsp; su_subject 
                  CHAR(20) NOT&nbsp; NULL,// 
                  定义列su_subject课程名,类型为20位定长字符串,非空<BR>  &nbsp;&nbsp;&nbsp; 
                  su_credit&nbsp; INTEGER,// 
                  定义列su_credit学分,类型为长整数<BR>  &nbsp;&nbsp;&nbsp; su_period&nbsp; 
                  INTEGER,//定义列su_period学时,类型为长整数<BR>  &nbsp;&nbsp;&nbsp; 
                  su_preno 
                  CHAR(4),//定义列su_preno先修课号,类型为4位定长字符串<BR>  &nbsp;&nbsp;&nbsp; 
                  PRIMARY&nbsp; 
                  KEY(su_no))//定义su_no课号为主键。<BR>  &nbsp;&nbsp;&nbsp; 
                  例:要建立学生选课表(score)<BR>  &nbsp;&nbsp;&nbsp; CREATE TABLE 
                  score&nbsp; <A 
                  href="file://创/">file://创/</A>建基本表score<BR>  &nbsp;&nbsp;&nbsp; 
                  (st_no&nbsp; 
                  CHAR(10),//定义列st_no学号,类型为10位定长字符串<BR>  &nbsp;&nbsp;&nbsp; 
                  su_no&nbsp; 
                  CHAR(4),//定义列su_no课号,类型为4位定长字符串<BR>  &nbsp;&nbsp;&nbsp; 
                  sc_score&nbsp; INTEGER&nbsp; 
                  NULL,//定义列sc_score,类型为长整形,可以为空值<BR>  &nbsp;&nbsp;&nbsp; 
                  FOREIGN&nbsp; KEY (st_no) REFERENCE&nbsp; 
                  student,//从表student中引入参照外键st_no,以确保本表与表student的关联与同步<BR>  &nbsp;&nbsp;&nbsp; 
                  FOREIGN&nbsp; KEY (suno) REFERENCE&nbsp; 
                  subject)//从表subject中引入参照外键su_no,以确保本表与表subject的关联与同步<BR>  &nbsp;&nbsp;&nbsp; 
                  (2)基本表的删除:用以从数据库中删除一个基本表及其全部内容,其语句格式为:<BR>  &nbsp;&nbsp;&nbsp; 
                  DROP TABLE[&lt;数据库名&gt;.]表名<BR>  &nbsp;&nbsp;&nbsp; 
                  例如:将上面建立的表都删除<BR>  &nbsp;&nbsp;&nbsp; DROP TABLE&nbsp; 
                  student,subject,score<BR>  &nbsp;&nbsp;&nbsp; 
                  (3)基本表的修改:在基本表建立并使用一段时间后,可能需要根据实际要求对基本表的结构进行修改,即增加新的属性或删除属性。<BR>  &nbsp;&nbsp;&nbsp; 
                  增加属性的语句格式为:<BR>  &nbsp;&nbsp;&nbsp; ALTER TABLE 
                  [&lt;数据库名&gt;.]表名 ADD<BR>  &nbsp;&nbsp;&nbsp; (&lt;列名&gt; 数据类型 
                  [缺省值] [NOT&nbsp; NULL / NULL]<BR>  &nbsp;&nbsp;&nbsp; 
                  [,&lt;列名&gt; 数据类型[缺省值][NOT&nbsp; NULL / 
                  NULL]]......<BR>  &nbsp;&nbsp;&nbsp; [,UNIQUE 
                  (列名[,列名]......)]<BR>  &nbsp;&nbsp;&nbsp; [,PRIMARY&nbsp; 
                  KEY(列名)]<BR>  &nbsp;&nbsp;&nbsp; [,FOREIGN&nbsp; 
                  KEY(列名[,列名]......) REFERENCE&nbsp; 
                  &lt;表名&gt;(列名[,列名]......)]<BR>  &nbsp;&nbsp;&nbsp; 
                  [,CHECK(条件)][其它参数])<BR>  &nbsp;&nbsp;&nbsp; 
                  例如:在基本表student中加入列stborn出生日期,数据类型为DATE,且不能为空值<BR>  &nbsp;&nbsp;&nbsp; 
                  ALTER&nbsp; TABLE&nbsp; student&nbsp; ADD&nbsp; (stborn&nbsp; 
                  DATE&nbsp; NOT&nbsp; NULL)<BR>  &nbsp;&nbsp;&nbsp; 
                  删除属性的语句格式为:<BR>  &nbsp;&nbsp;&nbsp; ALTER&nbsp; TABLE&nbsp; 
                  [&lt;数据库名&gt;.]表名&nbsp; DROP<BR>  &nbsp;&nbsp;&nbsp; ( 
                  &lt;列名&gt;&nbsp; 数据类型 [缺省值][NOT&nbsp; NULL / 
                  NULL]<BR>  &nbsp;&nbsp;&nbsp; [,&lt;列名&gt;&nbsp; 数据类型 
                  [缺省值][NOT&nbsp; NULL / NULL]]......)<BR>  &nbsp;&nbsp;&nbsp; 
                  例如:将基本表student中的列st_age删除<BR>  &nbsp;&nbsp;&nbsp; ALTER&nbsp; 
                  TABLE student&nbsp; DROP (st_age)<BR>  &nbsp;&nbsp;&nbsp; 
                  3.视图定义与删除<BR>  &nbsp;&nbsp;&nbsp; 
                  在SQL中,视图是外模式一级数据结构的基本单位。它是从一个或几个基本表中导出的表,是从现有基本表中抽取若干子集组成用户的“专用表”。这种构造方式必须使用SQL中的SELECT语句来实现。在定义一个视图时,只是把其定义存放在系统的数据中,而并不直接存储视图对应的数据,直到用户使用视图时才去求得对应的数据。<BR>  &nbsp;&nbsp;&nbsp; 
                  (1)视图的定义:定义视图可以使用CREATE&nbsp; 
                  VIEW语句实现,其语句格式为:<BR>  &nbsp;&nbsp;&nbsp; CREATE&nbsp; 
                  VIEW&nbsp; 视图名 AS SELECT语句<BR>  &nbsp;&nbsp;&nbsp; 
                  从一个基本表中导出视图:<BR>  &nbsp;&nbsp;&nbsp; 
                  例:从基本表student中导出只包括女学生情况的视图<BR>  &nbsp;&nbsp;&nbsp; 
                  CREATE&nbsp; VIEW&nbsp; WOMANVIEW&nbsp; AS <A 
                  href="file://创/">file://创/</A>建一个视图WOMANVIEW<BR>  &nbsp;&nbsp;&nbsp; 
                  SELECT&nbsp; st_class,st_no,st_name,st_age <A 
                  href="file://选/">file://选/</A>择列st_class,st_no,st_name,st_age显示<BR>  &nbsp;&nbsp;&nbsp; 
                  FROM&nbsp; student <A 
                  href="file://从/">file://从/</A>基本表student引入<BR>  &nbsp;&nbsp;&nbsp; 
                  WHERE 
                  st_sex=‘女’//引入条件为性别为“女”,注意字符变量都使用单引号引用<BR>  &nbsp;&nbsp;&nbsp; 
                  从多个基本表中导出视图:<BR>  &nbsp;&nbsp;&nbsp; 
                  例如:从基本表student和score中导出只包括女学生且分数在60分以上的视图<BR>  &nbsp;&nbsp;&nbsp; 
                  CREATEVIEW&nbsp; WOMAN_SCORE&nbsp; AS <A 
                  href="file://定/">file://定/</A>义视图WOMANSCORE<BR>  &nbsp;&nbsp;&nbsp; 
                  SELECT&nbsp; 
                  student.st_class,student.st_no,student.st_name,student.st_age,score.sc_score 
                  <A 
                  href="file://有/">file://有/</A>选择性显示相关列<BR>  &nbsp;&nbsp;&nbsp; 
                  FROM&nbsp; student.score <A 
                  href="file://从/">file://从/</A>基本表student和score中引入<BR>  &nbsp;&nbsp;&nbsp; 
                  WHERE&nbsp; student.st_sex=‘女’AND&nbsp;&nbsp; 
                  score.sc_score&gt;=60&nbsp; AND&nbsp; 
                  student.st_no=score.st_no&nbsp; <A 
                  href="file://选/">file://选/</A>择条件:性别为“女” 
                  且分数在60分以上。并使用st_no将两表联系起来。<BR>  &nbsp;&nbsp;&nbsp; 
                  以后如果进行这一视图的应用,则只需使用语句<BR>  &nbsp;&nbsp;&nbsp; SELECT * FROM 
                  WOMAN_SCORE <A 
                  href="file://其/">file://其/</A>中“*”为通配符,代表所有元素<BR>  &nbsp;&nbsp;&nbsp; 
                  (2)视图的删除:用于删除已不再使用的视图,其语句格式如下:<BR>  &nbsp;&nbsp;&nbsp; 
                  DROP&nbsp; VIEW&nbsp; 视图名<BR>  &nbsp;&nbsp;&nbsp; 
                  例:将上面建立的WOMAN_SCORE视图删除<BR>  &nbsp;&nbsp;&nbsp; DROP&nbsp; 
                  VIEW&nbsp; WOMAN_SCORE<BR>  &nbsp;&nbsp;&nbsp; 
                  4.索引的定义与删除<BR>  &nbsp;&nbsp;&nbsp; 
                  索引属于物理存储概念,而不是逻辑的概念。在SQL中抛弃了索引概念,直接使用主键概念。值得一提的是,有些关系DBMS同时包括索引机制和主键机制,这里我们推荐使用主键机制,因为它对系统资源占用较低且效率较高。<BR>  &nbsp;&nbsp;&nbsp; 
                  (1)索引的定义:索引是建立在基本表之上的,其语句格式为:<BR>  &nbsp;&nbsp;&nbsp; CREATE 
                  [UNIQUE] INDEX&nbsp; 索引名&nbsp; ON<BR>  &nbsp;&nbsp;&nbsp; 
                  [&lt;数据库名&gt;.]表名(列名 [ASC/DESC][,列名 
                  [ASC/DESC]]......)<BR>  &nbsp;&nbsp;&nbsp; 
                  这里,保留字UNIQUE表示基本表中的索引值不允许重复,若缺省则表示索引值在表中允许重复;DESC表示按索引键降序排列,若缺省或ASC表示升序排列。<BR>  &nbsp;&nbsp;&nbsp; 
                  例:对基本表student中的st_no和st_age建立索引,分别为升序与降序,且索引值不允许重复<BR>  &nbsp;&nbsp;&nbsp; 
                  CREATE&nbsp; UNIQUE&nbsp; INDEX&nbsp; STINDEX&nbsp; 
                  ON//创建索引STINDEX<BR>  &nbsp;&nbsp;&nbsp; student(st_no 
                  ASC,st_age 
                  DESC)//对student中的st_no和st_age建立索引<BR>  &nbsp;&nbsp;&nbsp; 
                  (2)索引的删除:<BR>  &nbsp;&nbsp;&nbsp; DROP&nbsp; INDEX&nbsp; 
                  索引名<BR>  &nbsp;&nbsp;&nbsp; 
                  例:删除上面建立的索引STINDEX<BR>  &nbsp;&nbsp;&nbsp; DROP&nbsp; 
                  INDEX&nbsp; STINDEX<BR>  ##2&nbsp;&nbsp;&nbsp; 
                  (二)数据查询<BR>  &nbsp;&nbsp;&nbsp; 
                  SQL是一种查询功能很强的语言,只要是数据库存在的数据,总能通过适当的方法将它从数据库中查找出来。SQL中的查询语句只有一个:SELECT,它可与其它语句配合完成所有的查询功能。SELECT语句的完整语法,可以有6个子句。完整的语法如下:<BR>  &nbsp;&nbsp;&nbsp; 
                  SELECT&nbsp; 目标表的列名或列表达式集合<BR>  &nbsp;&nbsp;&nbsp; FROM 
                  基本表或(和)视图集合<BR>  &nbsp;&nbsp; [WHERE条件表达式]<BR>  &nbsp;&nbsp; 
                  [GROUP BY列名集合<BR>  &nbsp;&nbsp; 
                  [HAVING组条件表达式]]<BR>  &nbsp;&nbsp; [ORDER 
                  BY列名[集合]…]<BR>  &nbsp;&nbsp;&nbsp; 
                  整个语句的语义如下:从FROM子句中列出的表中,选择满足WHERE子句中给出的条件表达式的元组,然后按GROUPBY子句(分组子句)中指定列的值分组,再提取满足HAVING子句中组条件表达式的那些组,按SELECT子句给出的列名或列表达式求值输出。ORDER子句(排序子句)是对输出的目标表进行重新排序,并可附加说明ASC(升序)或DESC(降序)排列。 
                  <BR>&nbsp;<BR>&nbsp;</P>
                  <P><STRONG>SQL数据库完全手册</STRONG>_2 <BR>&nbsp;&nbsp; <BR>&nbsp; 
                  <BR>&nbsp;在WHERE子句中的条件表达式F中可出现下列操作符和运算函数:<BR>  &nbsp;&nbsp;&nbsp; 
                  算术比较运算符:&lt;,&lt;=,&gt;,&gt;=,=,&lt;&gt;。<BR>  &nbsp;&nbsp;&nbsp; 
                  逻辑运算符:AND,OR,NOT。<BR>  &nbsp;&nbsp;&nbsp; 
                  集合运算符:UNION(并),INTERSECT(交),EXCEPT(差)。<BR>  &nbsp;&nbsp;&nbsp; 
                  集合成员资格运算符:IN,NOT&nbsp; IN<BR>  &nbsp;&nbsp;&nbsp; 
                  谓词:EXISTS(存在量词),ALL,SOME,UNIQUE。<BR>  &nbsp;&nbsp;&nbsp; 
                  聚合函数:AVG(平均值),MIN(最小值),MAX(最大值),SUM(和),COUNT(计数)。<BR>  &nbsp;&nbsp;&nbsp; 
                  F中运算对象还可以是另一个SELECT语句,即SELECT语句可以嵌套。<BR>  &nbsp;&nbsp;&nbsp; 
                  上面只是列出了WHERE子句中可出现的几种主要操作,由于WHERE子句中的条件表达式可以很复杂,因此SELECT句型能表达的语义远比其数学原形要复杂得多。<BR>  &nbsp;&nbsp;&nbsp; 
                  下面,我们以上面所建立的三个基本表为例,演示一下SELECT的应用:<BR>  &nbsp;&nbsp;&nbsp; 
                  1.无条件查询<BR>  &nbsp;&nbsp;&nbsp; 
                  例:找出所有学生的的选课情况<BR>  &nbsp;&nbsp;&nbsp; SELECT 
                  st_no,su_no<BR>  &nbsp;&nbsp;&nbsp; FROM 
                  score<BR>  &nbsp;&nbsp;&nbsp; 
                  例:找出所有学生的情况<BR>  &nbsp;&nbsp;&nbsp; 
                  SELECT*<BR>  &nbsp;&nbsp;&nbsp; FROM 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -