网站导航网学 原创论文 原创专题 网站设计 最新系统 原创论文 论文降重 发表论文 论文发表 UI设计定制 论文答辩PPT格式排版 期刊发表 论文专题
返回网学首页
网学原创论文
最新论文 推荐专题 热门论文 论文专题
当前位置: 网学 > 交易代码 > SQL语法 > 正文

SQL语句--语法hinp

论文降重修改服务、格式排版等 获取论文 论文降重及排版 论文发表 相关服务

【网学网提醒】:本文主要为网学会员提供SQL语句--语法hinp,希望对需要SQL语句--语法hinp网友有所帮助,学习一下!


    简单的SQL语法
    -(一)简单的sql查询
    --(1)不限制列查询--显示员工表中所有列的信息select*fromemployee--(2)限制列查询--查询所有员工的员工号与员工姓。--用SELECT子句来指定查询所需的列,多个列之间用逗号分开。selectemp_id,emp_lnamefromemployee--(3)查询全体员工的员工号、。--(要求用一个字段显示出来)selectemp_id,emp_lname+emp_fnamefromemployee--(4)在查询结果中添加列--为上题的查询结果添加一个别名””--查询结果中产生的列,别名用AS命名。selectemp_id,emp_lname+emp_fnameas‘’
    fromemployee
    --(5)查询结果添加说明文字--查询所有员工的,电话号码。(电话号码列在结果中显示为:“XXX的电话号码是:”)Selectemp_lname+emp_fname+’的电话号码是:’+phonefromemployee--(6)对查询结果进行排序--查询所有员工名单,并将工资降序排序。--说明:OrderBy有一个附加参数:ASC|DESC表明是升序排序还是降序排序。缺省ASC。Selectemp_id,emp_lname+emp_fname‘’,salaryfromemployeeOrderbysalarydesc--(7)去冗余查询--查询员工来自的城市(不出现重复的城市名称)。--说明:使用DISTINCT消除重复数据。Selectdistinctcityfromemployee
    第1页
    2011-11-30
     --(8)选取前几行数据--查询员工表中前三名员工的与工号。--说明:在SELECT语句中使用TOPn或TOPnPERCENT,--来选取查询结果的前n行或前百分之n的数据。Selecttop3emp_id,emp_lname+emp_fname‘’fromemployee
    建立表
    createtabledepartment(dept_idvarchar(8)notnull,dept_namevarchar(20),dept_head_idvarchar(8)notnull)createtableemployee(emp_idvarchar(8)notnullprimarykey,emp_fnamevarchar(20),emp_lnamevarchar(20),sexchar(2),dept_idvarchar(8),cityvarchar(100),zip_codeint,phoneint,ss_numberint,salaryintnotnull,birth_datedatetimenotnull)altertabledepartmentaddconstraintpk_department_dept_head_idprimarykey(dept_head_id)altertabledepartmentaddconstraintfkforeignkey(dept_head_id)referencesemployee(emp_id)
    第2页
    2011-11-30
     建立数据库
    createdatabaseweb--创建数据库名为webon(name=stu,--主数据文件名stufilename="d:\haima\stu.mdf",--主数据文件名的存放路径size=1mb,--文件大小maxsize=2mb,--文件最大filegrowth=1mb--文件增长),(name=stu1,--辅助数据文件名stu1filename="d:\haima\stu1.ndf",--文件存放的地址size=1mb,maxsize=2mb,filegrowth=1mb)logon(--日志文件stu2name=stu2,filename="d:\haima\stu2.ldf",size=1mb,maxsize=2mb,filegrowth=25%)--删除数据库webdropdatabaseweb--添加数据文件组名字为softalterdatabasewebaddfilegroupsoft--给文件组soft添加文件stu3alterdatabasewebaddfile(name=stu3,
    filename="d:\haima\stu3.ndf")tofilegroupsoft
    第3页
    2011-11-30
     --数据库属性sp_helpdbweb--重命名数据库名称web改为web1sp_renamedbweb,web1sp_renamedbweb1,web
    --1、用SQL语句创建学生表,表中字段及其数据类型如下所示:(20分)--(1).学生(varchar(10),varchar(8),性别char(2),政治面貌varchar(20)),其中字段设置为主键字段。createtablestudent(stu_idvarchar(10)notnullprimarykey,stu_namevarchar(10),sexchar(2),visagevarchar(20))--查看表约束sp_helpconstraintstudent--删除表droptablestudent--第二种加主键的方法createtablestudent(stu_idvarchar(10)notnull,stu_namevarchar(10),sexchar(2),visagevarchar(20)constraintpk_stu_idprimarykey(stu_id))--第三种加主键方法createtablestudent(stu_idvarchar(10)notnull,stu_namevarchar(10),sexchar(2),visagevarchar(20))
    第4页2011-11-30
     altertablestudentaddconstraintpk_stuidprimarykey(stu_id)--(2).用SQL语句将学生表的政治面貌字段数据类型改为varchar(26)。altertablestudentaltercolumnvisagevarchar(26)sp_helpstudent--(3).用SQL语句为学生表增加一个新的字段:班级char(20)。altertablestudentaddclasschar(20)--(4).用SQL语句删除学生表的班级字段。altertablestudentdropcolumnclass--(5).用SQL语句为学生表添加如下记录:(’0001',’张三’,’男’,’党员’)。insertintostudentvalues('001','张三','男','党员')select*fromstudent--(6).用SQL语句将学生表中所有政治面貌为‘党员’的记录改为‘共产党员’。updatestudentsetvisage='共产党员'wherevisage='党员'
    --(7).用SQL语句删除学生表中的为‘0003’的记录。insertintostudentvalues('003','李四','男','党员')select*fromstudentdeletefromstudentwherestu_id='003'
    --(8).用SQL语句查出所有男同学的、、政治面貌。insertintostudentvalues('004','李四','男','党员')insertintostudentvalues('005','李四1','女','党员')insertintostudentvalues('006','李四2','男','党员')select*fromstudentwheresex='男'
    --(9).用SQL语句查出政治面貌为‘共产党员’或者‘团员’的学生信息
    第5页
    2011-11-30
     insertintostudentvalues('007','李四1','女','共产党员')insertintostudentvalues('008','李四2','男','共产党员')insertintostudentvalues('009','李四2','男','团员')select*fromstudentwherevisage='共产党员'orvisage='团员'
    --(10).用SQL语句查出每种政治面貌的学生人数。selectcount(*)as人数,visagefromstudentgroupbyvisageselect*fromstudent
    --外键--先建一张学生表droptableStu_grade--第一种createtableStu_grade(idintnotnullprimarykey,stu_idvarchar(10)foreignkey(stu_id)referencesstudent
    (stu_id),gradevarchar(20),)sp_helpStu_grade--第二种createtableStu_grade(idintnotnullprimarykey,stu_idvarchar(10),gradevarchar(20),constraintfk_stuidforeignkey(stu_id)referencesstudent(stu_id))--第三种createtableStu_grade(idintnotnullprimarykey,stu_idvarchar(10),gradevarchar(20))altertableStu_gradeaddconstraintfk_stuidforeignkey(stu_id)referencesstudent(stu_id)
    第6页
    2011-11-30
     ---惟一性约束--(1)第二种createtablestudent(stu_idvarchar(10)notnull,stu_namevarchar(10)unique,sexchar(2),ageint,visagevarchar(20))--(2)第二种createtablestudent(stu_idvarchar(10)notnull,stu_namevarchar(10),sexchar(2),ageint,visagevarchar(20)constraintun_nameunique(stu_name))--(3)第三种altertablestudentaddconstraintun_nameunique(stu_name)-检查约束--(1)createtablestudent(stu_idvarchar(10)notnull,stu_namevarchar(10),sexchar(2),ageint,visagevarchar(20)constraintch_sexcheck(sexin('男','女')))--(2)altertablestudentaddconstraintch_sexcheck(age>=18andage<=40)
    --缺省约束--(1)createtablestudent(
    第7页2011-11-30
     stu_idvarchar(10)notnull,stu_namevarchar(10),sexchar(2)default'男',ageint,visagevarchar(20))(2)createtablestudent(stu_idvarchar(10)notnull,stu_namevarchar(10),sexchar(2)default'男',ageint,visagevarchar(20)constraintde_sexdefault'男')--(3)altertablestudentaddconstraintde_sexdefault'男'
    条件限制查询
    --(1)比较条件查询Selectemp_id,emp_namefromemployeeWheresalary>50000Andsex=‘男’--(2)模糊查询Selectemp_id,emp_namefromemployeeWhereemp_namelike‘%P%’--(3)范围条件查询--查询出工资介于20000元和30000元之间的员工。Selectemp_id,emp_namefromemployeeWheresalarybetween20000and30000--(4)使用统计函数查询
    第8页2011-11-30
     --查询员工表中共有多少名员工。Selectcount(*)fromemployeeSelectcount(emp_id)fromemployee--(5)分组查询--查询各个部门的总人数各是多少Selectcount(*)fromemployeeGroupbydept_id
    --(6)带条件分组查询--显示部门人数多于20人的部门编号与人数。Selectdept_id,count(*)fromemployeeGroupbydept_idHavingcount(*)>20
    增删改查
    insertintousersvalues(10000,'haha');--一个表向另一个表复制行--insert合select组合使用,介绍oracle的merge来复制行insertintousers(selectuserid,namefromuserswhereuserid=10004)添加insertintoemployeevalues('1','张三','男','1','西安','1336666','0295444545','54545','101','2006-5-6')insertintoemployeevalues('2','李四','男','1','西安','1336666','0295444545','54545','101','2006-5-6')insertintodepartmentvalues('1','网络部','1')--修改updateuserssetname='heihei'whereuserid=10003;
    第9页2011-11-30
     --删除deletefromuserswhereuserid=10001;
    连接查询
    --连接查询
    --(1)等值连接查询--查询所有员工所在的部门名称。Selectemp_id,e.dept_id,dept_namefromemployeee,departmentdWheree.dept_id=d.dept_id--(3)非等值连接查询--在非等值查询的连接条件中不使用等号,而使用其它比较运算符。--比较运算符有:>、>=、<、<=、!=,<>--查询所有非领导的员工的所有信息Selectemp_id,e.dept_id,dept_namefromemployeee,departmentdWheree.dept_id=d.dept_idande.emp_id<>d.dept_head_id
    --
    (二)高级查询--在一个SELECT语句的WHERE子句或HAVING子句中嵌套另一个SELECT语句的查询称为嵌套查询,又称子查询。--(1)查询出工资最高的员工的个人信息。select*fromemployeewheresalary=(selectmax(salary)fromemployee)--(2)查询各个部门中工资最高的人的个人信息。select*fromemployeewheresalaryin(selectmax(salary)fromemployeeGroupbydept_id)--(3)查询出工资超过90000元的员工的部门名称,查询结果按部门编号排序。
    第10页2011-11-30
     --(使用谓词IN连接子查询)selectdept_id,dept_namefromdepartmentwheredept_idin(selectdept_idfromemployeewheresalary>90000)orderbydept_iddesc-××××××××××虚拟表查询×××××--(5)各个部门中按性别分组的平均工资各是多少。--1、先将各部门女员工的平均工资计算出来。--2、再将各部门男员工的平均工资计算出来。--3、以上面的1、2为两张虚拟表,进行连接查询。selectf.dept_id,fsalary,msalaryfrom(selectdept_id,avg(salary)fsalaryfromemployeegroupbydept_id,sexhavingsex='f')f,------各部门女员工的平均工资。(selectdept_id,avg(salary)msalaryfromemployeegroupbydept_id,sexhavingsex='m')m-----各部门男员工的平均工资wheref.dept_id=m.dept_id
    存储过程
    -(1)无参数的存储过程--创建一个存储过程,存储各部门的总工资与平均工资及部门编号。createprocdeptsalaryasselectavg(salary)avgsalary,sum(salary)sumsalary,dept_idfromemployeegroupbydept_id--调用execdeptsalary--(2)带有输入参数的存储过程--创建一个存储过程,以员工为参数,输入后显示某人信息(模糊查询).
    第11页2011-11-30
     createprocempname@namevarchar(20)asselect*fromemployeewhereemp_lname+emp_fnamelike'%'+@name+'%'--调用execempname'A'
    --(3)带有输出参数的存储过程--创建一个存储过程,能够输出工资最高员工的工资。createprocempsalary@empsaldecimal(20,3)outputasselect@empsal=max(salary)fromemployee--调用带输出参数的存储过程:declare@empsaldecimal(20,3)execempsalary@empsaloutputprint@empsal--(4)查看存储过程--sp_helptext存储过程名sp_helptextdeptsalarysp_helptextsp_helptextempnameempsalary
    --(5)sp_rename旧存储过程名,新存储过程名--(6)dropproc存储过程名
    第12页
    2011-11-30
    
    
  • 上一篇资讯: SQL语句
  • 下一篇资讯: SQL触发器语法
  • 设为首页 | 加入收藏 | 网学首页 | 原创论文 | 计算机原创
    版权所有 网学网 [Myeducs.cn] 您电脑的分辨率是 像素
    Copyright 2008-2020 myeducs.Cn www.myeducs.Cn All Rights Reserved 湘ICP备09003080号 常年法律顾问:王律师