数据库的语法

上传人:do****y1 文档编号:188683432 上传时间:2023-02-20 格式:DOCX 页数:15 大小:61.83KB
收藏 版权申诉 举报 下载
数据库的语法_第1页
第1页 / 共15页
数据库的语法_第2页
第2页 / 共15页
数据库的语法_第3页
第3页 / 共15页
资源描述:

《数据库的语法》由会员分享,可在线阅读,更多相关《数据库的语法(15页珍藏版)》请在装配图网上搜索。

1、添加列:alter table table_namecolumn_name column_properties;查询表结构:desc| table_name;新建一窗口一命令窗口删除列:alter table table_nameDrop colcolumn_name;修改列属性:alter table table_ namemodify column_name column_properties;删除表:drop table table_name;添加数据:insert into table_name (column1,column2修改表时添加非空约束alter table table_n

2、amemodifycolumn_name constraint constraint_name not null;修改表时添加主键约束:Alter tablq table_nameAdd constraint constraint_name primary key (column_name);修改表时添加检查性约束:Alter tabld table_name修改表时添加唯一约束:alter tableAdd constraint constriant_name check(expression);table_nameconstraint constraint_name |uniqu(colu

3、mn_name);修改表时添加默认约束:modifyter table table namecolumn_name default(default_value);修改表时添加外键约束:Alter table table_nameAdd condition condition_nameTable nameforeign kej(column_ _name)eferences删除约束语法:alter table table_namedrop constraint constraint_name;检索(查询)语法:select select_listtable_name wherq conditio

4、n_expression加别名:select name1,stock as取括号任意一个值: where column_name in(value1,value2.)在两者之间: where column_name between.and.模糊查询:where column_name like.is null 用法: where column_name is null返回表中的行数:select Eount(*) column_name去掉完全重复的记录:select distinct column_ name.自然连接(内连接):select select_list from! table1

5、 joi# table2 table1.column=table2.column做降序或升序排序:select select_listtable_name Where conditionorde:r by colunm_nameasc/desc左外连接:select select_listtable1table1.column=table2.column右外连接:select select_list from| table1 right jointable2 ontable1.column=table2.column全外连接:select select_listtable1 full join

6、 table2table1.column=table2.column自连接:select distinct select_list from table1 a join table1 ba.stock=b.stock and a.idb.id嵌套:Select select_list fro table_name where condition)table_name where column_name(select column_name from查询结果创建成新表:create table table_nameselect select listtable_name where condit

7、ion测试条件是否存在:select select_listtable_name where existselect select_list from table_name where condition);group by 分组:select column1,集合函数(column2) column1order by column_nametable_name where condition group byhaving: selec select_list from table_name where codition group by| column having condition or

8、der by column集合操作符:select columun name1,fromfrom集合操作符 (union、union all、intersert、minus) select columun name2,序列语法:create sequence sequence_name increment by n递增值 start with n起始值 maxvalue最大值 minvalue最小值cache cache_value缓冲 cycle循环更新数据update语法:获取值:sequence_name.NEXTVALupdate table_name set column_name=

