欧美三区_成人在线免费观看视频_欧美极品少妇xxxxⅹ免费视频_a级毛片免费播放_鲁一鲁中文字幕久久_亚洲一级特黄

轉(zhuǎn):SQL Server 數(shù)據(jù)庫基礎(chǔ)編程

系統(tǒng) 1927 0

SQL Server 數(shù)據(jù)庫基礎(chǔ)編程

?

? Go批處理語句

???? 用于同時執(zhí)行多個語句

?

? 使用、切換數(shù)據(jù)庫

          
            use
          
           master
        
          
            go
          
        
?

?

? 創(chuàng)建、刪除數(shù)據(jù)庫

方法1、

          --判斷是否存在該數(shù)據(jù)庫,存在就刪除
        
          
            if
          
           (
          
            exists
          
           (
          
            select
          
           * 
          
            from
          
           sys.databases 
          
            where
          
           name = 
          
            'testHome'
          
          ))
        
          
            drop
          
          
            database
          
           testHome
        
          
            go
          
        
          --創(chuàng)建數(shù)據(jù)庫,設(shè)置數(shù)據(jù)庫文件、日志文件保存目錄
        
          
            create
          
          
            database
          
           testHome
        
          
            on
          
          (
        
              name = 
          
            'testHome'
          
          ,
        
              filename = 
          
            'c:\data\students.mdf'
          
        
          )
        
          log 
          
            on
          
          (
        
              name = 
          
            'testHome_log'
          
          ,
        
              filename = 
          
            'c:\data\testHome_log.ldf'
          
        
          )
        
          
            go
          
        
?

?

方法2(設(shè)置文件大小)、

          
            if
          
           (
          
            exists
          
           (
          
            select
          
           * 
          
            from
          
           sys.databases 
          
            where
          
           name = 
          
            'testHome'
          
          ))
        
          
            drop
          
          
            database
          
           testHome
        
          
            go
          
        
          
            create
          
          
            database
          
           testHome
        
          --默認就屬于primary主文件組,可省略
        
          
            on
          
          
            primary
          
           (    
        
              --數(shù)據(jù)文件的具體描述
        
              name = 
          
            'testHome_data'
          
          ,                --主數(shù)據(jù)文件的邏輯名
        
              fileName = 
          
            'c:\testHome_data.mdf'
          
          ,    --主數(shù)據(jù)文件的物理名
        
          
            size
          
           = 3MB,                        --主數(shù)據(jù)文件的初始大小
        
              maxSize = 50MB,                    --主數(shù)據(jù)文件增長的最大值
        
              fileGrowth = 10%                --主數(shù)據(jù)文件的增長率
        
          )
        
          --日志文件的具體描述,各參數(shù)含義同上
        
          log 
          
            on
          
           (
        
              name = 
          
            'testHome_log'
          
          ,
        
              fileName = 
          
            'c:\testHome_log.ldf'
          
          ,
        
          
            size
          
           = 1MB,
        
              fileGrowth = 1MB
        
          )
        
          
            go
          
        
?

?

方法3(設(shè)置次數(shù)據(jù)文件)、

          
            if
          
           (
          
            exists
          
           (
          
            select
          
           * 
          
            from
          
           sys.databases 
          
            where
          
           name = 
          
            'testHome'
          
          ))
        
          
            drop
          
          
            database
          
           testHome
        
          
            go
          
        
          
            create
          
          
            database
          
           testHome
        
          --默認就屬于primary主文件組,可省略
        
          
            on
          
          
            primary
          
           (    
        
              --數(shù)據(jù)文件的具體描述
        
              name = 
          
            'testHome_data'
          
          ,                --主數(shù)據(jù)文件的邏輯名
        
              fileName = 
          
            'c:\testHome_data.mdf'
          
          ,    --主數(shù)據(jù)文件的物理名
        
          
            size
          
           = 3MB,                        --主數(shù)據(jù)文件的初始大小
        
              maxSize = 50MB,                    --主數(shù)據(jù)文件增長的最大值
        
              fileGrowth = 10%                --主數(shù)據(jù)文件的增長率
        
          ),
        
          --次數(shù)據(jù)文件的具體描述
        
          (    
        
              --數(shù)據(jù)文件的具體描述
        
              name = 
          
            'testHome2_data'
          
          ,            --主數(shù)據(jù)文件的邏輯名
        
              fileName = 
          
            'c:\testHome2_data.mdf'
          
          ,    --主數(shù)據(jù)文件的物理名
        
          
            size
          
           = 2MB,                        --主數(shù)據(jù)文件的初始大小
        
              maxSize = 50MB,                    --主數(shù)據(jù)文件增長的最大值
        
              fileGrowth = 10%                --主數(shù)據(jù)文件的增長率
        
          )
        
          --日志文件的具體描述,各參數(shù)含義同上
        
          log 
          
            on
          
           (
        
              name = 
          
            'testHome_log'
          
          ,
        
              fileName = 
          
            'c:\testHome_log.ldf'
          
          ,
        
          
            size
          
           = 1MB,
        
              fileGrowth = 1MB
        
          ),
        
          (
        
              name = 
          
            'testHome2_log'
          
          ,
        
              fileName = 
          
            'c:\testHome2_log.ldf'
          
          ,
        
          
            size
          
           = 1MB,
        
              fileGrowth = 1MB
        
          )
        
          
            go
          
        

