资源描述:
《t-sql语句学习笔记》由会员上传分享,免费在线阅读,更多相关内容在工程资料-天天文库。
1、T_SQL语句001.新建数据库:createdatabaseSaleson(name=Sales_dat,--主数据文件逻辑名Sales_datfilename='D:databasesales_dat.mdf',--物理名为sales_dat.mdfsize=10mb,--初始大小10MBmaxsize=50mb,--最大值50MBfilegrowth=5%--自动增长值5)logon(name=Sales_log,--日志文件逻辑名Sales_logfilename='D:databasesales_log.ldf',--物理名s
2、ales_log.ldfsize=5mb,--初值5MBmaxsize=25,--最大值25filegrowth=5%--自动增长值5)Go002.新建表:use学生信息管理系统--设置当前数据库createtablecourse_info--创建课程信息表(c_nochar(10)notnull,--课程号,必填c_namenvarchar(20)notnull,--课程名,必填c_creditintnull--学分,允许为空)003.增加表字段:USE学生信息管理系统altertablecourse_infoadd增加c_periodsma
3、llintnotnull004.修改表字段:USE学生信息管理系统altertablecourse_infoaltercolumn修改表字段c_namenvarchar(40)notnull005.删除表字段:USE学生信息管理系统altertablecourse_infodropcolumn删除表字段c_period006.删除表:USE学生信息管理系统altertablecourse_infodroptable删除表course_info007.创建主键:Altertable表名addconstraint主键约束名primarykey(字段
4、)008.默认约束:Altertable表名addconstraint默认约束名default('默认值')for字段009.唯一约束:Altertable表名addconstraint唯一约束名unique(字段)010.检查约束:altertable表名AddConstraint约束名Check约束表达式011.外键约束:altertable外键表AddConstraint外键约束名foreignkey(字段)references主键表(字段)012.表中插入数据:(1).插入一行Insertintostu_info(s_Name,s_No
5、,s_Sex,stuAge,s_Seat,s_Address)Values(‘张三’,‘2012060108’,‘男’,‘28’,‘15’,‘湘潭’)(2).插入多行USE学生信息管理系统insertintoCourse_info(C_no,C_name,C_credit,C_period)select'0102','蔡敏芳','85','91'unionselect'0103','黄美美','89','99'unionselect'0106','萧红','99','95'013.表中删除数据:Deldtestu_infowheres_No=’
6、2012060108’014.表中修改数据:Updatestu_infosets_Sex=’男’Wheres_Name=’熊智’015.查询全部列USE学生信息管理系统select*fromCourse_info016.查询部分列selectC_no,C_namefromCourse_info017.查询部分列(按降序)selectC_periodfromCourse_infowhere条件判断(C_period>=90)orderbyC_perioddesc按降序排列018.查询部分列(按升序)selectC_periodfromCourse
7、_infowhere条件判断(C_period>=90)orderbyC_periodasc按升序排列019.查询地址(去掉相同的地址)USE学生信息管理系统selectdistinct去掉相同的地址C_AddressfromCourse_info020查询前三行(搜索表中的前三行)USE学生信息管理系统selecttop3表示前三行*fromCourse_info021.查询前20%的行记录USE学生信息管理系统selecttop20percent表示前20%的行记录*fromCourse_info022.查询【同学表】中18岁到20岁的姓李
8、的女生。提示:有3个限制条件:①年龄>=18并且年龄<=20②姓名like'李%'③性别='女';同时年龄需要由出生日期计算得到。Year()函数是用