Linux下mysql基本操作小結

來源:互聯網
上載者:User

標籤:

shell> mysql -uroot -p       //以root使用者串連mysql,預設密碼為空白

注意:mysql的大部分命令都需以";"(分號)結束;對大小寫不敏感!

mysql> show databases;    //顯示資料庫列表
mysql> use 資料庫名          //選中指定的資料庫
mysql> show tables;           //顯示選中資料庫中的資料表列表
mysql> select * from 表名;  //顯示指定資料表的內容
mysql> create database 資料庫名;     //建立資料庫
mysql> create table user_info(           //建立資料表
    -> id int(6) not null auto_increment,
    -> username varchar(20) not null, 
    -> password varchar(20) not null,
    -> email varchar(50) not null,
    -> key id (id));
因為使用了auto_increment (自動成長),所以必須指定key id。

mysql> describe 表名;                //顯示表的結構
mysql> insert into user_info values    //插入資料到表中
    -> (‘1‘, ‘aa‘, ‘11‘, ‘[email protected]‘),
    -> (‘2‘, ‘bb‘, ‘22‘, ‘[email protected]‘), 
    -> (‘3‘, ‘cc‘, ‘33‘, ‘[email protected]‘); 
插入資料有兩種寫法,上面是第一種,這種寫法需要給出每一列的值!

第二種:
mysql> insert into user_info(username, password, email) values    //插入資料到表中
    -> (‘aa‘, ‘11‘, ‘[email protected]‘),
    -> (‘bb‘, ‘22‘, ‘[email protected]‘), 
    -> (‘cc‘, ‘33‘, ‘[email protected]‘); 
因為第一個id被設定為auto_increment了,所以系統會自動添加!如果這裡也像第一種省略了列條目,則就會提示列數不匹配的錯誤;

mysql> drop database 資料庫名;    //刪除資料庫
mysql> drop table 表名;                  //刪除資料表
mysql> delete from 表名;                //清空資料表

mysql> grant select,insert,delete,update       //為資料庫指定一個專門的使用者進行管理
    -> on 資料庫名.* to [email protected]
    -> identified by ‘password‘;
該命令用於建立使用者並指定許可權,注意username和password不能太簡單,否則建立不成功!
mysql> mysql -uusername -p    //然後輸入999使用者的密碼後就會以999使用者進行管理mysql了

mysql> alter table 表名 add ‘列名‘ VARCHAR(12);       //在表中增加一列
mysql> alter table 表名 change ‘列名1‘ ‘列名2‘ VARCHAR(12);        //更改列名
mysql> alter table 表名 drop ‘列名‘;       //刪除一列
mysql> alter table 表名 rename t2;        //修改表名

mysql> drop database if exists school;        //如果存在SCHOOL則刪除 
mysql> load data local infile "file" into table 表名;    //文本資料應符合的格式:欄位資料之間用tab鍵隔開,null值用\n來代替.

備份資料庫:
匯出:mysqldump -u使用者名稱 -p密碼 資料庫名 表名 > out.sql
匯入:mysql -u使用者名稱 -p密碼 --database=資料庫名 < out.sql 

備份資料表:    
匯出:mysqldump -uroot test table1 > ./out.sql
匯入:mysql -uroot --database=test < ./out.sql

列資料類型:分為數字型、字串型和日期
1、數字型:以字串的形式儲存,decima/numeric;分為整形和浮點型,可以有兩個選項:unsigned(無符號,只儲存正數)和zerofill(用0填充而不是空格)

  • 小數型:float/double
  • 整數型:int bigint tinyint

範例:create table a (id int zerofill);

2、字串型
char varchar等

3、日期型
datetime timestamp等

4、枚舉型
enum(‘男‘,‘女‘)

Linux下mysql基本操作小結

聯繫我們

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