[计算机软件及应用]SQL常用语句大全.doc

上传人:音乐台 文档编号:1991703 上传时间:2019-01-29 格式:DOC 页数:50 大小:243KB
返回 下载 相关 举报
[计算机软件及应用]SQL常用语句大全.doc_第1页
第1页 / 共50页
[计算机软件及应用]SQL常用语句大全.doc_第2页
第2页 / 共50页
[计算机软件及应用]SQL常用语句大全.doc_第3页
第3页 / 共50页
亲,该文档总共50页,到这儿已超出免费预览范围,如果喜欢就下载吧!
资源描述

《[计算机软件及应用]SQL常用语句大全.doc》由会员分享,可在线阅读,更多相关《[计算机软件及应用]SQL常用语句大全.doc(50页珍藏版)》请在三一文库上搜索。

1、-安装环境 SQL Server Management Studio 推荐使用SQL2008 -(SQL)从入门最基础开始 -搭配环境 Microsoft Visual Studio 2008 可以做出各类牛X软件-创建一个简单的数据库:属性用默认值create database stu-创建一个包含一个主要数据文件,一个次要数据文件,一个日志文件create database stu1on primary-默认主要数据文件要放在主要文件组(name=stu_data,filename=d:stu11.mdf,-主要数据文件size=5,maxsize=100,filegrowth=1),-主

2、要数据文件和次要数据文件要有逗号(name=stu_data1,filename=d:stu12.ndf,size=3,maxsize=unlimited,filegrowth=10%)-日志文件与数据文件之间没有逗号log on( name=stu_data2, filename=d:stu13.ldf, size=5, maxsize=unlimited, filegrowth=2)-create database 员工管理数据库on primary( name=员工, filename=d:员工.mdf, size=5),( name=员工, filename=d:员工.ndf, siz

3、e=5, maxsize=unlimited, filegrowth=1)log on( name=员工, filename=d:员工.ldf)-查看数据库的信息exec sp_helpdb-查看所有数据库的信息exec sp_databases -查看指定数据库exec sp_helpdb stu1-查看数据文件的信息(在当前数据库下)use stu1exec sp_helpfile exec sp_helpfile stu_data-查看文件组信息exec sp_helpfilegroup-修改数据库create database xiugaion primary ( name=xiuga

4、i1, filename=d:xiugai1.mdf, size=5, maxsize=100, filegrowth=1)log on( name=xiugai2, filename=d:xiugai2.ldf)use xiugai exec sp_helpfile -增加一个数据文件alter database xiugaiadd file( name=xiugai3, filename=d:xiugai3.ndf)-增加一个日志文件alter database xiugaiadd log file( name=xiugai4, filename=d:xiugai4.ldf)-增加一个文件

5、组alter database xiugai add filegroup abcexec sp_helpfilegroup-增加一个数据文件放入指定的文件组alter database xiugai add file( name=xiugai5, filename=d:xiugai5.ndf)to filegroup abcexec sp_helpfilegroup -移除文件组alter database xiugairemove file xiugai5alter database xiugai remove filegroup abc-修改文件的属性值alter database xiu

6、gai modify file( name=xiugai3, maxsize=100)exec sp_helpfile -修改数据库的名称exec sp_renamedb xiugai,xiugai1-删除数据库drop database xiugai1-数据类型create database 数据类型use 数据类型-整型create table 整型( big bigint, int_n int, small smallint, tiny tinyint)SELECT * FROM 整型insert into 整型 values(100,1500,200,20)insert into 整型