?

? 基本數(shù)據(jù)類型

精確數(shù)字類型

類型

描述

bigint

bigint 數(shù)據(jù)類型用于整數(shù)值可能超過 int 數(shù)據(jù)類型支持范圍的情況,范圍:-2^63 到 2^63-1,存儲空間8字節(jié)

int

整數(shù)數(shù)據(jù)類型,范圍在-2^31 到 2^31-1,存儲空間4字節(jié)

smallint

整數(shù),范圍在-2^15 到 2^15-1,存儲空間2字節(jié)

tinyint

范圍在0 到 255,存儲空間1字節(jié)

bit

可以取值為 1、0 或 NULL 的整數(shù)數(shù)據(jù)類型,每8個bit占一個字節(jié),16bit就2個字節(jié),24bit就3個字節(jié)

decimal

帶固定精度和小數(shù)位數(shù)的數(shù)值數(shù)據(jù)類型,有效值從 - 10^38 +1 到 10^38 - 1

numeric

同上

money

貨幣或貨幣值的數(shù)據(jù)類型,范圍在-922,337,203,685,477.5808 到 922,337,203,685,477.5807

smallmoney

貨幣類型,-214,748.3648 到 214,748.3647

?

近似數(shù)字類型

類型

描述

float

表示浮點數(shù)值數(shù)據(jù)的大致數(shù)值數(shù)據(jù)類型。浮點數(shù)據(jù)為近似值;范圍-1.79E + 308 至 -2.23E - 308、0 以及 2.23E - 308 至 1.79E + 308

real

real 的 SQL-92 同義詞為 float(24),范圍在-3.40E + 38 至 -1.18E - 38、0 以及 1.18E - 38 至 3.40E + 38

?

日期時間類型

類型

描述

datetime

表示某天的日期和時間的數(shù)據(jù)類型,范圍在1753 年 1 月 1 日到 9999 年 12 月 31 日

smalldatetime

范圍在1900 年 1 月 1 日到 2079 年 6 月 6 日

?

字符串類型

類型

描述

char

固定長度或可變長度的字符數(shù)據(jù)類型,范圍在范圍為 1 至 8,000字節(jié)

text

最大長度為 2^31-1

varchar

固定長度或可變長度的字符數(shù)據(jù)類型,最大存儲大小是 2^31-1 個字節(jié)

?

Unicode字符串類型

類型

描述

nchar

字符數(shù)據(jù)類型,長度固定,在必須在 1 到 4,000 之間

nvarchar

可變長度 Unicode 字符數(shù)據(jù)。最大存儲大小為 2^31-1 字節(jié)

ntext

長度可變的 Unicode 數(shù)據(jù),最大長度為 2^30 - 1 (1,073,741,823) 個字符

?

二進制字符串類型

類型

描述

binary

長度為 n 字節(jié)的固定長度二進制數(shù)據(jù),范圍從 1 到 8,000 的值。存儲大小為 n 字節(jié)。

varbinary

可變長度二進制數(shù)據(jù)。n 可以取從 1 到 8,000 的值。最大的存儲大小為 2^31-1 字節(jié)

image

長度可變的二進制數(shù)據(jù),從 0 到 2^31-1 (2,147,483,647) 個字節(jié)

?

