mysql學習筆記1-表和字段的操作


創建表

在創建表之前要選擇數據庫,否則會暴錯(No database selected),

這裏有兩種方法

1-create table db_name.table_name(利用點符號來引用)

2-在使用前先生命在哪個數據庫創建 use db_name (例如在 test數據庫裏面就是 usetest)


創建表的語句:

          Create table tb_name [結構] [選項]

Exp:    

create table admin (

id  int,

name varchar(20),

pwd varchar(32)

) ;


表的命名規則:

表的命名規則和數據庫命名規則類似。(他會在數據庫的目錄下創建一個文件,但是不是一定的。)

表的查詢:

查看全部表 show tables;

查看包含關鍵詞的表 show tables like ‘%xxx%’;

                Exp: show tables like ‘%dd’ ;查詢以dd結尾的表名

查看錶的創建語句 :show create table tb_name ;

表的重命名rename table tb_name tonewname ; 可以支持一次性修改多個表;

 Exprename teble xss to new_xss , inject to new_inject;

表的重命名還支持跨數據庫,

Exprename teble xss to db.xss;//這樣在當前數據庫xss表會被刪除,在db數據庫會添加一個表

查看錶結構describe tb_name ; (describe 可以簡寫爲desc )

      Exp: desc xss;

刪除一個表:droptable [if exists] tb_name;

  Exp:droptable  if esiste test; //加上if exists就算表不存在也不會報錯。

修改表的字符屬性altertable tb_name character set utf8; //把字符集修改爲utf8編碼


字段的命名規則:字段命名規則和表的類似;

字段信息的修改: alerttable tb_name [add|drop|change|modify];

1-添加一個字段:alert table tb_name add new_colum type

Exp: alert tablenima add ano int;

2-刪除一個字段alter table tb_name drop colum

Exp: alter tablenima drop ano;

3-修改一個字段定義alter table tb_name modifycloum  type

Exp:alert tablenima modify item vharchar(33);

4-重命名一個列alter table tb_name changecloum new_colum type;

Exp: alter tablenima change id str_no varchar(30);






發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章