7、 values(100,1500,200,255)insert into 整型 values(100,1500,200,256)-发生数据类型tinyint 的算术溢出错误insert into 整型 values(100,1500,200000,255)-浮点型create table 浮点( fl float, re real, de decimal(5,2), nu numeric(6,2)select * from 浮点insert into 浮点 values(2.3,15.2,123.12,1234.12)insert into 浮点 values(2.3,15.2,123.126

8、9,1234.1242)-四舍五入insert into 浮点 values(2.3,15.2,1231.12,1234.12)-将numeric 转换为数据类型numeric 时出现算术溢出错误。-字符型create table 字符( char_n char(6), varchar_n varchar(6), text_n text, nch nchar(6), nvar nvarchar(6), nt ntext)select * from 字符insert into 字符 values(a,r,w,f,r,y)insert into 字符 values(中国人,中国人,w,中国人民万岁

9、,r,y)insert into 字符 values(中国人民,中国人,w,中国人民万岁,r,y)-将截断字符串或二进制数据。-1、char varchar区别(固定空间可变空间)-2、charnchar区别:输入汉字时,nchar为双字节-日期与时间型:datetime(1753-9999),smalldatetime(1900-2079):要单引号create table 日期( datetime_n datetime, small smalldatetime)select * from 日期insert into 日期 values (2008-08-08,2010-12-4)inser

10、t into 日期 values (2008-08-08 20:08:00,2010-12-4)-货币类型:money,smallmoneycreate table 货币( mo money, small smallmoney)select * from 货币insert into 货币 values(200.5,300)insert into 货币 values($200,$300)insert into 货币 values(¥,¥)-二进制型:binary,varbinary,image(图像)-特殊:sql_variant(万能):除了二进制类型和text类型create table 特

11、殊( mn sql_variant)select * from 特殊insert into 特殊 values(200)insert into 特殊 values(200.56)insert into 特殊 values(aa)insert into 特殊 values(2008-5-8)insert into 特殊 values(200)-建表加约束create database studentuse studentcreate table t_student( sno char(10), sname varchar(10), ssex char(4), sage int, sdep cha

12、r(20), sphone char(15)select * from t_studentinsert into t_student values(01,张三,男,25,软工,110)insert into t_student values(01,张三feng ,猫,2588,软工,110)-添加约束:保证数据的完整性drop table t_student-第一种加约束的方法create table t_student( sno char(10) primary key,-主键约束:具有唯一性,他能够决定一条记录,不能为空 sname varchar(10) not null,-非空约束 s

13、sex char(4) check(ssex=男 or ssex=女) default 男,-检查约束:范围 sage int check(sage=0 and sage=0 and sage=0 and sage=0 and score=0 and em_age,!=,不等于select * from t_studentwhere sage20select * from t_studentwhere sdep软工-逻辑:and,orselect * from t_studentwhere sage=22 and sage22 and sdep=网工select * from t_studen

14、twhere sage22 or sdep=网工-between.andselect * from t_studentwhere sage=22 and sage=10 and 年龄22-8.查询出年龄在-22之间的学生的姓名,年龄,所在班级select 姓名,年龄,班级 from studentinforwhere 年龄=20 and 年龄=22select 姓名,年龄,班级 from studentinforwhere 年龄 between 20 and 22-9.查询出学生信息表中学生都来自哪地方select distinct 籍贯 from studentinfor-10.将所有学生的

15、学号与姓名作为一个字段联合显示:+(同一个类型)select 学号+姓名 as 联合显示 from studentinforselect 姓名+年龄 as 联合显示 from studentinfor/*错*/-类型转换-11.查询从第三条记录开始的二条记录(,)select top 2 * from studentinforwhere 学号 not in(select top 2 学号 from studentinfor)-12.查询出所有男同学的信息select * from studentinforwhere 性别=男-常用统计函数:聚合函数select * from studentin

16、for-求和:sum()select SUM(年龄) as 年龄之和 from studentinfor-求平均:avg()select AVG(年龄) as 平均年龄 from studentinfor-max()select MAX(年龄) as 最大年龄 from studentinfor-min()select MIN(年龄) as 最低年龄 from studentinfor-count():计数select COUNT(*) as 数量 from studentinforwhere 性别=男-模糊查询:likeselect * from studentinfor-%:任意字符select * from studentinforwhere 姓名 like 于%-下划线:任意单个字符insert into t_student values(06,张三丰,男,26,网工,556)select * from t_studentselect * from t_studentwhere sname like 张%select * from t_studentwhere sname like 张_select * from t_studentwhere sname like

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 其他


经营许可证编号:宁ICP备18001539号-1