T-SQL 基础学习 01

  1 --新建数据库
  2 create database Studentdb
  3 go
  4
  5 --使用数据库
  6 use Studentdb
  7 go
  8
  9 --新建表
 10 create table Username
 11 (
 12     StudentNo int identity(1,1) not null,--学号并设置标识列
 13     LoginPwd nvarchar(20) not null,--密码
 14     StudentName nvarchar(20) not null,--姓名
 15     Sex char(2) not null,--性别
 16     GradeId int not null,--年级
 17     Phone nvarchar(20)not null,--电话
 18     Address nvarchar(40) not null,--地址
 19     BornDate datetime,--生日
 20     Email nvarchar(20) not null,--邮件
 21 )
 22
 23 --创建一个主键约束
 24 alter table Username
 25 add constraint Keyname--主键的别名
 26 primary key(StudentNo)
 27
 28 --通过select into语句将现有表中的数据添加到新表中
 29 select identity(int,1,1) as StudentNo
 30 into NewTable
 31 from Username
 32
 33 --通过UNION关键字合并数据进行插入
 34 insert Username(LoginPwd,StudentName,Sex,GradeId,Phone,Address,BornDate,Email)
 35 select ‘123‘,‘秦浩天‘,‘男‘,‘S2‘,‘13518159261‘,‘北京抚琴南路‘,1992-05-12,‘[email protected]‘ UNION
 36 select ‘51842‘,‘王冰‘,‘女‘,‘Y2‘,‘15883958283‘,‘上海交大西门‘,1990-10-05,‘[email protected]‘
 37
 38 --使用UPDATE更新数据
 39 update Username
 40 set StudentName=‘秦浩然‘
 41 where StudentName=‘秦浩天‘
 42
 43 update Score
 44 set Score=Score+5
 45 where Score <= 95
 46
 47 --使用delete删除数据
 48 delete from Username
 49 where StudentName=‘秦浩然‘
 50
 51 --使用truncate table删除数据
 52 truncate table Username
 53
 54 --查询部分行或列
 55 select LoginPwd,StudentName,Address
 56 from Username
 57 where StudentName=‘王冰‘--反之:where StudentName<>‘王冰‘
 58
 59 --查询中使用别名
 60 select StudentName as 学生姓名,Address as 地址,Phone as 电话号码
 61 from Username
 62 where StudentName <> ‘王冰‘
 63 --查询结果合并
 64 select StudentName+‘.‘+LoginPwd as 姓名.密码
 65 from Username
 66
 67 select 姓名.密码 = StudentName+‘.‘+LoginPwd
 68 from Username
 69
 70 --查询空值
 71 select StudnetName
 72 from Username
 73 where LoginPwd is null
 74
 75 --用常量指定学校
 76 select 姓名=StudentName,地址=Address,‘北京大学‘ as 学校名称
 77 from Username
 78
 79 --显示前3条数据
 80 select top 3 *
 81 from Username
 82
 83 --按数据量的百分比显示此处显示20%
 84 select top 20 percent StudnetName,Address
 85 from Username
 86
 87 --使用order by排序,默认为asc
 88 select *
 89 from Score
 90 order by Score,desc
 91 --年龄如果为空姓名按降序排序
 92 select *
 93 from Student
 94 where Age is null
 95 order by StudentName desc
 96
 97 --用来寻找一个指定字符串在另一个字符串中的起始位置
 98 select charindex(‘name‘,‘My name is hqs‘,1)--返回int值:4
 99