9、new_value,column_name2= new_value2where condition删除 delete: delete from table_namewhere condition册。除 truncate: truncate table table_name仓0建保存点:savepoin|savepoint_name回滚:rollback to savepoint_name pl/sql 结构:declare提交事务:创建索引:删除索引:视图创建:删除视图:createuniqueindex index_nametable_name(column_nameasc/descdrop

10、 index index_namecreate or replaceforce|noforce viewview_name(column_name,)selej_statement;viewview_name;(声明部分);begin(执行部分);exception、(执行异常部分);pl/sql 输出语句:dbms_output.put_line(value)声明变量:variable_name data_type:=value;:variable_name date_type default value;while.loop:while expression loopstatement;f

11、or.,loop:I in reverse lb.hb loopstatements;case.when:case expressionwhen expression_value1when expression_value2statements;statements;else statements;if.else:f expression1 then statement_a;expression2elsif expressions thenstatement_b;statement_c;Else |Statement_n;End if;loop.exit: loopstatements;f e

12、prssion |hen ex;end fi;end loop;select into:select select_list i|H variable_name from table_name where condition%tyep:variable_name definition_variable_nam%rowtype: variable_name table_name%rowtypeexception:exceptionwhen exception_namestatements;when others then statements;自定义异常:if exp then raise异常名

13、;(激活异常)(begin 内,exception 前)exceptionwhen expthen.end;无参存储过程:create| or replace procedureproc_name-声明/定义部分-; begin-执行部分-;有参存储过程:exception异常处理;proc_name)create or replace procedure (parameter_name1in data_type, -声明/定义部分-;begin-执行部分-;exception异常处理;end;含有默认值参数:parameter_name1in data_type|=null有输出参数存储过程

14、:create or replace procedure (parameter_name1|in/out/in outproc_nameTMdata type,)-声明/定义部分-;执行部分;exception异常处理;删除存储过程:drop procedure procedure_name触发器 trigger: create or replace tHB trigger_namebefore/after insert/update/delete|table namefor each row when(condition)declare定义部分;beginstatements;end;表空间

15、:create tablespace tablespace_namedatafile删除表空间:drop tablespace tablespacenameincluding contentsSql servermssql简要介绍基础语句:1、创建数据库CREATE DATABASE database-name2、删除数据库drop database dbname3、备份 sql server-创建 备份数据 的 device USE master EXEC sp_addumpdevice disk, testBack, c:mssql7backupbacksql.dat -开始备份 BACK

16、UP DATABASE pubs TO testBack4、创建新表 create table tabname(col1 type1 not null primary key,col2 type2 not null,.) 根据已有的表创建新表:A: create table tab_new like tab_old (使用旧表创建新表)B: create table tab_new asselect col1,col2. from tab_old definition only5、 删除新表:drop table tabname6、 增加一个列: Alter table tabname add

17、 column col type注:列增加后将不能删 除。DB2中列加上后数据类型也不能改变,唯一能改变的是增加varchar类型的长度。7、 添加主键:Alter table tabname add primary key(col) 说明:删除主键:Alter table tabname drop primary key(col)8、创建索弓I : create unique index idxname on tabname(col.) 删除索引:drop index idxname 注:索引是不可更改的,想更改必须删除重新建。有些语法会让mssql失效如: select * from aa

18、 where aa like %gg%等,索引的合理利用将大大提高mssql对大数据量的处理 效率。9、创建视图: create view viewname as select statement 册。除视图:drop view viewname10、几个简单的基本的sql语句选择:select * from table1 where 范围 插入:insert into table1(field1,field2) values(value1,value2) 删除:delete from table1 where 范围 更新:update table1 set field1=value1 whe

19、re 范围 查找:select * from tablel where fieldl like 雷锋 -like 的 语法很精妙,查资料!不过会让索引失效,而select * from tablel where fieldl like 雷锋这 句的的话索引是有效的!排序:select * from table1 order by field1,field2 desc 总数:select count * as totalcount from table1求和:select sum(field1) as sumvalue from table1平均:select avg(field1) as av

20、gvalue from table1最大:select max(field1) as maxvalue fromtable1最小:select min(field1) as minvalue from table111、几个高级查询运算词A: UNION运算符 UNION运算符通过组合其他两个结果表(例如TABLE1 和TABLE2)并消去表中任何重复行而派生出一个结果表。当ALL随UNION 一起使用 时(即UNION ALL),不消除重复行。两种情况下,派生表的每一行不是来自TABLE1就 是来自TABLE2。 B: EXCEPT运算符 EXCEPT运算符通过包括所有在 TABLE1中但不

21、在TABLE2中的行并消除所有重复行而派生出一个结果表。当 ALL随 EXCEPT 一起使用时(EXCEPT ALL),不消除重复行。C: INTERSECT运算符INTERSECT运算符通过只包括TABLE1和TABLE2中都有的行并消除所有重复行而派 生出一个结果表。当ALL随INTERSECT 一起使用时(INTERSECT ALL),不消除重复行。 注:使用运算词的几个查询结果行必须是一致的。12、使用外连接A、left outer join:左外连接(左连接):结果集几包括连接表的匹配行,也包括左连接表的所有行。SQL: select a.a, a.b, a.c, b.c, b.d,

22、 b.f from a LEFT OUT JOIN b ONa.a = b.c B:right outer join:右外连接(右连接):结果集既包括连接表的匹配连接行,也包括右连接表的所有行。C: full outer join:全外连接:不仅包括符号连接表的匹配行,还包括两个连接表中的所有记录。编辑本段mssql经典语句介绍1、说明:复制表(只复制结构,源表名:a新表名:b) (Access可用) 法一: select * intob from a where 11法二:select top 0 * into b from a2、说明:拷贝表(拷贝数据,源表名:a目标表名:b) (Acce

23、ss可用) insert into b(a, b, c) select d,e,f from a;3、 说明:跨数据库之间表的拷贝(具体数据使用绝对路径)(Access可用)insert into b(a, b, c) select d,e,f from b in 具体数据库 where 条件 例子:.from b in &Server.MapPath(.)&data.mdb & where.4、说明:子查询(表名 1: a 表名 2: b) select a,b,c from a where a IN (select d from b ) 或者:select a,b,c from a whe

24、re a IN (1,2,3)5、 说明:显示文章、提交人和最后回复时间select a.title,a.username,b.adddate from table a,(select max(adddate) adddate from table where table.title=a.title) b6、 说明:外连接查询(表名 1: a 表名 2: b)select a.a, a.b, a.c, b.c, b.d, b.f from a LEFT OUT JOIN b ON a.a = b.c7、说明:在线视图查询(表名 1 : a ) select * from (SELECT a,b

25、,c FROM a) T where t.a 1;8、说明:between的用法,between限制查询数据范围时包括了边界值,not between不包 括 select * from table1 where time between time1 and time2 select a,b,c, from table1 where a not between 数值 1 and 数值 29、 说明:in 的使用方法select * from tablel where a not in (值 1,值 2,值 4,值 6,)10、 说明:两张关联表,删除主表中已经在副表中没有的信息delete f

26、rom table1 where not exists ( select * from table2 where table1.field1=table2.field1 )11、 说明: 四表联查问题:select * from a left inner join b on a.a=b.b right inner join c on a.a=c.c inner join d on a.a=d.d where .12、 说明:旦程安排提前五分钟提醒 SQL: select * from日程安排where datediff(minute,f 开始时间,getdate()513、 说明:一条sql语

27、句搞定数据库分页select top 10 b.* from (select top 20 主键 字段,排序字段from表名order by排序字段desc) a,表名b where b.主键字段=a.主键字 段order by a.排序字段14、说明:前 10 条记录 select top 10 * form table1 where 范围15、说明:选择在每一组b值相同的数据中对应的a最大的记录的所有信息(类似这样的用法可以用于论坛每月排行榜,每月热销产品分析,按科目成绩排名,等等.)select a,b,cfrom tablename ta where a=(select max(a)

28、from tablename tb where tb.b=ta.b)16、说明:包括所有在TableA中但不在TableB和TableC中的行并消除所有重复行而派生出一个结果表(select a from tableA ) except (select a from tableB) except (select afrom tableC)17、说明:随机取出 10 条数据 select top 10 * from tablename order by newid()18、说明:随机选择记录select newid()19、说明:册。除重复记录Delete from tablename wher

29、e id not in (select max(id) fromtablename group by col1,col2,.)20、 说明:列出数据库里所有的表名select name from sysobjects where type=U21、说明:列出表里的所有的 select name from syscolumns where id=object_id(TableName)22、说明:列示type、vender、pcs字段,以type字段排列,case可以方便地实现多重 选择,类似 select 中的 case。 select type,sum(case vender when A

30、then pcs else 0 end),sum(case vender when C then pcs else 0 end),sum(case vender when B then pcs else 0 end) FROM tablename group by typeMysql数据库启动:net start mySql;进入:mysql -u root -p/mysql -h localhost -u root -p databaseName;列出数据库:show databases;选择数据库:use databaseName;列出表格:show tables;显示表格列的属性:sho

31、w columns from tableName;建立数据库:source fileName.txt;匹配字符:可以用通配符_代表任何一个字符,代表任何字符串;增加一个字段:alter table tabelName add column fieldName dateType;增加多个字段:alter table tabelName add column fieldName1 dateType,add columns fieldName2 dateType;多行命令输入:注意不能将单词断开;当插入或更改数据时,不能将字段的字符串展开到多 行里,否则硬回车将被储存到数据中;增加一个管理员帐户:g

32、rant all on *.* to userlocalhost identified by password;每条语句输入完毕后要在末尾填加分号;,或者填加g也可以;查询时间:select now();查询当前用户:select user();查询数据库版本:select version();查询当前使用的数据库:select database。;1、删除student_course数据库中的students数据表:rm -f student_course/students.*2、备份数据库:(将数据库test备份)mysqldump -u root -p testc:test.txt备份表

33、格:(备份test数据库下的mytable表格)mysqldump -u root -p test mytablec:test.txt将备份数据导入到数据库:(导回test数据库)mysql -u root -p test3、创建临时表:(建立临时表zengchao)create temporary table zengchao(name varchar(10);4、创建表是先判断表是否存在create table if not exists students();5、从已经有的表中复制表的结构create table table2 select * from tablel where 11;

34、6、复制表create table table2 select * from tablel;7、对表重新命名alter table tablel rename as table2;8、修改列的类型alter table tablel modify id int unsigned;/修改列 id 的类型为 int unsignedalter table tablel change id sid int unsigned;/修改列 id 的名字为 sid,而且把属性修改为 int unsigned9、创建索引alter table tablel add index ind_id (id);crea

35、te index ind_id on tablel (id);create unique index ind_id on tablel (id);/ 建立唯一性索引10、删除索引drop index idx_id on table1;alter table table1 drop index ind_id;11、联合字符或者多个列(将列id与:和列name和=连接) select concat(id,:,name,=) from students;12、limit(选出10到20条)select * from students order by id limit 9,10;13、MySQL不支

36、持的功能事务,视图,外键和引用完整性,存储过程和触发器14、MySQL会使用索引的操作符号,=,=,between,in,不带或者_开头的 like15、使用索引的缺点1)减慢增删改数据的速度;2)占用磁盘空间;3)增加查询优化器的负担;当查询优化器生成执行计划时,会考虑索引,太多的索引会给查询优化器增加工作量,导 致无法选择最优的查询方案;16、分析索引效率方法:在一般的SQL语句前加上explain;分析结果的含义:1)table:表名;2)type:连接的类型,(ALL/Range/Ref)。其中ref是最理想的;3)possible_keys:查询可以利用的索引名;4)key:实际使用

