一个例子



创建数据库

[ding @miao ~]$ ll /var/lib/mysql
total 28680
-rw-rw---- 1 mysql mysql 18874368 Jun 26 21:57 ibdata1
-rw-rw---- 1 mysql mysql  5242880 Jun 26 21:57 ib_logfile0
-rw-rw---- 1 mysql mysql  5242880 Jun 23 12:12 ib_logfile1
drwx------ 2 mysql mysql     4096 Jun 23 12:12 mysql
srwxrwxrwx 1 mysql mysql        0 Jun 26 21:00 mysql.sock
drwx------ 2 mysql mysql     4096 Jun 23 12:12 performance_schema

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
+--------------------+
3 rows in set (0.00 sec)

mysql> create database test;
Query OK, 1 row affected (0.00 sec)

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| test               |
+--------------------+
4 rows in set (0.00 sec)


[ding @miao ~]$ ll /var/lib/mysql
total 28684
-rw-rw---- 1 mysql mysql 18874368 Jun 26 21:50 ibdata1
-rw-rw---- 1 mysql mysql  5242880 Jun 26 21:50 ib_logfile0
-rw-rw---- 1 mysql mysql  5242880 Jun 23 12:12 ib_logfile1
drwx------ 2 mysql mysql     4096 Jun 23 12:12 mysql
srwxrwxrwx 1 mysql mysql        0 Jun 26 21:00 mysql.sock
drwx------ 2 mysql mysql     4096 Jun 23 12:12 performance_schema
drwx------ 2 mysql mysql     4096 Jun 26 21:31 test




创建表,增加数据,更新数据

mysql> use test;
Database changed
mysql> show tables;
Empty set (0.00 sec)

mysql> create table pet (
    -> name varchar(20),
    -> owner varchar(20),
    -> species varchar(20),
    -> sex char(1),
    -> birth date,
    -> death date );
Query OK, 0 rows affected (0.14 sec)

mysql> show tables;
+----------------+
| Tables_in_test |
+----------------+
| pet            |
+----------------+
1 row in set (0.00 sec)

mysql> describe pet;
+---------+-------------+------+-----+---------+-------+
| Field   | Type        | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+-------+
| name    | varchar(20) | YES  |     | NULL    |       |
| owner   | varchar(20) | YES  |     | NULL    |       |
| species | varchar(20) | YES  |     | NULL    |       |
| sex     | char(1)     | YES  |     | NULL    |       |
| birth   | date        | YES  |     | NULL    |       |
| death   | date        | YES  |     | NULL    |       |
+---------+-------------+------+-----+---------+-------+
6 rows in set (0.00 sec)


准备数据如下(存储目录: /home/ding/temp/pet.data ,注意,第六列为空,各列之间用 Tab 符隔开):
Fluffy    Harold    cat    f    1993-02-04   
Claws    Gwen    cat    m    1994-03-17   
Buffy    Harold    dog    f    1989-05-13   



mysql> select * from pet;
Empty set (0.00 sec)

mysql> load data local infile '/home/ding/temp/pet.data' into table pet;
Query OK, 3 rows affected, 3 warnings (0.09 sec)
Records: 3  Deleted: 0  Skipped: 0  Warnings: 3

mysql> select * from pet;
+--------+--------+---------+------+------------+------------+
| name   | owner  | species | sex  | birth      | death      |
+--------+--------+---------+------+------------+------------+
| Fluffy | Harold | cat     | f    | 1993-02-04 | 0000-00-00 |
| Claws  | Gwen   | cat     | m    | 1994-03-17 | NULL       |
| Buffy  | Harold | dog     | f    | 1989-05-13 | NULL       |
+--------+--------+---------+------+------------+------------+
3 rows in set (0.00 sec)


mysql> insert into pet values (
    -> 'Puffball',
    -> 'Diane',
    -> 'hamste',
    -> 'f',
    -> '1999-03-30',
    -> NULL );
Query OK, 1 row affected (0.05 sec)

