中文字幕在线观看,亚洲а∨天堂久久精品9966,亚洲成a人片在线观看你懂的,亚洲av成人片无码网站,亚洲国产精品无码久久久五月天

MySQL中多表操作和批處理詳細介紹

2018-06-11    來源:

容器云強勢上線!快速搭建集群,上萬Linux鏡像隨意使用

多表操作

在一個數(shù)據(jù)庫中,可能存在多個表,這些表都是相互關聯(lián)的。我們繼續(xù)使用前面的例子。前面建立的表中包含了員工的一些基本信息,如姓名、性別、出生日期、出生地。我們再創(chuàng)建一個表,該表用于描述員工所發(fā)表的文章,內(nèi)容包括作者姓名、文章標題、發(fā)表日期。

1、查看第一個表mytable的內(nèi)容

mysql> select * from mytable;

+----------+------+------------+-----------+

| name | sex | birth | birthaddr |

+----------+------+------------+-----------+

| abccs | f | 1977-07-07 | china |

| mary | f | 1978-12-12 | usa |

| tom | m | 1970-09-02 | usa |

+----------+------+------------+-----------+

2、創(chuàng)建第二個表title(包括作者、文章標題、發(fā)表日期)

mysql> create table title(writer varchar(20) not null,

-> title varchar(40) not null,

-> senddate date);

向該表中填加記錄,最后表的內(nèi)容如下:

mysql>

select * from title;

+--------+-------+------------+

| writer | title | senddate |

+--------+-------+------------+

| abccs | a1 | 2000-01-23 |

| mary | b1 | 1998-03-21 |

| abccs | a2 | 2000-12-04 |

| tom | c1 | 1992-05-16 |

| tom | c2 | 1999-12-12 |

+--------+-------+------------+

5 rows in set (0.00sec)

3、多表查詢

現(xiàn)在我們有了兩個表: mytable 和 title。利用這兩個表我們可以進行組合查詢: 例如我們要查詢作者abccs的姓名、性別、文章:

mysql> SELECT name,sex,title FROM mytable,title

-> WHERE name=writer AND name=′abccs′;

+-------+------+-------+

| name | sex | title |

+-------+------+-------+

| abccs | f | a1 |

| abccs | f | a2 |

+-------+------+-------+

上面例子中,由于作者姓名、性別、文章記錄在兩個不同表內(nèi),因此必須使用組合來進行查詢。必須要指定一個表中的記錄如何與其它表中的記錄進行匹配。

注意:如果第二個表title中的writer列也取名為name(與mytable表中的name列相同)而不是writer時,就必須用mytable.name和title.name表示,以示區(qū)別。

再舉一個例子,用于查詢文章a2的作者、出生地和出生日期:

mysql> select title,writer,birthaddr,birth from mytable,title

-> where mytable.name=title.writer and title=′a2′;

+-------+--------+-----------+------------+

| title | writer | birthaddr | birth |

+-------+--------+-----------+------------+

| a2 | abccs | china | 1977-07-07 |

+-------+--------+-----------+------------+

修改和備份、批處理

有時我們要對數(shù)據(jù)庫表和數(shù)據(jù)庫進行修改和刪除,可以用如下方法實現(xiàn):

1、增加一列:

如在前面例子中的mytable表中增加一列表示是否單身single:

mysql> alter table mytable add column single char(1);

2、修改記錄

將abccs的single記錄修改為“y”:

mysql> update mytable set single=′y′ where name=′abccs′;

現(xiàn)在來看看發(fā)生了什么:

mysql> select * from mytable;

+----------+------+------------+-----------+--------+

| name | sex | birth | birthaddr | single |

+----------+------+------------+-----------+--------+

| abccs | f | 1977-07-07 | china | y |

| mary | f | 1978-12-12 | usa | NULL |

| tom | m | 1970-09-02 | usa | NULL |

+----------+------+------------+-----------+--------+


3、增加記錄

前面已經(jīng)講過如何增加一條記錄,為便于查看,重復與此:

mysql> insert into mytable

-> values (′abc′,′f′,′1966-08-17′,′china′,′n′);

Query OK, 1 row affected (0.05 sec)

查看一下:

mysql> select * from mytable;

+----------+------+------------+-----------+--------+

| name | sex | birth | birthaddr | single |

+----------+------+------------+-----------+--------+

| abccs | f | 1977-07-07 | china | y |

| mary | f | 1978-12-12 | usa | NULL |

| tom | m | 1970-09-02 | usa | NULL |

| abc | f | 1966-08-17 | china | n |

+----------+------+------------+-----------+--------+

4、刪除記錄

用如下命令刪除表中的一條記錄:

mysql> delete from mytable where name=′abc′;

DELETE從表中刪除滿足由where給出的條件的一條記錄。

再顯示一下結(jié)果:

mysql> select * from mytable;

+----------+------+------------+-----------+--------+

| name | sex | birth | birthaddr | single |

+----------+------+------------+-----------+--------+

| abccs | f | 1977-07-07 | china | y |

| mary | f | 1978-12-12 | usa | NULL |

| tom | m | 1970-09-02 | usa | NULL |

+----------+------+------------+-----------+--------+

5、刪除表:

mysql> drop table ****(表1的名字),***表2的名字;

可以刪除一個或多個表,小心使用。

6、數(shù)據(jù)庫的刪除:

mysql> drop database 數(shù)據(jù)庫名;

小心使用。

7、數(shù)據(jù)庫的備份:

退回到DOS:

mysql> quit

d:mysqlbin

使用如下命令對數(shù)據(jù)庫abccs進行備份:

mysqldump --opt abccs>abccs.dbb

abccs.dbb就是你的數(shù)據(jù)庫abccs的備份文件。

8、用批處理方式使用MySQL:

首先建立一個批處理文件mytest.sql,內(nèi)容如下:

use abccs;

select * from mytable;

select name,sex from mytable where name=′abccs′;

在DOS下運行如下命令:

d:mysqlbin mysql < mytest.sql

在屏幕上會顯示執(zhí)行結(jié)果。

標簽: Mysql 數(shù)據(jù)庫

版權申明:本站文章部分自網(wǎng)絡,如有侵權,請聯(lián)系:west999com@outlook.com
特別注意:本站所有轉(zhuǎn)載文章言論不代表本站觀點!
本站所提供的圖片等素材,版權歸原作者所有,如需使用,請與原作者聯(lián)系。

上一篇:建設現(xiàn)代存儲系統(tǒng)也要強調(diào)TCO

下一篇:編寫MySQL數(shù)據(jù)庫的用戶認證系統(tǒng)實例