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 | +-------+--------+-----------+------------+
修改和備份、批處理
有時我們要對數據庫表和數據庫進行修改和刪除,可以用如下方法實現:
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′; 現在來看看發生了什么:
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 | +----------+------+------------+-----------+--------+
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 給出的條件的一條記錄。再顯示一下結果:
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、數據庫的刪除:
mysql> drop database 數據庫名; 小心使用。
7、數據庫的備份:
退回到 DOS:
mysql> quit
d:\mysqlbin
使用如下命令對數據庫 abccs 進行備份:
mysqldump --opt abccs>abccs.dbb
abccs.dbb 就是你的數據庫 abccs 的備份文件。
8、用批處理方式使用 MySQL:
首先建立一個批處理文件 mytest.sql,內容如下:
use abccs; select * from mytable; select name,sex from mytable where name=′abccs′;