mysql> select * from pet;
+----------+--------+---------+------+------------+------------+
| name     | owner  | species | sex  | birth      | death      |
+----------+--------+---------+------+------------+------------+
| Fluffy   | Harold | cat     | f    | 1993-02-04 | 0000-00-00 |
| Claws    | Gwen   | cat     | m    | 1994-03-17 | NULL       |
| Buffy    | Harold | dog     | f    | 1989-05-13 | NULL       |
| Puffball | Diane  | hamste  | f    | 1999-03-30 | NULL       |
+----------+--------+---------+------+------------+------------+
4 rows in set (0.00 sec)

mysql> insert into pet value (
    -> 'Slim',
    -> 'Benny',
    -> 'snake',
    -> 'm',
    -> '1996-04-29',
    -> NULL );
Query OK, 1 row affected (0.06 sec)

mysql> select * from pet;
+----------+--------+---------+------+------------+------------+
| name     | owner  | species | sex  | birth      | death      |
+----------+--------+---------+------+------------+------------+
| Fluffy   | Harold | cat     | f    | 1993-02-04 | 0000-00-00 |
| Claws    | Gwen   | cat     | m    | 1994-03-17 | NULL       |
| Buffy    | Harold | dog     | f    | 1989-05-13 | NULL       |
| Puffball | Diane  | hamste  | f    | 1999-03-30 | NULL       |
| Slim     | Benny  | snake   | m    | 1996-04-29 | NULL       |
+----------+--------+---------+------+------------+------------+
5 rows in set (0.00 sec)


mysql> update pet set death='1999-01-10' where name='Fluffy';
Query OK, 1 row affected (0.08 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> select * from pet;
+----------+--------+---------+------+------------+------------+
| name     | owner  | species | sex  | birth      | death      |
+----------+--------+---------+------+------------+------------+
| Fluffy   | Harold | cat     | f    | 1993-02-04 | 1999-01-10 |
| Claws    | Gwen   | cat     | m    | 1994-03-17 | NULL       |
| Buffy    | Harold | dog     | f    | 1989-05-13 | NULL       |
| Puffball | Diane  | hamste  | f    | 1999-03-30 | NULL       |
| Slim     | Benny  | snake   | m    | 1996-04-29 | NULL       |
+----------+--------+---------+------+------------+------------+
5 rows in set (0.00 sec)



删除数据,删除表

mysql> delete from pet;
Query OK, 5 rows affected (0.11 sec)

mysql> select * from pet;
Empty set (0.00 sec)


mysql> show tables;
+----------------+
| Tables_in_test |
+----------------+
| pet            |
+----------------+
1 row in set (0.00 sec)

mysql> drop table pet;
Query OK, 0 rows affected (0.08 sec)

mysql> show tables;
Empty set (0.00 sec)



删除数据库

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| test               |
+--------------------+
4 rows in set (0.00 sec)

[ding @miao ~]$ ll /var/lib/mysql
total 28684
-rw-rw---- 1 mysql mysql 18874368 Jun 26 21:57 ibdata1
-rw-rw---- 1 mysql mysql  5242880 Jun 26 21:57 ib_logfile0
-rw-rw---- 1 mysql mysql  5242880 Jun 23 12:12 ib_logfile1
drwx------ 2 mysql mysql     4096 Jun 23 12:12 mysql
srwxrwxrwx 1 mysql mysql        0 Jun 26 21:00 mysql.sock
drwx------ 2 mysql mysql     4096 Jun 23 12:12 performance_schema
drwx------ 2 mysql mysql     4096 Jun 26 21:57 test


mysql> drop database test;
Query OK, 0 rows affected (0.00 sec)

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
+--------------------+
3 rows in set (0.00 sec)

[ding @miao ~]$ ll /var/lib/mysql
total 28680
-rw-rw---- 1 mysql mysql 18874368 Jun 26 21:57 ibdata1
-rw-rw---- 1 mysql mysql  5242880 Jun 26 21:57 ib_logfile0
-rw-rw---- 1 mysql mysql  5242880 Jun 23 12:12 ib_logfile1
drwx------ 2 mysql mysql     4096 Jun 23 12:12 mysql
srwxrwxrwx 1 mysql mysql        0 Jun 26 21:00 mysql.sock
drwx------ 2 mysql mysql     4096 Jun 23 12:12 performance_schema

你可能感兴趣的:(一个例子)