sql server資料庫常用命令

來源:互聯網
上載者:User

標籤:模式   sage   進階   相同   column   刪除資料庫   資料類型   desc   分組   

建立資料庫:

   命令:create database 資料庫名;

  樣本:create database student;

刪除資料庫:

  命令:drop database 資料庫名;

  樣本:drop database  student;

建立表格:

  命令:create table 表名

              (列名  資料類型,列名2.....)

  樣本:create table student

             (sname  char(20),sid  int)

刪除表格:

  命令:drop table 表名

  樣本:drop table student

修改表結構:

    (插入(新增)列)

    命令:alter table 表名

                 add 新列名  資料類型

    樣本:alter table student

                 add  sage  int

    (刪除列)

    命令:alter table 表名

                   drop column 列名

    樣本:alter table student

                   drop column sid

    (修改列類型)

    命令:alter table 表名

                  alter  column 列名  資料類型

    樣本:alter table student

                  alter  column  sid  float(浮點型)

  (新增約束)

     命令:alter table 表名

                  alter column 列名  新資料類型

     樣本:alter table student

                   alter column PK_sid  primary  key(sid)(新增的約束類型是主鍵約束)

  (刪除約束)

    命令:alter table 表名

                  drop  列名

    樣本:alter table student

                  drop PK_sid

查詢表內容:

  命令:select  要查詢的資料列名

              from 表名

                where  篩選條件(無法對分組後的資料進行篩選)

(進階搜尋)【group  by 列名(分組)

                         having  篩選條件(只能對分組後的資料進行篩選)

                            order by  排序方式(控制資料最後輸出的相片順序有正序:asc、倒敘:desc)】

  樣本:select  sid

             from student

               where  sid=2

                    【group by sid

                              having  sid=1

                                   order by desc】

在表中插入資料:(值與列必須一一對應)

 命令:insert  into  表名

                (列名 ,列名)

            values

                (值,值)

  樣本:insert  into  表名

                (sname,sid,sage)

            values

               (‘張三’,12,15)

修改表中資料值:

  命令:update from 表名

               set 列名=新值

  樣本:update from student

              set sname=‘李四‘

查詢模式:(批量插入多條資料)

  命令:insert into 表名(值的總數必須和列的總數相同)

                select  值,值,值  union all

                selevt  值,值,值

  樣本:insert  into  表名

               select  ‘張三‘,15,18

               select  ‘李四‘,16,19

視圖:

  命令:create view 視圖名

              as

             select 列

             from 表名

  樣本:create view students

              as

                 select sname

                     from student

sql server資料庫常用命令

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.