操作MySQL資料庫

來源:互聯網
上載者:User

標籤:

向表中插入資料

insert 語句可以用來將一行或多行資料插到資料庫表中, 使用的一般形式如下:

insert [into] 表名 [(列名1, 列名2, 列名3, ...)] values (值1, 值2, 值3, ...);

其中 [] 內的內容是可選的, 例如, 要給 samp_db 資料庫中的 students 表插入一條記錄, 執行語句:

insert into students values(NULL, "王剛", "男", 20, "13811371377");

按斷行符號鍵確認後若提示 Query Ok, 1 row affected (0.05 sec) 表示資料插入成功。 若插入失敗請檢查是否已選擇需要操作的資料庫。


有時我們只需要插入部分資料, 或者不按照列的順序進行插入, 可以使用這樣的形式進行插入:

insert into students (name, sex, age) values("孫麗華", "女", 21);

查詢表中的資料

select 語句常用來根據一定的查詢規則到資料庫中擷取資料, 其基本的用法為:

select 列名稱 from 表名稱 [查詢條件];

例如要查詢 students 表中所有學生的名字和年齡, 輸入語句 select name, age from students; 執行結果如下:

mysql> select name, age from students;+--------+-----+| name   | age |+--------+-----+| 王剛   |  20 || 孫麗華 |  21 || 王永恒 |  23 || 鄭俊傑 |  19 || 陳芳   |  22 || 張偉朋 |  21 |+--------+-----+6 rows in set (0.00 sec)mysql>

也可以使用萬用字元 * 查詢表中所有的內容, 語句: select * from students;

按特定條件查詢:

where 關鍵詞用於指定查詢條件, 用法形式為: select 列名稱 from 表名稱 where 條件;

以查詢所有性別為女的資訊為例, 輸入查詢語句: select * from students where sex="女";

where 子句不僅僅支援 "where 列名 = 值" 這種名等於值的查詢形式, 對一般的比較運算的運算子都是支援的, 例如 =、>、<、>=、<、!= 以及一些擴充運算子 is [not] null、in、like 等等。 還可以對查詢條件使用 or 和 and 進行組合查詢, 以後還會學到更加進階的條件查詢方式, 這裡不再多做介紹。

樣本:

查詢年齡在21歲以上的所有人資訊: select * from students where age > 21;

查詢名字中帶有 "王" 字的所有人資訊: select * from students where name like "%王%";

查詢id小於5且年齡大於20的所有人資訊: select * from students where id<5 and age>20;

更新表中的資料

update 語句可用來修改表中的資料, 基本的使用形式為:

update 表名稱 set 列名稱=新值 where 更新條件;

使用樣本:

將id為5的手機號改為預設的"-": update students set tel=default where id=5;

將所有人的年齡增加1: update students set age=age+1;

將手機號為 13288097888 的姓名改為 "張偉鵬", 年齡改為 19: update students set name="張偉鵬", age=19 where tel="13288097888";

刪除表中的資料

delete 語句用於刪除表中的資料, 基本用法為:

delete from 表名稱 where 刪除條件;

使用樣本:

刪除id為2的行: delete from students where id=2;

刪除所有年齡小於21歲的資料: delete from students where age<20;

刪除表中的所有資料: delete from students;

 

建立後表的修改

alter table 語句用於建立後對錶的修改, 基礎用法如下:

添加列

基本形式: alter table 表名 add 列名 列資料類型 [after 插入位置];

樣本:

在表的最後追加列 address: alter table students add address char(60);

在名為 age 的列後插入列 birthday: alter table students add birthday date after age;

修改列

基本形式: alter table 表名 change 列名稱 列新名稱 新資料類型;

樣本:

將表 tel 列改名為 telphone: alter table students change tel telphone char(13) default "-";

將 name 列的資料類型改為 char(16): alter table students change name name char(16) not null;

刪除列

基本形式: alter table 表名 drop 列名稱;

樣本:

刪除 birthday 列: alter table students drop birthday;

重新命名表

基本形式: alter table 表名 rename 新表名;

樣本:

重新命名 students 表為 workmates: alter table students rename workmates;

刪除整張表

基本形式: drop table 表名;

樣本: 刪除 workmates 表: drop table workmates;

刪除整個資料庫

基本形式: drop database 資料庫名;

樣本: 刪除 samp_db 資料庫: drop database samp_db;

 

附錄修改 root 使用者密碼

按照本文的安裝方式, root 使用者預設是沒有密碼的, 重設 root 密碼的方式也較多, 這裡僅介紹一種較常用的方式。

使用 mysqladmin 方式:

開啟命令提示字元介面, 執行命令: mysqladmin -u root -p password 新密碼

執行後提示輸入舊密碼完成密碼修改, 當舊密碼為空白時直接按斷行符號鍵確認即可。

可視化管理工具 MySQL Workbench

盡 管我們可以在命令提示字元下通過一行行的輸入或者通過重新導向檔案來執行mysql 語句, 但該方式效率較低, 由於沒有執行前的文法自動檢查, 輸入失誤造成的一些錯誤的可能性會大大增加, 這時不妨試試一些可視化的MySQL資料庫管理工具, MySQL Workbench 就是 MySQL 官方 為 MySQL 提供的一款可視化管理工具, 你可以在裡面通過可視化的方式直接管理資料庫中的內容, 並且 MySQL Workbench 的 SQL 指令碼編輯器支援文法高亮以及輸入時的語法檢查, 當然, 它的功能強大, 絕不僅限於這兩點。

MySQL Workbench官方介紹: http://www.mysql.com/products/workbench/

MySQL Workbench 下載頁: http://dev.mysql.com/downloads/tools/workbench/

操作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.