1. 程式人生 > 其它 >mysql基礎資料操作命令

mysql基礎資料操作命令

1:基礎命令

    1>連線命令

      1) 連線遠端mysql mysql -h host -P port -u 使用者名稱 -p密碼

      2)連線本地mysql mysql -u 使用者名稱 -p密碼

      3)-h連線的mysql 所在服務的IP -P 埠號

    2>檢視命令

      1)檢視所有資料庫 show databases;

      2)使用資料庫 use 資料庫;

      3)檢視資料庫所有表 show tables;

      4)檢視當前使用的資料庫 select database();

    3>操作資料庫

      1)建立資料庫

         (1)建立一個學生資料庫

           create database student charset=utf8;

      2)刪除資料庫

          (drop database 資料庫名)

    4>操作表

      1)建立表

        案例(1)建立一個學生表(主鍵id,學號,學生姓名,出生日期,性別)

           create table students(

              id int primary key auto_increment,

              sn char(8) unique not null,

              name varchar(20),

              birthday date,

              gender bool  

              );

      2)刪除表

         (1)完全刪除 drop table 表名;

         (2)刪除所有內容,保留表結構 truncate 表名

         (3)按照行來刪除記錄 delete

   5>資料表

      1)約束 primary key \ not null \ unique \ default \ enum \(指定固定欄位值)

 6>修改表

      1)給已存在的表新增新欄位

        (1)alter table 表名 add 列名 型別

         (2)案例 新建愛好欄位 alter table student ADD hobby varchar(30);

      2)給已存在的表修改某個欄位名型別

        (1)alter table 表名 change 原名 新名 型別及約束;

        (2)案例:修改gender 欄位為列舉型別 , alter table student change gender gender12 enum ('男','女');