37、的索引;5)key_len:索引中被使用部分的长度(字节);6)ref:显示列名字或者const”(不明白什么意思);7)rows:显示MySQL认为在找到正确结果之前必须扫描的行数;8)extra: MySQL 的建议;17、使用较短的定长列1)尽可能使用较短的数据类型;2)尽可能使用定长数据类型;a)用char代替varchar,固定长度的数据处理比变长的快些;b)对于频繁修改的表,磁盘容易形成碎片,从而影响数据库的整体性能;c)万一出现数据表崩溃,使用固定长度数据行的表更容易重新构造。使用固定长度的数 据行,每个记录的开始位置都是固定记录长度的倍数,可以很容易被检测到,但是使用可变 长度

38、的数据行就不一定了;d)对于MyISAM类型的数据表,虽然转换成固定长度的数据列可以提高性能,但是占据 的空间也大;18、使用 not null 和 enum尽量将列定义为not null,这样可使数据的出来更快,所需的空间更少,而且在查询时, MySQL不需要检查是否存在特例,即null值,从而优化查询;如果一列只含有有限数目的特定值,如性别,是否有效或者入学年份等,在这种情况下应 该考虑将其转换为enum列的值,MySQL处理的更快,因为所有的enum值在系统内都是 以标识数值来表示的;19、使用 optimize table对于经常修改的表,容易产生碎片,使在查询数据库时必须读取更多的磁

