整理一下常用的数据库代码,避免自己忘记了。哈哈。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 |
--打开数据库服务(ctrl+r) net start mssqlserver --打开数据库(ctrl+r) ssms --使用代码创建数据库 create database 数据库名 go --防止数据库重名 if exists(select * from sys.sysdatabases where name='数据库名') drop database 数据库名 --使用数据库 use 数据库名 go --创建数据库表 create table 数据库名 ( 表名 int not null, stuName varchar(10) not null, stuAge int not null, stuSex varchar(2) not null, stuAddress nvarchar(50) not null, stuNo char(10) not null ) --创建数据库表(加约束) create table 数据库名 ( 表名 int not null primary key identity(100,1), stuName varchar(10) not null, stuAge int not null check(stuAge>=18 and stuAge<=40), stuSex varchar(2) not null check(stuSex='男' or stuSex='女'), stuAddress nvarchar(50) not null default '未填写', stuNo char(10) not null unique(stuNo), stuPhone char(11) not null check(len(stuPhone)=11) ) 表建立关系 create table 数据库名 ( 表名 int not null primary key foreign key references 关联表名(列名) ) 主键约束:primary key 标识列:identity(m,n) n表示起始数字 m表示增长量 identity默认从1开始以1增长 默认约束:default 默认值 检查约束:check(逻辑表达式) len(列名)区对应列名长度 唯一约束:unique(列名) 外键约束:foreign key references 关联表名(列名) 键外创建约束的基本格式 alter table 表名 add constraint 约束名称 约束类型 约束内容 键外主键约束 alter table 表名 add constraint PK_表名_列名 primary key(列名) 键外唯一约束 alter table 表名 add constraint UQ_表名_列名 unique(列名) 键外默认约束 alter table 表名 add constraint DF_表名_列名 default '默认值' for 列名 键外检查约束 alter table 表名 add constraint CK_表名_列名 check(逻辑表达式) 限制号码数 alter table 表名 add constraint CK_表名_列名 check(len(列名)=数) 男女 alter table 表名 add constraint CK_表名_列名 check(列名='男' or 列名='女') 键外外键约束 从表就是需要引用的那个表,主表就是被引用的表 alter table 从表表名 add constraint FK_从表表名_主表表名_从表列名 foreign key(从表列名) references 主表表名(主表列名) 添加数据 insert into 表名 values(第一个列要添加的数据,第二个列要添加的数据,……)中文英文要加单引号 --update修改数据语句 /* update 表名 set 列名=修改值 [where 更改条件] */ --没有where修改全部内容 update StuInfo set stuAddress='云南省' --有where表示对特定的数据进行修改 update StuInfo set stuAddress='云南省西双版纳' where stuAge=18 update stuinfo set cj+=5 原有基础上加五 update stuinfo set cj-=5 原有基础上减五 --delete删除数据语句 /* delete from 表名[where 删除条件] */ delete from StuInfo where stuAge=18 delete from StuInfo where stuAddress='云南省' --删除整个表的数据 delete from StuInfo --truncate删除整个表的数据 /* truncate table 表名 */ truncate table StuInfo /* delelte和truncate两者区别: (1)delete只是单纯删除表中数据,truncate删除表中数据并重新创建表 (2)truncate不能对有外键约束的表进行删除数据 */ --删除整个表的结构 drop table 表名 查询数据 select * from 表名 --*代表全部列名 select 列名 from 表名 --查询整个数据 select * from 表名 --查询单列数据 select 列名 from 表名 --查询多列数据 select 列名,列名,列名,…… from 表名 为列名取别名 方法一: select 列名 as 所取名称,列名 as 所取名称..... from 表名 方法二: select 列名 所取名称,列名 所取名称......from 表名 方法三: select 所取名称=列名,所取名称=列名......from 表名 --查询为空数据 select 列名 from 表名 where 为空列名 is null 不为空则是is not null --增加查询列 select 增加数据 as 增加列名 from 表名 --限制行数(限制分数排前三的人) select top 限制行数 * from 表名 where 条件 --限制年龄最大的三个人 select top 人数 * from 表名 where 条件 --条件查询 --使用and来实现条件查询 --查询成绩在80分以上并且年龄在20岁以上 select * from 表名 where 分数>=80 and 年龄>=20 --使用or来实现条件查询 --查询成绩在80分以上或者年龄在20岁以上 select * from 表名 where 分数>=80 or 年龄>=20 --带between的范围查询 --查询成绩在80到95之间的所有数据 select * from 表名 where score between 80 and 95--(score>=80 and score<=95) --带in的范围查询 --查询成绩是89分和95分的数据 select * from 表名 where score in(89,95) --查询年龄最大的学员。 select top 1 * from 表名 order by 列名 desc --和查询年龄最小的学员。 select top 1 * from 表名 order by 列名 asc --查询前百分之50条记录 select top 50 percent * from 表名 --使用order by实现排序 select 列名 from 表名[order by 排序列名 asc或desc] --升序(asc) --成绩按照升序排序 select * from 表名 order by 列名 asc --降序(desc) --成绩按照降序排序 select * from 表名 order by 列名 desc --随机抽取(newid()) --成绩随机抽取 select top 抽取数 * from 表名 order by newid() 防止数据重复 select distinct 列名 from 表名 --字符匹配 --‘_’表示单个字符 --查询姓为'张'的学生信息 select * from 表名 where 列名 like '张__' --‘%’任意长度字符串 --查询姓为‘张’的学生信息 select * from 表名 where 列名 like '张%' --查询姓名当中含有字母‘g’的学生信息 select * from 表名 where 列名 like '%g%' --‘[]’括号中所指定范围内的一个字符 --查询姓为‘张王李’的学生信息 select * from 表名 where 列名 like '[张王李]%' --‘[^]’不在括号中所指定范围的一个字符 --查询姓不为‘张王李’的学生信息 select * from 表名 where 列名 like '[^张王李]%' --聚合函数 --count() 返回结果集中行的数据 --返回男生人数 select COUNT(*) from 表名 where 列名='男' --sum()结果集值得总和 select SUM(age) from 表名 --avg()返回结果集值得平均值 select AVG(score) from 表名 --max()返回最大值 select max(score) from 表名 --min()返回最小值 select min(score) from 表名 --group by分组 /* 注意:前面查找的字段只能是列名或者是聚合函数 */ select COUNT(*) from 表名 where 列名='男' select COUNT(*) from 表名 where 列名='女' --以性别分组 select sex 性别,COUNT(*) from 表名 group by sex --以考试科目 select class 科目,sum(score) from 表名 group by class --having字句使用(group by分组之后只能用having条件查询) --查询以性别分组平均成绩在85分以上 select 列名,avg(列名) from 表名 group by 列名 having avg(列名)>85 --where、group by、order by的前后顺序 where>group by>order by --内联结(左联结和右联结只需要将inner换成left和right) select 需要查询的列名(有相同的要用表名.列名) from 当前表名 inner join 联结表名 on 当前表列名=联结表列名(两个表的列名要有外键关系) eg: select stuinfo.sno,score.cj from stuinfo inner join score on stuinfo.sno=score.sno 三表链接 select 需要查询的列名(有相同的要用表名.列名) from 第一个表,第二个表,第三个表 where 第一个表的列名=第二个表的外键列名 and 第二个表的列名=第三个表的外键列名(不一定是一对二和二对三) eg: select stuinfo.sno,score.cj,course.name from stuinfo,score,course where stuinfo=score.sno and score.cno=course.cno --查询所有学生信息(两个表的数据放到一个表当中) select * from StuInfo inner join ScoreInfo on StuInfo.id=ScoreInfo.id select * from StuInfo a inner join ScoreInfo b on a.id=b.id --查询姓名为张三的成绩 select StuInfo.name,ScoreInfo.score from StuInfo inner join ScoreInfo on StuInfo.id=ScoreInfo.id where name='张三' --数据库函数 --字符串函数 --charidex() 用来寻找一个指定的字符串在另一个字符串中的起始位置 select CHARINDEX('今','大吉大利,今晚吃鸡') --len() 返回传递给它的字符串长度 select LEN('sqlserver') --lower() 把传递给它的字符串转换成小写(大写字母转换成小写字母) select LOWER('SQLSERVER') --upper() 把传递给它的字符串转换成大写(小写字母转换成大写字母) select UPPER('sqlserver') --ltrim() 清除字符左边的空格 select LTRIM(' 诸葛亮 n') --rtrim() 清除字符右边的空格 select RTRIM(' 诸葛亮 ') --replace() 替换一个字符串中的字符 select REPLACE('你说你很快乐','你','我') --stuff() 在一个字符串中,删除指定长度的字符,并在该位置插入一个新的字符串 select STUFF('ABCDEFG',2,5,'我的地盘,听我的') --日期函数 /* yy年份 mm月份 dd日 hh小时 mi分钟 ss秒 dw星期 */ --getdate() 取得当前的系统时间 select GETDATE() --dateadd() 将指定的数值添加到指定的日期部分的日期上 select DATEADD(dd,4,'2018-3-20') --datediff() 两个日期之间的相差日期部分时间 select DATEDIFF(yy,'2017-01-01','2018-3-1') --datename() 日期中指定日期部分的字符串形式 select DATENAME(dw,'2018-3-20') --datepart() 日期中指定日期部分的整数形式 select DATEPART(mm,'2018-3-20') --数学函数 --abs() 取数值的绝对值 select ABS(-43) --ceiling() 返回大于或等于所给数字表达式的最小整数 select CEILING(43.1) --floor() 取小于或等于指定表达式的最大整数 select FLOOR(43.1) --power() 取数值表达式的幂值 select POWER(5,3) --round() 将数值表达式四舍五入为指定小数位数 select ROUND(43.563,1) --sign() 对于正数返回1,对于负数返回-1,对于0返回0 select sign(0) --sqrt() 取浮点表达式的平方根 select sqrt(81) |
评论