? 判斷表或其他對象及列是否存在

          --判斷某個表或?qū)ο笫欠翊嬖?
        
          
            if
          
           (
          
            exists
          
           (
          
            select
          
           * 
          
            from
          
           sys.objects 
          
            where
          
           name = 
          
            'classes'
          
          ))
        
          
            print
          
          
            '存在'
          
          ;
        
          
            go
          
        
          
            if
          
           (
          
            exists
          
           (
          
            select
          
           * 
          
            from
          
           sys.objects 
          
            where
          
           object_id = object_id(
          
            'student'
          
          )))
        
          
            print
          
          
            '存在'
          
          ;
        
          
            go
          
        
          
            if
          
           (object_id(
          
            'student'
          
          , 
          
            'U'
          
          ) 
          
            is
          
          
            not
          
          
            null
          
          )
        
          
            print
          
          
            '存在'
          
          ;
        
          
            go
          
        
          ?
        
          --判斷該列名是否存在,如果存在就刪除
        
          
            if
          
           (
          
            exists
          
           (
          
            select
          
           * 
          
            from
          
           sys.columns 
          
            where
          
           object_id = object_id(
          
            'student'
          
          ) 
          
            and
          
           name = 
          
            'idCard'
          
          ))
        
          
            alter
          
          
            table
          
           student 
          
            drop
          
          
            column
          
           idCard
        
          
            go
          
        
          
            if
          
           (
          
            exists
          
           (
          
            select
          
           * 
          
            from
          
           information_schema.columns 
          
            where
          
           table_name = 
          
            'student'
          
          
            and
          
           column_name = 
          
            'tel'
          
          ))
        
          
            alter
          
          
            table
          
           student 
          
            drop
          
          
            column
          
           tel
        
          
            go
          
        

?

? 創(chuàng)建、刪除表

          --判斷是否存在當前table
        
          
            if
          
           (
          
            exists
          
           (
          
            select
          
           * 
          
            from
          
           sys.objects 
          
            where
          
           name = 
          
            'classes'
          
          ))
        
          
            drop
          
          
            table
          
           classes
        
          
            go
          
        
          
            create
          
          
            table
          
           classes(
        
              id 
          
            int
          
          
            primary
          
          
            key
          
          
            identity
          
          (1, 2),
        
              name 
          
            varchar
          
          (22) 
          
            not
          
          
            null
          
          ,
        
              createDate datetime 
          
            default
          
           getDate()
        
          )
        
          
            go
          
        
          
            if
          
           (
          
            exists
          
           (
          
            select
          
           * 
          
            from
          
           sys.objects 
          
            where
          
           object_id = object_id(
          
            'student'
          
          )))
        
          
            drop
          
          
            table
          
           student
        
          
            go
          
        
          --創(chuàng)建table
        
          
            create
          
          
            table
          
           student(
        
              id 
          
            int
          
          
            identity
          
          (1, 1) 
          
            not
          
          
            null
          
          ,
        
              name 
          
            varchar
          
          (20),
        
              age 
          
            int
          
          ,
        
              sex 
          
            bit
          
          ,
        
              cid 
          
            int
          
        
          )
        
          
            go
          
        

?

? 給表添加字段、修改字段、刪除字段

          --添加字段
        
          
            alter
          
          
            table
          
           student 
          
            add
          
           address 
          
            varchar
          
          (50) 
          
            not
          
          
            null
          
          ;
        
          --修改字段
        
          
            alter
          
          
            table
          
           student 
          
            alter
          
          
            column
          
           address 
          
            varchar
          
          (20);
        
          --刪除字段
        
          
            alter
          
          
            table
          
           student 
          
            drop
          
          
            column
          
           number;
        
          ?
        
          --添加多個字段
        
          
            alter
          
          
            table
          
           student 
        
          
            add
          
           address 
          
            varchar
          
          (22),
        
              tel 
          
            varchar
          
          (11),
        
              idCard 
          
            varchar
          
          (3);
        
          ?
        
          --判斷該列名是否存在,如果存在就刪除
        
          
            if
          
           (
          
            exists
          
           (
          
            select
          
           * 
          
            from
          
           sys.columns 
          
            where
          
           object_id = object_id(
          
            'student'
          
          ) 
          
            and
          
           name = 
          
            'idCard'
          
          ))
        
          
            alter
          
          
            table
          
           student 
          
            drop
          
          
            column
          
           idCard
        
          
            go
          
        
          
            if
          
           (
          
            exists
          
           (
          
            select
          
           * 
          
            from
          
           information_schema.columns 
          
            where
          
           table_name = 
          
            'student'
          
          
            and
          
           column_name = 
          
            'tel'
          
          ))
        
          
            alter
          
          
            table
          
           student 
          
            drop
          
          
            column
          
           tel
        
          
            go
          
        