39、盘块,降低查询 性能。具有可变长的表都存在磁盘碎片问题,这个问题对blob数据类型更为突出,因为其 尺寸变化非常大。可以通过使用optimize table来整理碎片,保证数据库性能不下降,优化 那些受碎片影响的数据表。optimize table可以用于MyISAM和BDB类型的数据表。实际 上任何碎片整理方法都是用mysqldump来转存数据表,然后使用转存后的文件并重新建数 据表;20、使用 procedure analyse()可以使用procedure analyse()显示最佳类型的建议,使用很简单,在select语句后面加 上 procedure analyse()就可以了;例如

40、:select * from students procedure analyse();select * from students procedure analyse(16,256);第二条语句要求procedure analyse()不要建议含有多于16个值,或者含有多于256字节的 enum类型,如果没有限制,输出可能会很长;21、使用查询缓存1)查询缓存的工作方式:第一次执行某条select语句时,服务器记住该查询的文本内容和查询结果,存储在缓存 中,下次碰到这个语句时,直接从缓存中返回结果;当更新数据表后,该数据表的任何缓存 查询都变成无效的,并且会被丢弃。2)配置缓存参数:变量:q

41、uery_cache _type,查询缓存的操作模式。有3中模式,0:不缓存;1 :缓存查 询,除非与select sql_no_cache开头;2:根据需要只缓存那些以select sql_cache开头 的查询;query_cache_size:设置查询缓存的最大结果集的大小,比这个值大的不会被缓 存。22、调整硬件1)在机器上装更多的内存;2)增加更快的硬盘以减少I/O等待时间;寻道时间是决定性能的主要因素,逐字地移动磁头是最慢的,一旦磁头定位,从磁道读则 很快;3)在不同的物理硬盘设备上重新分配磁盘活动;如果可能,应将最繁忙的数据库存放在不同的物理设备上,这跟使用同一物理设备的不同 分区是不同的,因为它们将争用相同的物理资源(磁头)。

展开阅读全文
温馨提示:
1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
2: 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
3.本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
关于我们 - 网站声明 - 网站地图 - 资源地图 - 友情链接 - 网站客服 - 联系我们

copyright@ 2023-2025  zhuangpeitu.com 装配图网版权所有   联系电话:18123376007

备案号:ICP2024067431-1 川公网安备51140202000466号


本站为文档C2C交易模式,即用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知装配图网,我们立即给予删除!