1、作者:毛毛 (Char) 字符串 1~~2000个字节 (varchar) 字符串 1~~4000个字节 (Long) 2GB Number 可以储整数,负数,零,定点数,及精度为38的浮点数 如:colum_name unbler(p,s) {浮点} //(P)指精度,指总的数字数,1~~38 //(S)直小数位,即小数点右边的数字数位,-84~~127不等。 (Date) 用于表中储藏日期和时间。格式“dd-mon-yy” SQL分类: DDL—数据定义语言(CREATE,ALTER,DROP,
2、DECLARE) DML—数据操纵语言(SELECT,DELETE,UPDATE,INSERT) DCL—数据控制语言(GRANT,REVOKE,COMMIT,ROLLBACK) 定义命令: Create table命令—创建表 Alter table命令— Truncate table命令—删除表所有的记录,表结构不删除。 Drop table命令—删除表 操纵语言: Insert into –添加 Select --查询 Update –更新 Delete –删除 informix数据类型 字符型 char(20),varchar(minsize,m
3、axsize) 数值型 (六种,decimal(16,2),smallint,integer,smallfloat,float,serial) 日期型 (date,默认格式为:MM/DD/YYYY) ****要修改date类型的缺省格式,只要在用户主目录的.profile文件中作如下说明:DBDATE=Y4MD/ EXPORT DBDATE **** 货币型 (money(8,2)) 其他(如:interval,datetime等) 1.创建表 CREATE TABLE <表名>(<列名><数据类型> [列级完整性约束条件] [, <列名> <数据类型> [列级完整性约束条件]
4、]) [, <表级完整性约束条件>]; 注1:列级完整性约束通常包括:是否为空、缺省值。 注2:表级完整性约束通常包括:主键、外键、唯一性、检查。 例1:建立课程表(Course) CREATE TABLE Course ( Cno CHAR(1) NOT NULL DEFAULT '', Cname VARCHAR(20) DEFAULT '', Cpno CHAR(1) DEFAULT '', Ccredit INT DEFAULT 0, Constraint CoursePK Primary Key (Cno) ) 例2:建立学生表(Stud
5、ent) 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 AND Sage < 150) ) 建立学生选课表(SC) CREATE TABLE SC
6、 ( Sno CHAR(5) NOT NULL DEFAULT '', Cno CHAR(1) NOT NULL DEFAULT '', Grade INT DEFAULT 0, Constraint SCPK Primary Key (Sno,Cno), Constraint StudentFK Foreign Key (Sno) References Student(Sno), Constraint CourseFK Foreign Key (Cno) References Course(Cno), Constraint GradeCK Check (Grade
7、 >= 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 注:索引是不可更改的,想更改必须删除重
8、新建。 4、说明:创建视图:create 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 t
9、able1 where field1 like ’%value1%’ ---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 avgvalue from table1 最大:select max(field1) as maxvalue from table1
10、 最小:select min(field1) as minvalue from table1 2. 插入记录 一次插入一条记录 insert into tablename (字段名,字段名。。。) values (值,值。。。。); ***值 如果是字符型的需要加单引号‘’; 例1: insert into manager(id,name,sex,phonenum) values (4,'dfdf',1,23535); 一次插入多条记录 insert into tablename(字段名,字段名。。。) select 字段名,字段名。。 from a
11、nothertablename; 3. 删除记录 delete from tablename where 字段名 = ‘值’ 例: delete from Student where name = ‘pig’ 4. 查找记录 1.查询所有记录 select * from tablename 2.条件查询 select 字段名1,字段名2,。。。from tablename where 条件语句 例: select user_id,user_name from user_info where user_id = 1 3.使用like进行模糊查询 [
12、not]like’<匹配串>’[escape<‘换码字符’>] select id,name,sex,phonenum from manager where name like 'd%_' 4. 对查询结果排序 select id,name,sex,phonenum from manager where sex = 2 order by id desc 5. 使用between在某个范围内进行查询 select id,name,sex,phonenum from manager where id between 5 and 9; 6. 使用in在列举值里进
13、行查询 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 grade is null; 注意这里is不能用等号=代替 9.多重条件查询 逻辑运算符 and和or可用来联结多个查询条件 例: select sname
14、from Student where Sdept = ‘CS’and Sage < 20; 10. 使用集函数 count(*) 统计元组个数 count(<列名>) 统计一列中值得个数 sum (<列名>) avg (<列名>) max (<列名>) min (<列名>) 例:select cno,count(sno) from group by cno; 11. 对查询结果分组 group by 例:求各个课程及相应的选课人数 select cno,count(sno) from sc group b
15、y cno; 最终只输出满足指定条件的组,则可以使用having短语指定筛选条件 例:查询选修了3门以上课程的学生学号 select sno from sc group by sno having count(*)>3; 12.联结查询 1) 自连接查询 例:select o,second.cpno from course as first,course as second where first.cpno = o; 2) 外连接 例:select student.sno,sname,ssex,sage,sdept,cno,grade fr
16、om student,sc where student.sno = sc.sno(*); 外连接的表示方法为,在连接谓词的某一边加符号*。 如果外连接符出现在连接条件的右边,称其为右外连接,如果外连接符出现在 连接条件的左边,则称为左外连接。 3) 复合条件查询 and 4) 嵌套查询 例:select sname from student where sno in select sno from sc where cno = ‘2’; 5) 集合查询 主要包括并操作union,交操作intersect,和差操作minus 例
17、 select * from student where sdept = ‘cs’ union select * from student where sage <= 19; 5. 更新记录 update tablename set 字段名 = 值,字段名 = 值,。。。。 where 条件语句 例:update persondata set age = age+1 where name = ‘pig’; 6. 修改表结构 ALTER TABLE <表名> [ADD(<新列名><数据类型> [列级完整性约束条件] [MODIFY <
18、列名> <数据类型>] [DROP <完整性约束名>]; 例: 1 alter table sudent add scome date; 2 alter table student modify sage smallint 3 alter table student drop unique(sname) select语句的一般格式 select [all|distinct] <目标列表达式>[别名][,<目标列表达式>[别名]]… from <表名或视图>[别名][,<表名或视图>[别名]]… [where<条件表达式>] [group by <列名1>[having]<条件表达式>]] [order by <列名2>[asc|desc]] 1) 目标列表达式有以下可选格式: (1) * (2) <表名>.* (3) count([distinct|all]*) (4) [<表名>.]<属性列名表达式>[,<表名>.]<属性列名表达式>… 6






