注意:MySQL數據庫命令不區分大小寫。但在MAC的終端,若是你想使用tab自動補全命令,那麼你就必須使用大寫,這樣MAC的終端纔會幫你補全命令,不然你按N遍tab都不會有響應。html
一、數據庫(database)管理mysql
1.一、create建立數據庫sql
create database firstDB;
1.二、show查看全部數據庫數據庫
mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | firstDB | | mysql | | performance_schema | +--------------------+ 4 rows in set (0.00 sec)
1.三、alter修改數據庫編碼
alter命令修改數據庫編碼:code
默認建立的數據庫默認不支持中文字符,若是咱們須要它支持中文字符,則將它的編碼設置爲utf8格式:orm
mysql> ALTER DATABASE testDB CHARACTER SET UTF8; Query OK, 1 row affected (0.00 sec)
1.四、use使用數據庫htm
mysql> use firstDB; Database changed
1.五、查看當前使用的數據庫blog
mysql> select database(); +------------+ | database() | +------------+ | firstdb | +------------+ 1 row in set (0.00 sec)
1.六、drop刪除數據庫排序
mysql> drop database firstDB; Query OK, 0 rows affected (0.00 sec)
二、數據表(table)管理
咱們首先建立一個數據庫,提供咱們日後的使用:
mysql> create database testDB; Query OK, 1 row affected (0.00 sec)
建立後記得用use命令進入(使用)數據庫,否則後面的操做都會不成功的。
2.一、create建立表
mysql> create table PEOPLE ( -> ID int AUTO_INCREMENT PRIMARY KEY, -> NAME varchar(20) not null, -> AGE int not null, -> BIRTHDAY datetime); Query OK, 0 rows affected (0.01 sec)
2.二、show顯示錶
顯示當前數據庫全部的數據表
mysql> show tables; +------------------+ | Tables_in_testdb | +------------------+ | PEOPLE | +------------------+ 1 row in set (0.00 sec)
2.三、desc查看錶結構
mysql> desc PEOPLE -> ; +----------+-------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +----------+-------------+------+-----+---------+----------------+ | ID | int(11) | NO | PRI | NULL | auto_increment | | NAME | varchar(20) | NO | | NULL | | | AGE | int(11) | NO | | NULL | | | BIRTHDAY | datetime | YES | | NULL | | +----------+-------------+------+-----+---------+----------------+ 4 rows in set (0.01 sec)
2.四、alter修改表結構(增、刪、改)
默認建立的表不支持中文字符,因此需將表編碼設置爲utf8:
mysql> ALTER TABLE KEYCHAIN CONVERT TO CHARACTER SET UTF8; Query OK, 1 row affected (0.02 sec) Records: 1 Duplicates: 0 Warnings: 0
2.4.一、insert在表中添加列(字段)
mysql> alter table PEOPLE add star BOOL; Query OK, 0 rows affected (0.02 sec) Records: 0 Duplicates: 0 Warnings: 0
提示:在MySQL裏,布爾類型會自動轉換爲tinyint(1)類型。
咱們不妨使用desc去查看一下PEOPLE表結構:
mysql> desc PEOPLE; +----------+-------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +----------+-------------+------+-----+---------+----------------+ | ID | int(11) | NO | PRI | NULL | auto_increment | | NAME | varchar(20) | NO | | NULL | | | AGE | int(11) | NO | | NULL | | | BIRTHDAY | datetime | YES | | NULL | | | star | tinyint(1) | YES | | NULL | | +----------+-------------+------+-----+---------+----------------+ 5 rows in set (0.00 sec)
2.4.二、alter修改表(列)字段
mysql> alter table PEOPLE MODIFY star int; Query OK, 0 rows affected (0.01 sec) Records: 0 Duplicates: 0 Warnings: 0
也能夠指定int(n)的長度,好比int(2)。
咱們再次使用desc查看PEOPLE表結構:
mysql> desc PEOPLE; +----------+-------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +----------+-------------+------+-----+---------+----------------+ | ID | int(11) | NO | PRI | NULL | auto_increment | | NAME | varchar(20) | NO | | NULL | | | AGE | int(11) | NO | | NULL | | | BIRTHDAY | datetime | YES | | NULL | | | star | int(11) | YES | | NULL | | +----------+-------------+------+-----+---------+----------------+ 5 rows in set (0.00 sec)
2.4.三、delete刪除表(列)字段
mysql> alter table PEOPLE DROP column star; Query OK, 0 rows affected (0.02 sec) Records: 0 Duplicates: 0 Warnings: 0
刪除後,再次查看PEOPLE表結構:
mysql> desc PEOPLE; +----------+-------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +----------+-------------+------+-----+---------+----------------+ | ID | int(11) | NO | PRI | NULL | auto_increment | | NAME | varchar(20) | NO | | NULL | | | AGE | int(11) | NO | | NULL | | | BIRTHDAY | datetime | YES | | NULL | | +----------+-------------+------+-----+---------+----------------+ 4 rows in set (0.00 sec)
刪除字段成功,如今咱們已經不能看到star的字段了。
2.4.四、rename重命名錶名
mysql> RENAME TABLE PEOPLE TO NEW_PEOPLE; Query OK, 0 rows affected (0.00 sec)
2.4.五、null or not null
修改表字段容許爲空或不容許爲空:
mysql> ALTER TABLE PEOPLE MODIFY AGE INT(3) NULL; Query OK, 0 rows affected (0.01 sec) Records: 0 Duplicates: 0 Warnings: 0
把PEOPLE表的AGE字段設置成「容許爲空」,即插入記錄時這個字段能夠不錄入。不然相反。
它的格式爲:ALTER TABLE <TALBE_NAME> MODIFY <CLOUMN> <NULL 'OR' NOT NULL>
2.五、create利用已有數據建立新表
mysql> create table newTable select * from PEOPLE; Query OK, 0 rows affected (0.01 sec) Records: 0 Duplicates: 0 Warnings: 0
咱們查看一下目前數據庫存在的表:
mysql> show tables; +------------------+ | Tables_in_testdb | +------------------+ | PEOPLE | | newTable | +------------------+ 2 rows in set (0.00 sec)
三、數據的操做及管理
數據表的基本操做,包含增、刪、改、查數據。
如下命令均在PEOPLE表上操做。
3.一、增長數據(增)
PEOPLE表目前是沒有數據的,它是空的數據表,咱們如今先添加一些數據。
insert into命令添加數據:
mysql> insert into PEOPLE VALUES (null, 'Anny', 22, '1992-05-22'); Query OK, 1 row affected (0.00 sec)
使用select命令查看錶(會在後面介紹),如今咱們查看PEOPLE數據表的數據:
mysql> select * from PEOPLE; +----+------+-----+---------------------+ | ID | NAME | AGE | BIRTHDAY | +----+------+-----+---------------------+ | 1 | Anny | 22 | 1992-05-22 00:00:00 | +----+------+-----+---------------------+ 1 row in set (0.00 sec)
數據表如今有一條數據。
咱們多添加幾條數據,如:
mysql> select * from PEOPLE; +----+--------+-----+---------------------+ | ID | NAME | AGE | BIRTHDAY | +----+--------+-----+---------------------+ | 1 | Anny | 22 | 1992-05-22 00:00:00 | | 2 | Garvey | 23 | 1991-05-22 00:00:00 | | 3 | Lisa | 25 | 1989-05-22 00:00:00 | | 4 | Nick | 24 | 1990-05-22 00:00:00 | | 5 | Rick | 24 | 1991-05-22 00:00:00 | +----+--------+-----+---------------------+ 5 rows in set (0.00 sec)
3.二、刪除數據(刪)
delete命令刪除數據:
mysql> delete from PEOPLE where name = 'Lisa'; Query OK, 1 row affected (0.01 sec)
再次查詢PEOPLE表:
mysql> select * from PEOPLE; +----+--------+-----+---------------------+ | ID | NAME | AGE | BIRTHDAY | +----+--------+-----+---------------------+ | 1 | Anny | 22 | 1992-05-22 00:00:00 | | 2 | Garvey | 23 | 1991-05-22 00:00:00 | | 4 | Nick | 24 | 1990-05-22 00:00:00 | | 5 | Rick | 24 | 1991-05-22 00:00:00 | +----+--------+-----+---------------------+ 4 rows in set (0.00 sec)
已經看不到名爲「Lisa」的數據了。
3.三、修改數據(改)
update命令修改數據:
mysql> update PEOPLE set name='Calvin' where name = 'Garvey'; Query OK, 1 row affected (0.00 sec) Rows matched: 1 Changed: 1 Warnings: 0
查詢PEOPLE表內容:
mysql> select * from PEOPLE; +----+--------+-----+---------------------+ | ID | NAME | AGE | BIRTHDAY | +----+--------+-----+---------------------+ | 1 | Anny | 22 | 1992-05-22 00:00:00 | | 2 | Calvin | 23 | 1991-05-22 00:00:00 | | 4 | Nick | 24 | 1990-05-22 00:00:00 | | 5 | Rick | 24 | 1991-05-22 00:00:00 | +----+--------+-----+---------------------+ 4 rows in set (0.00 sec)
名爲「Garvey」的記錄已經修改成「Calvin」。
3.4 查詢數據(查)
select命令查詢數據,最簡單的就是查詢表的全部數據,也就是咱們最初使用到的那條命令:
mysql> select * from PEOPLE; +----+--------+-----+---------------------+ | ID | NAME | AGE | BIRTHDAY | +----+--------+-----+---------------------+ | 1 | Anny | 22 | 1992-05-22 00:00:00 | | 2 | Calvin | 23 | 1991-05-22 00:00:00 | | 4 | Nick | 24 | 1990-05-22 00:00:00 | | 5 | Rick | 24 | 1991-05-22 00:00:00 | +----+--------+-----+---------------------+ 4 rows in set (0.00 sec)
格式:select * from <表名>,*表明全部字段。
查詢數據時也可指定顯示的(列)字段:
mysql> select NAME, AGE, BIRTHDAY from PEOPLE; +--------+-----+---------------------+ | NAME | AGE | BIRTHDAY | +--------+-----+---------------------+ | Anny | 22 | 1992-05-22 00:00:00 | | Calvin | 23 | 1991-05-22 00:00:00 | | Nick | 24 | 1990-05-22 00:00:00 | | Rick | 24 | 1991-05-22 00:00:00 | +--------+-----+---------------------+ 4 rows in set (0.00 sec)
格式:select <字段名,字段名,...> from <表名>。
select查詢命令還有不少的高級用法,好比用來查找不重複(distinct)的數據,使數據按條件排序(order by),按查詢條件顯示數據(where)等等。這些都會在下一篇文章做重點介紹,請你們繼續留意個人博客,謝謝。
四、管理視圖
建立視圖
視圖是從數據庫裏導出一個或多個表的虛擬表,是用來方便用戶對數據的操做。
mysql> CREATE VIEW PEOPLE_VIEW ( -> NAME, AGE) -> AS SELECT NAME, AGE FROM PEOPLE;
建立成功後查看視圖。
PEOPLE PEOPLE.AGE PEOPLE.BIRTHDAY PEOPLE.ID PEOPLE.NAME mysql> SELECT * FROM PEOPLE_VIEW -> ; +--------+-----+ | NAME | AGE | +--------+-----+ | Anny | 22 | | Calvin | 23 | | Nick | 24 | | Rick | 24 | +--------+-----+ 4 rows in set (0.00 sec)
咱們也可使用DESC命令查看視圖的結構。
mysql> DESC PEOPLE_VIEW; +-------+---------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+---------+------+-----+---------+-------+ | ID | int(11) | NO | | 0 | | +-------+---------+------+-----+---------+-------+ 1 row in set (0.01 sec)
替換視圖
建立或替換原有視圖。
mysql> CREATE OR REPLACE VIEW PEOPLE_VIEW(PEOPLE_ID,PEOPLE_NAME,PEOPLE_AGE) AS SELECT ID,NAME,AGE FROM PEOPLE; Query OK, 0 rows affected (0.00 sec)
建立或替換後查看視圖。
mysql> SELECT * FROM PEOPLE_VIEW; +-----------+-------------+------------+ | PEOPLE_ID | PEOPLE_NAME | PEOPLE_AGE | +-----------+-------------+------------+ | 1 | Anny | 22 | | 2 | Calvin | 23 | | 4 | Nick | 24 | | 5 | Rick | 24 | +-----------+-------------+------------+ 4 rows in set (0.00 sec)
操做視圖
當視圖數據有變化時(增、刪、改),真實的表數據也會隨着改變。也就是說,對視圖的操做就是對錶的數據,因此咱們能夠把視圖看成表。
例:往視圖插入一條數據。
mysql> INSERT INTO PEOPLE_VIEW VALUES(NULL, 'Kerry', '33'); Query OK, 1 row affected (0.00 sec)
插入數據成功後查看視圖。
mysql> SELECT * FROM PEOPLE_VIEW ; +-----------+-------------+------------+ | PEOPLE_ID | PEOPLE_NAME | PEOPLE_AGE | +-----------+-------------+------------+ | 1 | Anny | 22 | | 2 | Calvin | 23 | | 4 | Nick | 24 | | 5 | Rick | 24 | | 6 | Kerry | 33 | +-----------+-------------+------------+ 5 rows in set (0.00 sec)
能夠在視圖上看到咱們剛剛插入的數據,如今咱們就來驗證一下真實的表是否也會做出變化。
mysql> SELECT * FROM PEOPLE; +----+--------+-----+---------------------+ | ID | NAME | AGE | BIRTHDAY | +----+--------+-----+---------------------+ | 1 | Anny | 22 | 1992-05-22 00:00:00 | | 2 | Calvin | 23 | 1991-05-22 00:00:00 | | 4 | Nick | 24 | 1990-05-22 00:00:00 | | 5 | Rick | 24 | 1991-05-22 00:00:00 | | 6 | Kerry | 33 | NULL | +----+--------+-----+---------------------+ 5 rows in set (0.00 sec)
可見,真實的表數據也已經有所改變,剛剛往視圖裏插入的那一條數據存在於真實表中,真理即是:對視圖的操做就是對錶的數據。
刪除視圖
mysql> DROP VIEW PEOPLE_VIEW; Query OK, 0 rows affected (0.00 sec)
參考: