当前位置:Gxlcms > 数据库问题 > 数据库总结

数据库总结

时间:2021-07-01 10:21:17 帮助过:31人阅读

".(本台)","lacal(本地)"登陆 点击电脑开始→ 运行→ cmd→ net start mssqlserver 服务器的启动 net stop mssqlserver 服务器的停止 .mdf 数据库文件,有且只有一个!(一般复制就复制mdf) .ldf 表示产生 日志并记录 ,至少有一个! Create database 数据库名 创建数据库名 注意:不能出现同名数据库 首字母不能为数字 use 数据库名 使用某个数据库 常用的数据类型 int:整形类型 varchar(50):字符串类型 decimal(18,2):小数类型 18:小数点后可以写18位 2:取小数点后俩位 datetime:时间类型 image:图片 create table 表名 --创建数据库表名 注意:不能出现同名表 对表进行:添加,修改,删除,查询 ( 列名 数据类型 列名 数据类型 not null, --not null列可否为空 列名 数据类型 primary key identity(1,1)not null, --设置主键自增长 ) 设置主键外键格式 设置主键的列的不能为空 不能重复 create table 表名1 ( 列名 数据类型 , 列名1 数据类型 primary key not null, ) create table 表名2 ( 列名1 数据类型 , foreign key (列名1) references 表名1 (列名1 ) , 列名 数据类型, ) go go可写可不写 insert into 表名 values() --括号里面的写的一定要跟你创建表时写的一一对应 values值的意思 uptade 表名 set 你要改为的内容 where 条件 delete from 表名 where 条件 truncate table 表名 --去除表中的内容 不删除表 数据库最重要的就是查询 投影 select * from 表名 select 列1,列2... from 表名 select distinct 列名 from 表名 --去重 筛选 select top 数字 列|* from 表名 (一)等值与不等值 select * from 表名 where 列名=select * from 表名 where 列名!=select * from 表名 where 列名>select * from 表名 where 列名<select * from 表名 where 列名>=select * from 表名 where 列名<=值 (二)多条件与范围 select * from 表名 where 条件1 and||or 条件2 ... select * from 表名 where between ... and ... select * from 表名 wherein (值列表) (三)模糊查询 like % _ select * from 表名 where 列 like %_.... 排序 select * from 表名 where 条件 order by 列名 ASC||DESC,列名 ASC||DESC 连接查询: 把多个表的列合在一个界面视图查看 第一种方法 第一步 :生成笛卡尔积 select*from 表1,表2 第二步: 对笛卡尔积进行筛选 select*from 表1,表2 where 表1.相关联列=表2.相关联列 第三步: 对你所需要的列进行显示 select 表1.列1,表1.列2,表2.列1...... from 表1,表2 where 表1.相关联列=表2.相关联列 --你所取得列一定要区分是那个表的 第二种方法 --后台运行速度比第一种方法快 建议使用 select *from 表1 join 表2 on 表1.相关联列=表2.相关联列 join 表3 on 表2.相关联列=表3.相关联列 join前 修饰符 默认inner left join左链接以左表为主 左表的信息全部显现出来 右表根据左表的需要显现 right join右链接以右表为主 右表的信息全部显现出来 左表根据右表的需要显现 子查询(嵌套查询) 必要因素:多个表一定是有相关联列的 至少俩层查询 先查询外层的再查询里层的 里层的查询市委外层的查询提供查询的结果 联合查询: 把多个表的行合在一个界面视图查看 -- 针对多个表的时候 一个表的时候用or就可以了 select 列1,列2 from 表1 where 条件 union select 列1,列2 from 表2 where 条件 分组查询:group...by...having... 统计函数(聚合函数) count(), max(), min(), sum(), avg() count(*)得到所有的行数 count(列)得到该列中所有非null个数。 max(列) 这一列的最大,min(列)这一列的最小 sum(列)这一列的和,avg(列)这一列的平均 select 列名,COUNT(*) from 表名 group by 列名 having 条件 .一旦使用group by分组了,则select和from中间就不能用*,只能包含两类东西一类是:group by 后面的列名,另一类是统计函数 having后面一般跟得是统计函数。它用来对分组后的数据进一步筛选。 对于统计函数生成的列,默认是无列名,可以通过下面的方法指定列名。 select 列名1 ,COUNT(*) as 起的列名1,avg(列名2) 起的列名2 from 表名 group by 列名1 转换函数 print cast (123as int)+10 --cast先写变量再写类型 字符串转换为整数型 print convert (int,123)+12 --convert先写类型再写变量 字符串转换为整数型 select YEAR(1993-03-09) --取年份 select DATEADD(DAY,1,1993-03-09) --加1天 select datediff(day,1993-03-09,2015-04-25)--计算你到今天活了多少天 print getdate() --获取当前电脑时间 print isdate(2015-04-25) --判断时间格式是否正确 正确返回1 否则0 print datename (weekday,2015-04-25) --datename返回的是一个字符串类型的值 今天周几 print datepart (weekday,2015-04-25) --datepart返回的是一个int类型的值 今天是这周的第几天 select YEAR(getdate())-year(1993-03-09)as 年龄 --获取你的年龄 select left(ASDFGH,3) --从左往右获取固定长度 right反之 select upper(AsdFgH) --转换成大写 lower 反之 select LEN(iubfbjnboidnslk) --len返回值int类型 返回总长度 print ltrim( UYH) --去掉左边的空格 rtrim反之 print substring(JHHGUUILIUI,6,3) --截取从3个长度从6开始 索引从1开始 print replace(2B2B2B2B2B2B,2B,HB) --替换字符串2B换成HB print replicate (你好,5) -- 复制你好5次显示 print reverse(就是这样子) --反转

 

数据库总结

标签:

人气教程排行