100 --返回字符串长度
101 select len(‘不是穿上情侣装就可以装情侣‘)--返回int值:26
102
103 --字母转换为大写
104 select upper(‘my name is tom‘)--返回大写值:MY NAME IS TOM
105
106 --清除字符左边的空格
107 select ltrim(‘ I love you ‘)--返回:I lovey you
108
109 --清除字符右边的空格
110 select rtrim(‘ me too ‘)--返回: me too
111
112 --从字符串右边返回指定数目的字符
113 select right(‘北京.上海‘,2)--返回:上海
114
115 --替换一个字符串
116 select replace(‘爱上你等于爱上了错‘‘上‘,‘下‘)--返回:爱下你等于爱下了错
117
118 --删除指定长度,并插入新的字符串
119 select stuff(‘123456‘,2,4,‘中华人民‘)--返回:1中华人民6
120
121 --获取系统当前时间
122 select getdate()--返回系统当前时间:2009-05-11 12:00:00.000
123
124 --将指定的数值添加到指定的日期部分后的日期
125 select dateadd(mm,4,‘01/05/1992‘)--返回:05/05/1992 注意这里的:4和日期中的天数
126
127 --两个日期之间的间隔
128 select datediff(mm,‘01/05/1992‘,‘05/05/1992‘)--返回:4
129
130 --指定字符串形式
131 select datename(dw,‘01/01/2000‘)--返回:当前是星期几
132 --年:yy 季度:qq 月份:mm 一年中第几天:dy 天:dd 周:wk 一周星期几:dw 小时:hh 分钟:mi 秒钟:ss 毫秒:ms
133
134 --指定日期中的整数
135 select datepart(day,‘01/15/2000‘)--返回15
136
137 --返回0到1之间的随机float值
138 select rand()--返回:0.7952618415626
139
140 --取数值表达式的绝对值
141 select abs(-43)--返回:43
142
143 --向上取整数
144 select ceiling(43.5)--返回:44
145
146 --向下取整数
147 select floor(43.5)--返回:43
148
149 --取数值表达式的幂值
150 select power(5,2)--返回:25
151
152 --四舍五入为指定精度
153 select round(43.541,1)--返回:43.500
154
155 --对于正数返回+1,对于负数返回-1,对于0返回0
156 select sign(-12)--返回:-1
157
158 --取浮点表达式的平方根
159 select sqrt(9)--返回:3
160
161
162 --转变数据类型
163 select convert(varchar(5),‘12345‘)--返回int型:12345
164
165 --返回当前用户的名字
166 select current_user--返回:你登录的用户名
167
168 --返回用于用于指定表达式的字节数
169 select datalength(‘我的世界我做主‘)--返回:7
170
171 --返回当前用户所登录的计算机名字
172 select host_name()--返回:你所登录的计算机的名字
173
174 --返回当前所登录的用户名称
175 select system_user--返回:你当前所登录的用户名
176
177 --给定用户的ID返回用户名
178 select user_name(1)--返回:从任意数据库中返回"dbo"
179
180 --使用like进行模糊查询
181 select * from Username where StudentName like ‘王%‘
182
183 --between在某个范围内进行查询
184 select * from Username where BornDate not between ‘2010-01-01‘ and ‘2010-12-12‘
185
186 --使用in在列举值内进行查询
187 select StudentName as 学生姓名
188 from Username
189 where Address in(‘北京‘,‘广州‘,‘上海‘)
190 order by Address
191
192 --sum()求和
193 select sum(Score) as 学号为23的学生总分
194 from Score
195 where StudentNo=23
196
197 --avg()求平均
198 select avg(Score) as 平均成绩
199 from Score
200 where Score>=60
201
202 --max()最大值 min()最小值
203 select avg(Score) as 平均成绩,max(Score) as 最高分,min(Score) as 最低分
204 from Score where Score >=60
205
206 --count() 集中计数
207 select count(*) as 及格人数
208 from Score
209 where Score>=60
210
211 --group by分组查询
212 select CourseId,avg(Score) as 课程平均成绩
213 from Score
214 group by CourseId
215
216 --查询男女学生的人数各是多少
217 select count(*) as 人数,SSex from Username
218 group by SSex
219
220 --查询每个年级的总人数
221 select count(*) as 年级人数,SGrade from Students
222 group by SGrade
223
224 --查询每个科目的平均分,并按照由低到高的顺序排列
225 select CourseId, avg(Score) as 课程平均成绩 from Score
226 group by CourseId
227 order by avg(Score) desc
228
229 --多列分组查询
230 select count(*) as 人数,SGrade as 年级,SSex as 性别 from Students
231 group by SGrade,SSex
232 order by SGrade
233
234 --查看一个班人数超过15个的年级
235 select count(*) as 人数,SGrade as 年级 from Students
236 group by SGrade
237 having count(*)>15
238
239 --查询平均分及格的课程信息
240 select CourseId as 课程编号,avg(Score) as 课程平均成绩
241 from Score
242 group by CourseId
243 having avg(Score)>=60
244
245 --查询每门课程及格总人数和及格学生的平均分
246 select count(*) as 人数,avg(Score) as 平均分,CourseId as 课程 from Score
247 where Score>=60
248 group by ScourseId
249
250 --查询每门课程及格总人数和及格平均分在80分以上的记录
251 select count(*) as 人数,avg(Score) as 平均分,CourseId as 课程 from Score
252 where Score>=60
253 group by CourseId
254 having avg(Score)>=80
255
256 --查询有多个员工工资不低于2000的部门编号
257 select 部门编号,count(*) from 员工信息表
258 where 工资>=2000
259 group by 部门编号
260 having count(*)>1
261
262 --在where子句中指定连接条件
263 select StudentName,CourseId,Score
264 from Students,Score
265 where Scode = StudentId
266
267 --在from 子句中使用inner join..on
268 select S.StudentName,C.CourseId,C.Score
269 from Students as S
270 inner join Score as C on (S.Scode = C.StudentId)
271
272 --左外连接查询
273 select S.SName,C.CourseId,C.Score
274 from Students as S
275 left outer join Score as C on S.Scode = C.StudentId
276
277 --右外连接查询
278 select Titles.Title_id,Titles.Title, Publishers.Pub_name
279 from titles
280 right outer join Publishers on Titles.Pub_id=Publishers.Pub_id