?

? 添加、刪除約束

          --添加新列、約束
        
          
            alter
          
          
            table
          
           student 
        
          
            add
          
           number 
          
            varchar
          
          (20) 
          
            null
          
          
            constraint
          
           no_uk 
          
            unique
          
          ;  
        
          --增加主鍵
        
          
            alter
          
          
            table
          
           student  
        
          
            add
          
          
            constraint
          
           pk_id 
          
            primary
          
          
            key
          
          (id);  
        
          --添加外鍵約束
        
          
            alter
          
          
            table
          
           student
        
          
            add
          
          
            constraint
          
           fk_cid 
          
            foreign
          
          
            key
          
           (cid) 
          
            references
          
           classes(id)
        
          
            go
          
        
          --添加唯一約束
        
          
            alter
          
          
            table
          
           student
        
          
            add
          
          
            constraint
          
           name_uk 
          
            unique
          
          (name);
        
          --添加check約束
        
          
            alter
          
          
            table
          
           student 
          
            with
          
          
            nocheck
          
        
          
            add
          
          
            constraint
          
           check_age 
          
            check
          
           (age > 1);
        
          
            alter
          
          
            table
          
           student
        
          
            add
          
          
            constraint
          
           ck_age 
          
            check
          
           (age >= 15 
          
            and
          
           age <= 50)
        
          --添加默認約束
        
          
            alter
          
          
            table
          
           student
        
          
            add
          
          
            constraint
          
           sex_def 
          
            default
          
           1 
          
            for
          
           sex;
        
          --添加一個包含默認值可以為空的列
        
          
            alter
          
          
            table
          
           student 
        
          
            add
          
           createDate smalldatetime 
          
            null
          
        
          
            constraint
          
           createDate_def 
          
            default
          
           getDate() 
          
            with
          
          
            values
          
          ;
        
          ?
        
          ---
          
            -- 多個列、約束一起創(chuàng)建--------
          
        
          
            alter
          
          
            table
          
           student 
          
            add
          
        
          
            /*添加id主鍵、自增*/  
          
        
              id 
          
            int
          
          
            identity
          
          
            constraint
          
           id 
          
            primary
          
          
            key
          
          ,   
        
          
            /* 添加外鍵約束*/   
          
        
              number 
          
            int
          
          
            null
          
        
          
            constraint
          
           uNumber 
          
            references
          
           classes(number),  
        
          
            /*默認約束*/  
          
        
              createDate 
          
            decimal
          
          (3, 3)  
        
          
            constraint
          
           createDate 
          
            default
          
           2010-6-1  
        
          
            go
          
        
          ?
        
          --刪除約束
        
          
            alter
          
          
            table
          
           student  
          
            drop
          
          
            constraint
          
           no_uk;
        

?

