mysql --- 创建数据库

时间:2021-12-23 06:03:58

1、每创建一个数据库,在mysql的data文件夹里会创建一个目录,数据库名就是目录名

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

mysql> show databases;
 -------------------- 
| Database           |
 -------------------- 
| information_schema |
| mysql              |
| performance_schema |
| phone_num          |
| sys                |
 -------------------- 
5 rows in set (0.00 sec)

  查看data文件,有phone_num这个文件夹

 cd /usr/local/mysql/data
[[email protected] data]# ls
auto.cnf  ib_buffer_pool  ibdata1  ib_logfile0  ib_logfile1  ibtmp1  iz2zedr8770rctry7tq8t7z.pid  mysql  performance_schema  phone_num  sys

 

2、创建表

mysql> use phone_num
Database changed
mysql> create table phone_num_data
    -> (case_id INT(4) PRIMARY KEY,
    -> phone_num INT(11) );
Query OK, 0 rows affected (0.01 sec)

mysql> show tables;
 --------------------- 
| Tables_in_phone_num |
 --------------------- 
| phone_num_data      |
 --------------------- 
1 row in set (0.00 sec)

 

3、查看表结构

mysql> desc phone_num_data ;
 ----------- --------- ------ ----- --------- ------- 
| Field     | Type    | Null | Key | Default | Extra |
 ----------- --------- ------ ----- --------- ------- 
| case_id   | int(4)  | NO   | PRI | NULL    |       |
| phone_num | int(11) | YES  |     | NULL    |       |
 ----------- --------- ------ ----- --------- ------- 
2 rows in set (0.00 sec)

 

4、想更改数据库名,从1中可以知道,直接改文件名就可以啦

[[email protected] data]# ls
auto.cnf  ib_buffer_pool  ibdata1  ib_logfile0  ib_logfile1  ibtmp1  iz2zedr8770rctry7tq8t7z.pid  mysql  performance_schema  phone_num  sys
[[email protected] data]# mv phone_num test_cases
[[email protected] data]# ls
auto.cnf  ib_buffer_pool  ibdata1  ib_logfile0  ib_logfile1  ibtmp1  iz2zedr8770rctry7tq8t7z.pid  mysql  performance_schema  sys  test_cases

  改完啦,查询看看吧

mysql> show databases;
 -------------------- 
| Database           |
 -------------------- 
| information_schema |
| mysql              |
| performance_schema |
| sys                |
| test_cases         |
 -------------------- 
5 rows in set (0.00 sec)

  不要改了,改完就报错

mysql> desc phone_num_data;
ERROR 1146 (42S02): Table test_cases.phone_num_data doesnt exist

 

5、

6、