sql server數據庫常用命令


創建數據庫:

   命令: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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM