首页 > 数据库 > SQL Server > 正文

看到当年自己学SQL Server 的笔记

2024-08-31 00:55:09
字体:
来源:转载
供稿:网友
看到当年自己学SQL Server 的笔记
  1 数据库  2 数据量DataBase,不同类型的数据应该放到不同的数据库中,  3     1.便于对各个数据类别进行个性管理  4     2.避免命名冲突  5     3.安全性更高;  6 table(表):数据库中的关系指的就是表;  7     一张表就是一个类,列就是类的字段,行就是一个类的对象  8 数据库的主键(PRimary key);  9     主键就是数据行的唯一标识。不会重复的列才能当主键。一个表可以没有主键,但是会非常难以处理,因此没有特殊理由表都要设定主键 10     主键有两种选用策略:业务主键和逻辑主键。业务主键是使用有业务意义的字段做主键,比如身份证号、银行账号等;逻辑主键是使用没有任何业务意义的字段做主键,完全给程序看的,业务人员不会看的数据。因为很难保证业务主键不会重复(身份证号重复)、不会变化(帐号升位),因此推荐用逻辑主键。 11  12     1.主键的作用:唯一标识表中的一条记录。 13     2.选择多列同时作为一个主键→组合主键(复合主键).(一般不建议采用) 14     组合主键-复合主键(多列同时作为主键)—不推荐 15     1.唯一的。不能为空值 16     2.不经常变化的(稳定)比较稳定的列(不经常更新的,最好是建好以后再也不更新。) 17     3.大量字符串的列不适合作为主键 18     4.优先选择单列作为主键(避免使用组合主键) 19     5.优先使用逻辑主键(没有意义的),避免使用业务主键(身份证号、工号等。) 20  21 主键表,外键表 22     外键表就是A表引用了B表的主键,那么A表叫做外键表, 23     B表叫做主键表,两者之间的联系通过主键和外键联系, 24  25 创建数据库 26     create database MyDatabase 27     on 28     ( 29         name='文件名', 30         filename='文件地址', 31         size=3mb, 32         filegrowth=1mb,--自动增长     33     ) 34     log on --操作日志文件 35     ( 36         name='文件名_log', 37         filename='地址名_log', 38         size=1mb, 39         filegrowth=10%, 40     ) 41  42 创建表 43     create table Student 44     ( 45         stuid int primary key identity(1,1), 46         stuName nvarchar(15) not null, 47         stuGender bit not null, 48         stuAge int , 49         stuClass nvarchar(20), 50     ) 51      52 数据类型 53     nvarchar(10),存10个汉字,10个字母 54     varchar(10),里面的内容可以是10个也可以不是10,少于10个 55     char(10),5个汉字,10个字母, 如果存的内容(字母)小于10,不足的用空格补充.固定的内容 56     nchar(10),凡是带n的那么就可以存汉字 57     int bit(true,false)写代码的时候用1,0的方式 float 58     --查询表中的全部内容 59     select *from Student--(表名) 60  61 向表中插入数据 62     --第一种方式 63     insert into Student(stuName,stuGender,stuAge,stuClass)values('刘备',1,27,'三年四班') 64     --第二种方式 65     insert into Student values('小乔',0,18,'三年二班') 66     --第三种,可以多条插入 用select 和union 连接 67     insert into Student 68     select '貂蝉',0,18,'三年四班' union 69     select '郭嘉',1,38,'三年三班' union 70     select '张飞',1,34,'三年四班' 71 修改表中的数据 72     --修改数据 关键字 是update <表名> set <要修改的列名=要修改的值> where 条件 73     update Student  set stuAge-=1 74     update Student set stuAge=30 where stuid=1 75  76 删除数据 77     --删除数据 delete from <表名> where <条件> 78     delete from student where stuid=7 79     --delete 删除的是数据,表还在,但自动增长的Id会接着已经删除的id接着加,不会在从1开始 80     --第二种删除方式 能把表给删除 81     drop table student 82     --第三种方i 式删除 删除的也是数据 表还在 但id是从1开始 83     truncate table student 84     --truncate 效率要比 delete 快的多  85  86 约束--非空约束 87     not null 88     --主键约束 89     primary key  90     --为年龄增加一个检查约束:年龄必须在-120岁之间,含岁与岁。 91     alter table student add constraint CK_stuAge check(stuAge >=0 and stuAge<=120) 92     --为EmpId增加一个主键约束 93     alter table student add constraint PK_stuId primary key(stuid) 94     --非空约束,为stuName增加一个非空约束 95     alter table student alter column stuName varchar(50) not  null 96     --为stuName增加一个唯一约束 97     alter table student add constraint UQ_stuName unique(stuName) 98     --为性别增加一个默认约束,默认为'男' 99     alter table student add constraint DF_stuGender default('男') for stuGender100     --手动修改一下stuName的数据类型(varchar(200))101     alter table student alter column stuName varchar(200)102 103 增加删除列104     --增加一个EmpId列 105     --alter  table <表名> add <要加的列名> <数据类型>106     alter table student add EmpId int107 108     --手动删除一列(删除列) 109     --alter table <表名> drop column <要删除的列名>110     alter table student drop column   empid  --(要删除的列名)111 112 查询表中的内容113     --第一种方式114     select stuName as '姓名',stuAge as '年龄'from student115     --第二种方式116     select 姓名=stuName,年龄=stuAge from student117     118     --带条件查询119     select *from Student where stuGender=0120     --查找前十位 top 121     select top 10 * from TblStudent122 123     --查找按照年龄从小到大 排序 order by  asc 124     select *from TblStudent order by TSAge  asc125 126     --查找按照年龄从大到小 排序 order by  desc 127     select *from TblStudent order by TSAge desc128 129     --查找年龄最小的前十位130     select top 10 * from tblstudent order by TSAge asc131 132     --查找年龄最大的显示总数的10%133 134     select top 10  percent * from TblStudent order by TSAge desc135 136     --去除重复姓名查找137     select distinct TSName as '姓名' from TblStudent 138 139 聚合函数140 141     --查询多少行; count()只能有一个参数142     select COUNT(TSName) as '人数' from TblStudent143     --也可以这样查询144     select COUNT(TSName) as '人数',COUNT(TSCardId) as '标号' from TblStudent145 146     --切换成绩表147     select *from TblScore148 149     --查询英语成绩最高150     select MAX(tsenglish) as '英语最高' from TblScore151 152     --查询英语最低 和数字最高分153     select MIN(tsenglish) as '英语最低',MAX(tsmath) as '数学最高' from TblScore154     155     --聚合函数还有sum()求和,avg()求平均值,len()长度,156     157 多条件查询158     --查询年龄在20-30之间的男生159     select * from student where stuage>=20 and stuage<=30 and stugender=1160     第二种做法 用between ...and ...161     select * from student where stuAge between 20 and 30 and stugender =1162     --in 查询班级id 为1 或者2  或者 3 的同学信息163     select * from student where classid=1 or classid=2 or classid=3164     第二种做法165     select * fromstudent where classid in(1,2,3)166 模糊查询167     --名字中已张字开头的%代表任何内容,一字或多字168     select 8 from student where tsName like '张%'169     --姓张的后面跟个%符号的,170     select 8 from student where tsName like '张[%]%'171     --查询姓张的的但是后面只能有一个字'_' 172     select 8 from student where tsName like '张_'173     --查询姓张的姓名一共三个字174     select 8 from student where tsName like '张%' and len(tsName)=3175 176 数据分组    177     --查询每个班级的id有多少人178     --group by 分组179     select stuid as '班级的id',180     count(*) as '人数'181     from student182     group by stuid183     184     --查询每个班中男生有多少人185     select stuid as '班级',186     count(*) as '人数'187     from student188     where stuGender=1189     group by stuid190     191     --查询每个班人数超过5个的人192     select stuid as '班级',193     count(*) as '人数'194     from student195     group by stuid196     having count(*) in(5)197     --注意having 不能使用在未分组的列 having 是筛选的意思198     199     --统计每种商品的总销售量 并进行降序排序200     select 商品名称,201     SUM(销售数量) as '总销售量'202     from MyOrders203     group by 商品名称204     having SUM(销售数量) >100205     order by '总销售量' desc206     207     --请统计总销售价格超过3000元的商品和销售总价 并进行降序排序208     select 商品名称,209     SUM(销售数量*销售价格) as '总销售价'210     from MyOrders211     group by 商品名称212     having SUM(销售数量*销售价格)>3000213     order by '总销售价' desc214         215     --统计各个客户对可口可乐的喜爱度(统计每个购买人对可口可乐的购买数量)216     select 购买人,217     sum(销售数量)as '销售总量'218     from MyOrders219     where 商品名称='可口可乐'220     group by 购买人221     order by '销售总量' desc    222 223 类型转换函数224 数据类型转换的语法是225     如果要把int类型转换为字符串 convert(char,列名)226     例如如果要把所有的信息放在一个单元格之中的语法就是227     select stuname+convert(varchar(10),stuAge)+stuClass from student228 229     --表连接,首先列数相同 ,类型相同230 231     select *from TblTeacher232 233     select *from TblStudent234 235     select ttname ,ttgender from TblTeacher236     union --单独的union 去除重复 加上all 不去重复237     select tsgender,tsaddress from TblStudent238     239     select * from TblScore240     241     --查询学生的的 最高成绩 最低成绩 总分242     --第一种243     select max(tenglish) as '最高成绩',244     MIN(tenglish) as '最低成绩',245     SUM(tenglish) as '总成绩'246     from TblScore247     248     --第二种249     select '最高成绩',MAX(tenglish) from TblScore250     union251     select '最低成绩',MIN(tenglish) from TblScore252     union 253     select '总成绩',SUM(tenglish) from TblScore254 255     1 2 3 变成列256     select 1 union257     select 2 union258     select 3 259     260 数据备份--261     select *from TblStudent262     --把TblStudent表中的结构和数据,存到一个新表中,这个新表可以没有,在备份数据的同时新表自动生成263     --就是把一个表复制过来264     select *into newd from TblStudent265     --表复制了 但没有数据266     select *into newd1 from TblStudent where 1<>1267     --第三种 表有了 没有数据 建议使用这个268     select top 0 * into newd2 from TblStudent269     --表要存在的一中270     insert into newd3 select *from TblStudent271 272 字符串函数273     select len("字符串的长度");274     select datalength('aa');--获得字符串的字节数275     --获得tblstudent表中tsname的字节数276     select *,datalength(tsName)from tblstudent277     select lower('转小写');278     select upper('转大写');279     select rtrim('去除字符串右侧的空格');280     select ltrim('去除字符串左侧的空格');281     select left('截取左边字符串',2)--'截取' 从左边切2个282     select right('从右边切字符串',2)-- '符串' 从右边切2个283
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表