sql语句示例大全.doc

上传人:cn****1 文档编号:550374997 上传时间:2022-10-10 格式:DOC 页数:6 大小:47.01KB
返回 下载 相关 举报
sql语句示例大全.doc_第1页
第1页 / 共6页
sql语句示例大全.doc_第2页
第2页 / 共6页
sql语句示例大全.doc_第3页
第3页 / 共6页
sql语句示例大全.doc_第4页
第4页 / 共6页
sql语句示例大全.doc_第5页
第5页 / 共6页
点击查看更多>>
资源描述

《sql语句示例大全.doc》由会员分享,可在线阅读,更多相关《sql语句示例大全.doc(6页珍藏版)》请在金锄头文库上搜索。

1、作者:毛毛(Char) 字符串 12000个字节(varchar) 字符串 14000个字节(Long) 2GBNumber 可以储整数,负数,零,定点数,及精度为38的浮点数 如:colum_name unbler(p,s) 浮点 /(P)指精度,指总的数字数,138 /(S)直小数位,即小数点右边的数字数位,-84127不等。(Date) 用于表中储藏日期和时间。格式“dd-mon-yy”SQL分类: DDL数据定义语言(CREATE,ALTER,DROP,DECLARE) DML数据操纵语言(SELECT,DELETE,UPDATE,INSERT) DCL数据控制语言(GRANT,REV

2、OKE,COMMIT,ROLLBACK)定义命令:Create table命令创建表Alter table命令Truncate table命令删除表所有的记录,表结构不删除。Drop table命令删除表操纵语言:Insert into 添加Select -查询Update 更新Delete 删除informix数据类型字符型 char(20),varchar(minsize,maxsize)数值型 (六种,decimal(16,2),smallint,integer,smallfloat,float,serial)日期型 (date,默认格式为:MM/DD/YYYY)*要修改date类型的缺

3、省格式,只要在用户主目录的.profile文件中作如下说明:DBDATE=Y4MD/ EXPORT DBDATE *货币型 (money(8,2)其他(如:interval,datetime等)1.创建表CREATE TABLE ( 列级完整性约束条件, 列级完整性约束条件.) , ;注1:列级完整性约束通常包括:是否为空、缺省值。注2:表级完整性约束通常包括:主键、外键、唯一性、检查。例1:建立课程表(Course) CREATE TABLE Course (Cno CHAR(1) NOT NULL DEFAULT ,Cname VARCHAR(20) DEFAULT ,Cpno CHAR(

4、1) DEFAULT ,Ccredit INT DEFAULT 0,Constraint CoursePK Primary Key (Cno)例2:建立学生表(Student) CREATE TABLE Student (Sno CHAR(5) NOT NULL DEFAULT ,Sname CHAR(6) DEFAULT ,Ssex CHAR(2) DEFAULT , Sage INT DEFAULT 0, Sdept CHAR(2) DEFAULT ,Constraint StudentPK Primary Key (Sno),Constraint SageCK Check (Sage 0

5、 AND Sage = 0 AND Grade = 100) 1说明:增加一个列 Alter table tabname add column col type 2、说明:添加主键: Alter table tabname add primary key(col) 说明:删除主键: Alter table tabname drop primary key(col) 3、说明:创建索引:create unique index idxname on tabname(col.) 删除索引:drop index idxname 注:索引是不可更改的,想更改必须删除重新建。 4、说明:创建视图:crea

6、te view viewname as select statement 删除视图:drop view viewname 5、说明:几个简单的基本的sql语句 选择:select * from table1 where 范围 插入:insert into table1(field1,field2) values(value1,value2) 删除:delete from table1 where 范围 更新:update table1 set field1=value1 where 范围 查找:select * from table1 where field1 like %value1% -l

7、ike的语法很精妙,查资料! 排序: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 avgvalue from table1 最大:select max(field1) as maxvalue from table1 最小:select min(field1) as minvalue from table1 2 插入记

8、录一次插入一条记录insert into tablename (字段名,字段名。) values (值,值。);*值 如果是字符型的需要加单引号;例1:insert into manager(id,name,sex,phonenum) values (4,dfdf,1,23535); 一次插入多条记录insert into tablename(字段名,字段名。) select 字段名,字段名。 from anothertablename;3 删除记录delete from tablename where 字段名 = 值例: delete from Student where name = pi

9、g4 查找记录1查询所有记录select * from tablename2条件查询select 字段名1,字段名2,。from tablename where 条件语句例:select user_id,user_name from user_info where user_id = 13使用like进行模糊查询notlikeescapeselect id,name,sex,phonenum from manager where name like d%_4. 对查询结果排序select id,name,sex,phonenum from manager where sex = 2 order

10、 by id desc5. 使用between在某个范围内进行查询select id,name,sex,phonenum from manager where id between 5 and 9; 6. 使用in在列举值里进行查询select id,name,sex,phonenum from manager where sex in (2,1) 7. 分组查询select sex, sum(phonenum) as fgh from manager where sex in (2,1) group by sex 8.设计空值的查询例:select sno,cno from sc where

11、 grade is null;注意这里is不能用等号=代替9.多重条件查询逻辑运算符 and和or可用来联结多个查询条件例:select sname from Student where Sdept = CSand Sage 20;10. 使用集函数count(*) 统计元组个数count() 统计一列中值得个数sum ()avg ()max ()min ()例:select cno,count(sno)fromgroup by cno;11. 对查询结果分组group by例:求各个课程及相应的选课人数select cno,count(sno)from scgroup by cno;最终只输

12、出满足指定条件的组,则可以使用having短语指定筛选条件例:查询选修了3门以上课程的学生学号select snofrom scgroup by snohaving count(*)3;12.联结查询1) 自连接查询例:select o,second.cpnofrom course as first,course as secondwhere first.cpno = o; 2) 外连接例:select student.sno,sname,ssex,sage,sdept,cno,gradefrom student,scwhere student.sno = sc.sno(*);外连接的表示方法

13、为,在连接谓词的某一边加符号*。如果外连接符出现在连接条件的右边,称其为右外连接,如果外连接符出现在 连接条件的左边,则称为左外连接。3) 复合条件查询and4) 嵌套查询例:select sname from studentwhere sno inselect snofrom scwhere cno = 2;5) 集合查询主要包括并操作union,交操作intersect,和差操作minus例:select *from studentwhere sdept = csunionselect *from studentwhere sage = 19;5. 更新记录update tablename set 字段名 = 值,字段名 = 值,。 where 条件语句例:update

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 生活休闲 > 社会民生

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号