关于SQL语句的一般使用实例
一、sql数据定义
(一)新建表
create table 表名(字段名及其定义, 字段名及其定义,……)
1.1.1 创建自由表xs1(学号 c(8),姓名 c(6),性别 c(2),出生年月 d,个人简历 m)
    create table xs1(学号 c(8),姓名 c(6),性别 c(2),出生年月 d,高考成绩 n(6,2),个人简历 m)
1.1.2:创建计算机网络表xs2(要先打开一计算机网络)
modify data xsgl
create table xs2(学号 c(8) primary key, 姓名 c(8) not null, 性别 c(2) default "" check 性别$"男女" error "性别一栏必须填写男或者女",出生年月 d check 出生年月<={^1990-01-10} and 出生年月>={^1970-01-01}, 入校总分 n(3) ,三好生 l,特长 m,照片 g)
()修改表
alter table 表名 add字段名及其定义
alter table 表名 drop 字段名
alter table 表名 rename 原字段名to 新字段名
alter table 表名 alter 字段名及其新定义
1.2.1 新增电话字段并建立候选索引
alter table xs2 add 电话 c(8) unique
1.2.2 修改入校总分字段相关定义
alter table xs2 alter 入校总分 n(3) check between(入校总分, 535,750) error "入校总分必须在535~~750之间"
alter table xs2 alter 入校总分 drop check
alter table student1 alter 入校总分 set check between(入校总分, 535,750) error "入校总分必须在535~~750之间"
1.2.3 修改性别字段的相关定义
alter table xs2 alter 性别 drop default
alter table xs2 alter 性别 c(2) check 性别$"男女" error "性别一栏必须填写男或者女"
alter tabl xs2 alter 性别 set default ""
1.2.4 关于索引的增删改:
alter table xs2 drop primary key  &&删除主索引
alter table xs2 add  primary key 学号  &&增加主索引
alter table xs2 drop unique tag 电话  &&删除候选索引
alter table xs2 add unique  电话  &&增加候选索引
1.2.5 修改字段名
alter table xs2 rename 姓名 to student姓名
1.2.6 删除三好生字段:
  alter table xs2 drop 三好生
()删除表
    drop table 表名
1.3.1 删除xs2表:
drop table xs2
二、sql的数据更新
()追加新记录
      insert into 表名(字段1,字段2,……) values (1,2,……)
insert into xs2(学号,姓名,性别,出生年月,入校总分,三好生) values ("s0201112","徐畅","",{^1984-06-25},588,.t.)
(二)删除记录
      delete from 表名 where 删除条件
  例:delete from student where 性别=""
delete from student where 姓名="章立"
delete from studing
delete from teaching where 教师编号=(select 教师编号 from teacher where 姓名="黄敏")
(三)修改表记录
update 表名 set 字段1=表达式1, 字段2=表达式2,…… where 条件
例:update xscj set 平均分=(高数+英语+体育+计算机网络)/4
update teacher set 职称="副教授" where 姓名="黄敏"
update teacher set 工资=1.2*工资 where 工资<=2000
update studing set 成绩=0 where 学号 in (select student.学号 from student,studing where student.学号=studing.学号 and 姓名="毛强" )
三、sql的数据查询
  select 要查询的内容
    from 数据来源
    where 查询条件
    group by 分组依据 having 分组条件
    order by 排序依据1 asc/desc, 排序依据2 asc/desc,……
    [to file 文件名/to arrary 数组名]/[into table 表名/into curaor 表名]
() 单表查询
3.1.1 查询student表中部分字段
select 学号,姓名,入校总分,性别,出生年月 from student
3.1.2 查询student表中入校总分最高的前5名同学  &&关于top n
增加字段的sql语句
select top 5 学号 as 学生编号,姓名 as 学生姓名,性别 as 学生性别,入校总分 as 高考分数 from student order by 入校总分 desc
下面是vf语句实现方法:
use student
index on 入校总分 tag入校总分desc
go top
brow next 5 fiel 学号:h="学生编号",姓名:h="学生姓名",性别:h="学生性别",入校总分:h="
考分数"
3.1.3 studing表中查询有哪些学生选课了:
select distinct 学号 from studing  &&关于distinct
select distinct 学号 from studing &&注意少了distinct有何变化
3.1.4 查询teacher表所有信息:
select * from teacher  &&关于*
3.1.5 条件查询:
select 学号,成绩 from studing where 课程编号="96120708"
select 学号,课程编号,成绩 from studing where 成绩>=85
select 学号,课程编号,成绩 from studing where (课程编号="96120701" or 课程编号="97120706") and 成绩>=80
select 教师编号,姓名,职称 from teacher where 工资 between 1500 and 2000
select 教师编号,姓名,职称 from teacher where between(工资,1500,2000)
select 教师编号,姓名,职称 from teacher where工资>=1500 and 工资<=2000
select 教师编号,姓名,职称 from teacher where not between(工资,1500,2000)
select 学号,课程编号,成绩 from studing where 课程编号 in ("96120701", "96120706") and 成绩>=80
select 学号,课程编号,成绩 from studing where 课程编号 not in ("96120701", "96120706") and 成绩 between 85 and 90
select 学号,姓名 from student where 姓名 like "%"
select 学号,姓名 from student where 姓名 like "_%"
3.1.6 关于查询函数:
select 学号,sum(成绩) as 总分,avg(成绩) as 平均分 from studing where 学号="200707010103"
select 课程编号,max(成绩) as 最高分,min(成绩) as 最低分, max(成绩) -min(成绩)  as 相差分数 from studing where 课程编号="96120705"
select count(学号) as 入校总分在540分以上的人数  from student where 入校总分>=540
select count(distinct 课程编号) as 选课表中course  from studing
select count(*) as 教授和副教授的人数  from teacher where 职称 in("教授","副教授")
3.1.7 关于分组查询:
select 课程编号,max(成绩) as 最高分,min(成绩) as 最低分, max(成绩) -min(成绩)  as 相差分数, count(*) as 选修人数 from studing group by 课程编号
select 教师编号,count(*) as 任课门数  from teaching group by 教师编号
select 学号 as 选修两门以上course学生的学号,count(*) as 选修门数  from studing group by 学号 having count(*)>=2
select 课程编号,avg(成绩)  as 平均分  from studing where 课程编号 in ("96120702","96120704","96120706","96120708") group by 课程编号 having avg(成绩)>=80

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。