MySQL數(shù)據(jù)庫修改、備份和批處理

字號:

有時我們要對數(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 |
    +----------+------+------------+-----------+--------+
    3、刪除記錄
    用如下命令刪除表中的一條記錄:
    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 |
    +----------+------+------------+-----------+--------+
    4、刪除表:
    mysql> drop table ****(表1的名字),***表2的名字;
    可以刪除一個或多個表,小心使用。
    5、數(shù)據(jù)庫的刪除:
    mysql> drop database 數(shù)據(jù)庫名;
    小心使用。
    6、數(shù)據(jù)庫的備份:
    退回到DOS:
    mysql> quit
    d:mysqlbin
    使用如下命令對數(shù)據(jù)庫abccs進行備份:
    mysqldump --opt abccs>abccs.dbb
    abccs.dbb就是你的數(shù)據(jù)庫abccs的備份文件。
    7、用批處理方式使用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í)行結果。
    如果想看結果,而輸出結果很多,則可以用這樣的命令:
    mysql < mytest.sql | more
    我們還可以將結果輸出到一個文件中:
    mysql < mytest.sql > mytest.out