随笔 - 2,  文章 - 3,  评论 - 0,  阅读 - 1272
< 2025年7月 >
29 30 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 1 2
3 4 5 6 7 8 9

==============================================================================================

1.基本知识简介:

         01.服务和实例:

                            服务相当于引擎,而实例相当于授权

                            在使用数据库的时候,利用实例名进行登录,进入数据库管理系统以后就可以处理相关操作

                            即使在同一台计算机中,实例和实例之间没有数据的共享,仅仅是空间共享

         02.字符串用单引号

                            大小写不敏感,关键字不敏感,字符串值大小写敏感

                            切换数据库: use 数据库名 如:use School

                            go表示将前面的SQL打包,一起执行

                            go不属于T-SQL,它不是SQL语句的一部分

                            go属于sqlcmd的命令

         03.数据库架构:

                            架构Schema相当于 命名空间

                            架构默认为:dbo 即:database object

                            每张表对应一个类(ORM)

                            每一行数据就对应一个完整的对象(记录、元祖)

                            一张表的所有数据构成一个集合

                            常用的Management值时一个管理软件,用来讲T-sql命令交给DBMS, 将数据展现出来      

                            表名 建议使用"架构名。表名"描述(Schema:架构)架构默认为:dbo 即:database object

         04.文件简称:

                            数据文件-mdf : master database file

                            日志文件-ldf : log database file

                            非主数据文件-ndf : not master database file

=============================================================================================

2.数据库和表的创建和删除

         01.字段类型:

                   字符串类型:

                            char(n):

                                     char类型最大容量为n个字节,若存储数据不足n个字节,内存一样会分配n个字节的长度

                            varchar(n)

                                     varchar类型最大容量为n个字节,内存按存储数据的字节数来分配长度,

                            nchar(n)

                                     nchar类型最大容量为n个字符,若存储数据不足n个字符,内存一样会分配n个字符的长度

                            nvarchar(n):

                                     nvarchar类型最大容量为n个字符,内存按存储数据的字符数来分配长度

                   二进制数据类型:

                            image

                                     image类型可以存储图片数据

                   时间和日期类型:

                            date

                            datetime

                            time

                   数据类型:

                            int

                            real   实数类型

                            smailint

                            float

                   货币类型:

                            Money

                   bit数据类型:

                            bit  存储bool类型数据

         02.创建数据库:

                   如果自定义名字与关键字冲突或中间需要有空格等,则使用[]将其括起来

                   create database db_MyDataBase

                   on

                   (

                            name='逻辑名',

                            filename='文件名.mdf',

                            size=  MB  KB  TB,

                            filegrowth=文件增长MB 或 %,

                            maxsize=文件的最大值

                   )

                   log on

                   (

                            name='逻辑名',

                            filename='文件名.ldf',

                            size= MB KB TB,

                            filegrowth = 文件增长速度 MB 或 %,

                            maxsize = 文件的最大值

                   )

         03.创建表

                   use db_MyDataBase

                   create table dbo.tb_MyTable

                   (

                            字段名  字段类型,

                            字段名  字段类型

                   )

         04.删除数据库:

                   --习惯上我们先判断数据库是否存在,再删除

                   if(DB_ID('MyDateBase')is not null)

                   drop database db_MyDateBase

         05.删除表:

                   if(OBJECT_ID('tb_MyTable')is not null)

                   drop table dbo.tb_MyTable

==========================================================================================

3.数据简单的增删改查         

         01.增

                   insert into tb_MyTable('字段1','字段1')

                   values('值1','值2')

         02.删

                   delete  dbo.tb_MyTable

                   delete from dbo.tb_MyTable  where 条件

         03.改

                   update dbo.tb_MyTable set 字段名 = 值

                   update dbo.tb_MyTable set 字段名 = 值 where 条件

         04.查

                   select * from dbo.tb_MyTable where 条件

                   select 字段名 from dbo.tb_MyTable where 条件

=============================================================================================