转载出至:http://blog.csdn.net/heqingsong1/article/details/7495496
时间: 2024-08-26 13:57:32

T-SQL 基础学习 01的相关文章

pyqt listview基础学习01

from decimal import * from PyQt4.QtGui import * from PyQt4.Qt import * from PyQt4.QtCore import * import sys class Example(QWidget): def __init__(self,args=None): super(Example, self).__init__(args) list_data=[1,2,3,4] lm=MyListMode(list_data,self) s

SQL基础学习_04_视图

视图 1. 视图的创建 ? ? 视图就是保存好的SELECT语句,这些SELECT语句执行之后会产生新的表,所以在SQL中,视图和表是不做差别对待的,也就是SQL也可以对视图做一些操作: ? ? 由于视图并不实际保存数据,只是保存了执行的SELECT语句,可以节约存储空间,但是对于大数据量的存储,使用视图来频繁操作的话,势必会占用较大的计算时间,也算是一种用时间换空间的方案. ? ? 通过以下语句创建视图: ? ? CREATE VIEW 视图名称 (<视图列1>,<视图列2>,&

SQL 基础学习: 和深度学习资料

SQL is a standard language for storing, manipulating and retrieving data in databasee. 关系型数据库:RDBMS(Relational Database Mangement System) SQLite3: Rails默认的轻量级数据库,集成于Rails中,在db/development.sqlite3这个档案中. 用途:单机用途.所以在实际部署的时候会换成MySQL等数据库服务器. MySQL: 目前流行的开

sql基础学习

学习参考网站:http://www.runoob.com/sql/sql-tutorial.html 一.SQL命令 1.SELECT 语句 用于从数据库中选取数据. select column_name,column_name from table_name; #数据库中选取某个表格某几列数据 select * from table_name; #数据库中选取某个表格所有列数据 2.SELECT DISTINCT 语句 用于返回唯一不同的值. select distinct column_na

SQL基础学习笔记(三)—约束

约束: 一.创建表时,定义约束 create table emp2( id number(10) constraint emp2_id_nn not null, name varchar2(20) not null, salary number(10,2) ) constraint emp2_id_nn 给约束起个名增加阅读性,不写也行,会自动加上sys_XXXX 作用范围: ①列级约束只能作用在一个列上 ②表级约束可以作用在多个列上(当然表级约束也可以作用在一个列上) 定义方式:列约束必须跟在

PHP基础学习01

PHP语法基础 PHP是一种服务器端技术,不能在客户端运行 保存PHP脚本是必须使用.php扩展名,(例如:index.php). 必须把PHP代码放置在<?php和?>标签之间. <?php print "Hello World"; ?> PHP注释,注释将不会被发送至Web浏览器.PHP提供三种添加注释的方法. <?php         print "Hello"; //第一种单行注释         print "Wo

SQL基础学习_05_函数、谓词、CASE表达式

函数 算术函数 1. 四则运算: +.-.*./? 2. ABS:求绝对值, ABS(数值) 3. MOD: 求余,MOD(被除数,除数) 4. ROUND:四舍五入,ROUND(对象数值,保留小数的位数) 字符串函数 1. 字符串拼接: ||, str1 || str2 || str3 || - || strn ?AS str_all 2. 字符串长度:LENGTH,LENGTH(str1) AS len_str1 3. 小写转换:LOWER,LOWER(str1) AS low_str 4.

SQL基础学习笔记(一)

感觉SQL,学的云里雾里的,整理一下笔记吧. SQL语言分为三种: DML: Data Manipulation Language 数据操纵语言 DDL:  Data Definition Language 数据定义语言 DCL:  Data Control Language 数据控制语言 select * (表示查询表中所有的列 ) from employees; select employee_id , last_name,email from employees;(查询指定) SELECT

SQL基础学习笔记(二)

向表中插入一条数据 <span style="font-family:KaiTi_GB2312;font-size:18px;"><strong>create table emp1 as select employee_id , last_name , hire_date , salary from employees where 1 = 2</strong></span> 需要注意的是,插入的数据,必须和表中数据种类,一一对应 <