? 插入數(shù)據(jù)

          insert 
          
            into
          
           classes(name) 
          
            values
          
          (
          
            '1班'
          
          );
        
          insert 
          
            into
          
           classes 
          
            values
          
          (
          
            '2班'
          
          , 
          
            '2011-06-15'
          
          );
        
          insert 
          
            into
          
           classes(name) 
          
            values
          
          (
          
            '3班'
          
          );
        
          insert 
          
            into
          
           classes 
          
            values
          
          (
          
            '4班'
          
          , 
          
            default
          
          );
        
          ?
        
          insert 
          
            into
          
           student 
          
            values
          
          (
          
            'zhangsan'
          
          , 22, 1, 1);
        
          insert 
          
            into
          
           student 
          
            values
          
          (
          
            'lisi'
          
          , 25, 0, 1);
        
          insert 
          
            into
          
           student 
          
            values
          
          (
          
            'wangwu'
          
          , 24, 1, 3);
        
          insert 
          
            into
          
           student 
          
            values
          
          (
          
            'zhaoliu'
          
          , 23, 0, 3);
        
          insert 
          
            into
          
           student 
          
            values
          
          (
          
            'mazi'
          
          , 21, 1, 5);
        
          insert 
          
            into
          
           student 
          
            values
          
          (
          
            'wangmazi'
          
          , 28, 0, 5);
        
          insert 
          
            into
          
           student 
          
            values
          
          (
          
            'jason'
          
          , 
          
            null
          
          , 0, 5);
        
          insert 
          
            into
          
           student 
          
            values
          
          (
          
            null
          
          , 
          
            null
          
          , 0, 5);
        
          ?
        
          insert 
          
            into
          
           student 
        
          
            select
          
          
            'bulise'
          
           name, age, sex, cid 
        
          
            from
          
           student 
        
          
            where
          
           name = 
          
            'tony'
          
          ;
        
        
          --多條記錄同時插入
        
          insert 
          
            into
          
           student
        
          
            select
          
          
            'jack'
          
          , 23, 1, 5 
          
            union
          
        
          
            select
          
          
            'tom'
          
          , 24, 0, 3 
          
            union
          
        
          
            select
          
          
            'wendy'
          
          , 25, 1, 3 
          
            union
          
        
          
            select
          
          
            'tony'
          
          , 26, 0, 5;
        

?

? 查詢、修改、刪除數(shù)據(jù)

          --查詢數(shù)據(jù)
        
          
            select
          
           * 
          
            from
          
           classes;
        
          
            select
          
           * 
          
            from
          
           student;
        
          
            select
          
           id, 
          
            'bulise'
          
           name, age, sex, cid 
          
            from
          
           student 
        
          
            where
          
           name = 
          
            'tony'
          
          ;
        
          
            select
          
           *, (
          
            select
          
          
            max
          
          (age) 
          
            from
          
           student) 
          
            from
          
           student 
        
          
            where
          
           name = 
          
            'tony'
          
          ;
        
          ?
        
          --修改數(shù)據(jù)
        
          
            update
          
           student 
          
            set
          
           name = 
          
            'hoho'
          
          , sex = 1 
          
            where
          
           id = 1;
        
          ?
        
          --刪除數(shù)據(jù)(from可省略)
        
          
            delete
          
          
            from
          
           student 
          
            where
          
           id = 1;
        

?

? 備份數(shù)據(jù)、表

          --備份、復(fù)制student表到stu
        
          
            select
          
           * 
          
            into
          
           stu 
          
            from
          
           student;
        
          
            select
          
           * 
          
            into
          
           stu1 
          
            from
          
           (
          
            select
          
           * 
          
            from
          
           stu) t;
        
          
            select
          
           * 
          
            from
          
           stu;
        
          
            select
          
           * 
          
            from
          
           stu1;
        

?

? 利用存儲過程查詢表信息

          --查詢student相關(guān)信息
        
          
            exec
          
           sp_help student;
        
          
            exec
          
           sp_help classes;
        

轉(zhuǎn):SQL Server 數(shù)據(jù)庫基礎(chǔ)編程


更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯(lián)系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發(fā)表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 久草国产在线观看 | 国产丝袜在线 | 久久99深爱久久99精品 | 日韩一区二区三区在线 | 精品国产九九 | 日韩一区二区三区精品 | 999精品视频 | 日韩欧美在线观看一区 | 精品在线一区二区 | 成人av一区二区三区 | 精品欧美一区二区在线看片 | 久久久99国产精品免费 | 国内精品一区二区三区最新 | 色开心 | 亚洲精品国偷拍自产在线观看 | 成人精品一区久久久久 | 99re久久资源最新地址 | 日日夜夜婷婷 | 久久草视频 | 26uuu在线观看| 色婷婷国产 | 韩漫重考生漫画画免费读漫画下拉式土豪漫 | 日本粉嫩一区二区三区视频 | av在线播放免费 | 成人天堂网 | 亚洲天堂av在线 | 男女午夜| 日本高清不卡一区久久精品 | 国产精品视频久久 | 久久久国产精品免费观看 | 国产一区二区久久久 | 亚洲综合国产精品 | 99国产在线 | 久久精品一区二区三区四区 | 一级毛片特级毛片免费的 | 夭天曰天天躁天天摸在线观看 | 日韩欧美二区 | 又大又紧又硬又湿a视频 | 91精品国产免费久久久久久 | 免费的色网站 | 免费国产一区 |