4.约束:

         00.约束前准备:

                   在创建约束时,我们一般先把数据删除

                   delete  from 表名

                   truncate table 表名

                   truncate会删除表的所有数据,包括所有动作,不计入日志

         01.主键约束PK:

                   主键是唯一键,不可重复,不可为null

                   在创建表时约束:

                            字段  字段类型 primary key

                            name  nvarchar(10) primary key

                   在创建表后约束:

                            alter table tb_MyTable

                            add constraint

                            PK_tb_MyTable_Name

                            primary key(Name)

         02.唯一约束UQ:

                   约束的字段不可重复,可以为null

                   在创建表时:

                            Name  nvarchar(10) unique

                   在创建表后:

                            alter table tb_MyTable

                            add constraint

                            UQ_tb_MyTable_Name

                            unique(Name)

         03.默认约束DF:

                   在值为null时 系统默认字段的值

                   在创建表时:

                            Name nvarchar(10) default('默认值')

                   在创建表后:

                            alter table tb_MyTable

                            add constraint

                            DF_tb_MyTable_Name

                            default('默认值')

         04.检查约束CK:

                   该字段值得范围和格式限制  >  = < and  len() 等等

                   在创建表时:

                            Age int check(Age>0 and Age<100)

                   在创建表之后:

                            alter table tb_MyTable

                            add constraint

                   CK_tb_MyTable_Age

                   check(Age>0 and Age<100)

         05.外键约束FK:

                   表关系,含有外键的表为外键表

                   创建表时,创建约束

                            约束创建到外键表上

                            先创建主键表

                            create table tb_MainTable

                            (

                                     IDCard nvarchar(18) primary key,

                                     Name nvarchar(10),

                            )

                            建外键表

                            create table tb_ForeignTable

                            (

                                     ID int primary key,

                                     IDCard nvarchar(18) foreign key references tb_MainTable(IDCard)

                            )

                   在建表后创建外键连接:

                            create table tb_MainTable

                            (

                                     IDCard nvarchar(18) primary key,

                                     Name nvarchar(10),

                            )

                            create table tb_ForeignTable

                            (

                                     ID int primary key,

                                     IDCard nvarchar(18)

                            )

                            alter table tb_ForeignTable

                            add constraint

                            FK_ForeignTable_MainTable_IDCard

                            Foreign Key(IDCard) references MainTable(IDCard)

=============================================================================================

5.查询语句:

         01.语法:

                   select 字段名 from 表名 where 条件

         02.添加列的别名:

                   1. 字段名    别名

                   2. 字段名 as 别名

                   3. 别名  = 字段名

         03.top order by:

                            系统默认排序为升序,在后面加上desc 表示降序, esc 表示升序

                            select top 3 * from 表名          表示显示排列的前三行数据

                            select top 3 percent * from 表名  表示显示排列的钱百分之3的数据

                            select * from 表名 where 条件 order by 字段

         04.去除重复数据:

                            select distinct 要选择的列 from 表名

                            select distinct  *  from 表名

         05.聚合函数:

                            sum  avg max  min  count  分别是 求和 求平均数 求最大值 求最小值 求总数

                            聚合函数常常与分组group by 一起用

                            group by

                            group by 表示分组,比如:以名字分组 group by  name

                            注意:group by 后面出现的字段 才可以再select语句中出现

                            group by 写在from 后面 写在order by 的前面

                                     例:

                                     select

                                               name,

                                               count(name)

                                     from

                                               class

                                     group by name

                   聚合函数的注意事项:

                            在处理聚合数据的时候,不计入Null数据

                            如果7组数据, 一组为空 怎 count 为6         

                            在SQL中 null 表示未知

                            null+1 = null

                            null-2 = null

                            即任何数和null 相运算都为null

         06.带条件查询

                   范围条件: between and

                            select * from 表名

                            where

                                     Age = 10

                                     or

                                     Age = 11

                                     or

                                     Age = 12

                            现在我们选用between and

                            between and 表示条件范围,

                            where

                                     Age between 10 and 20

                   集合条件:

                            语法:字段 in (数值)

                            求出年龄在 17 到 19 岁 和 21 岁和22 到50岁的人

                            where

                                     Age in(17,18,19,21)

                                     or

                                     (Age between 22 and 50)

         07.模糊查询:

                   如果字段名不太清楚,比如 吴战磊  XX磊

                            _   表示单个字符

                            %    零个或多个字符

                            select * from dbo.Class where Name like '__磊'  这样 XX磊都会被查询出来

                            [] 表示转译  若寻找***_log 

                            则用 ___[_]log

                            在方括号中[^]也表示否定,表示不出现这个元素

                            name like '[^赵]'  表示查询不含 赵 的字段

                            name not like '赵%' 同上

         08.isnull

                            在SQL中,使用三值逻辑(true,false,unknow)

                            >  =  <  >=  <=   <>(不等于 或 !=)  !<  !>

                            由于null的存在,我们要特殊处理

                            select       isnull(math,'缺考') from tblScore,即如果不为null 则显示math成绩,否则为缺考

                            求出数学成绩为null的人:

                            select * from tblScore where math is null

                            ISNULL(expression,value) :如果expression不为空则返回expression,否则返回value。

                            select studentId,isnull(english,0) from score

