MySQL資料庫操作基本知識(1/4)

來源:互聯網
上載者:User

1.建立資料庫.

    在MySQL中應用 create database 語句建立資料庫.格式如下:

            create database db_name;    db_name 為資料庫名,必須為合法名稱.規定如下:

      a.不能與其他資料庫重名.

      b.名字可以是任意的字母,阿拉伯數字,底線或者"$"組成.可以使用以上任意的字元開頭,但不能是使用單獨的數字,哪樣會造成與數字混淆.

      c.名字最長可由64個字元組成(包括表,列和索引),而別名最多可長達256個字元.

      d.不能使用MySQL關鍵字作為資料庫名稱和表名.

  ps: 執行過程:在建立資料庫時,首先串連MySQL伺服器,使用者名稱是root,密碼也是root,然後編寫"create database db_name;" sql語句,資料庫建立成功.

   2.選擇資料庫.

    use 語句用於選擇一個資料庫,使其成為當前預設資料庫.格式如下:      

      use db_name;    

  3.刪除資料庫.

    刪除資料庫使用drop database語句.格式如下:

      drop database db_name    ps.對於刪除資料庫的操作應該謹慎使用.一旦刪除資料庫中的所有結構和資料都將會被刪除,沒有恢複的可能,除非資料庫中有備份.

<MySQL 資料庫表>

  上面將完資料庫的操作,下面講一下資料庫中,對錶的操作.MySQL 資料庫表的操作包括 建立,查看,修改,重新命名和刪除.

    1.建立表.

      建立表使用create table 語句.格式如下:

       create [temporary] table [if not exists] 資料表名 [(create_definition,...)][table_options][select_statement]        a. temporary ,如果使用該關鍵字,表示建立一個暫存資料表.

        b. if not exists ,該關鍵字用於避免建立表時,表存在時 MySQL報告的錯誤.

        c. create_definition,這是表的列表屬性部分.MySQL要求在建立表時,表至少要包含一列.

          create_deifnition 格式如下:

           

 代碼如下 複製代碼

   col_name type [not null] [default default_value] [auto_increment] [primary key] [reference_definition]

              col_name:欄位名. type:欄位類型. not null|null:指出該列是否允許空值.not null 表示不允許空值.default default_value:表示預設值.

              auto_increament:表示預設值.primary key:表示是否為主鍵,一個表中只能有一個primary key.如果沒有primary key ,而某些應用要求primary key,

              伺服器將返回第一個沒有null 列的unique 鍵作為primary key.

        d. table_option, 表的一些特性參數.

        e. select_statement,select語句描述部分,用它可以快速地建立表.

   2.查看錶.

    對於建立成功的表,使用show columns 語句或describe 語句查看指定資料表的表結構.格式如下:

     show columns 語句:

      show [full] columns from 資料表名 [from 資料庫名];
                    或者
      show [full] columns from 資料表名.資料庫名;    describe  語句,describe 可以簡寫成DESC.

      describe 資料表名;           或者      describe 資料表名 列名;  

首頁 1 2 3 4 末頁

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.