============================================================================================

6.一些语句函数:

         01.having函数:

                   select

                            stuAge,

                            count(stuAge)

                   from

                            classname

                   where

                            stuGender = 1

                   group by stuAge

                   having

                            stuAge>14

         02.结果集

                            查询年龄大于25的男同学

                            select

                            *

                            from classname

                            where

                                     stuAge>25

                            and

                                     stuGender = 1

                            union

                            查询年龄最大的女同学

                            select

                            top 1

                            *

                            from classname

                            where

                                     stuGender = 0

                            order by

                                     stuAge desc

                            联合结果集

                                     将以上两张表联合为一张表

                                     只需在上面一张表下加上一个union

                            union 会自动合并重复数据

                            我们用union all 会保留重复数据

         03.考虑类型处理

                            select 1234,'哈哈'

                            union

                            select '嘻嘻','string'

                            以上联合会报错 我们这样处理:

                            select cast(1234 as varchar(4)),'哈哈'

                            union

                            select '嘻嘻','string'

                            或者:

                            select convert(varchar(4),1234),'哈哈'

                            union

                            select '嘻嘻','string'

                            求出数据库的最高分、最低分、平均分

                            convert 函数的其他功能:

                            select convert(nvarchar(20),getdate(),101)

                            即转换为:11/7/2012

         04.字符串函数:

                            Len(): 计算字符串长度(字符的个数)

                            datalength(): 计算字符串所占用的字节数,不属于字符串函数

                            LOWER() UPPER(), 转大小写

                            ltrim():字符串左侧的空格去掉

                            rtrim():字符串右侧的空格去掉

                            left() right() 截取字符串

                                     select left('abcdefg',2)

                            substring(string.start_position,length)

                                     参数string为主子符串,start_position为子字符串在主字符串中的其实位置,length为子字符串的最大长度,

                                     select substring('abcdef123',2,3)

                                     第一个字符的索引值为:1

         05.日期函数:

                            getdate() 获得当前日期

                            获得时间的部分信息:

                                     select Year('2012-7-21'),month('2012-7-21'),day('2012-7-21')

                            获得年龄:

                                     select year(getdate())-year(birthday) as age from classname

                            时间间隔:

                                     今天下单,30天后付款,怎样确定时间间隔

                                     在下单时:

                                     select getdate(),dateadd(day,30,getdate())

                            时间求差

                                     select datediff(单位,开始时间,结束时间)

                            返回待定的部分:

                                     datepart(year,getdate())

=============================================================================================

         其他:

                   identity(1,1)   是让该字段的值从1 开始 每增加一个字段 值增加1

                   print 也可以显示数据,只是显示在消息栏

                   where  是在select之前执行,having 是在select之后进行,

                   如果对select中的数据进行赛选,就要放在having子句里面

                  

==============================================================================================

posted @ 2012-07-12 21:16 轻雨 阅读(181) 评论(0) 推荐(0)
摘要: .net基础加强简单小结: 01.面向对象的三大特征: 继承 封装 多态 02.过渡期的三个阶段: 模仿阶段 扶手阶段 创造阶段上节复习: 01.Path类常见的方法: Path.GetDirectorys(); Path.GetFileName(); Path.GetFileNameWithoutExtention(); 02.什么叫深考,什么叫浅考 有什么区别: 深考和浅考只有在对象中有引用了类型时才区分; 03.回答:文件夹递归能否写出来:(写熟) 04.object 提供的完成浅考的方法是什么?返回什么类型,使用时注意什么? public void MyClone() { return 阅读全文
posted @ 2012-07-09 21:14 轻雨 阅读(218) 评论(0) 推荐(0)
点击右上角即可分享
微信分享提示