MySQL 创建、查看创建语句、删除数据库、表

刺骨的言语ヽ痛彻心扉 2022-03-19 06:46 514阅读 0赞

show databases 查看当前的数据库

use database_name 切换到数据库

show tables 查看当前数据中的表

CRATE DATABASE database_name; 创建数据库

CRATE TABLE <表名>

(

字段名1 数据类型 [列级别约束条件] [默认值],

字段名2 数据类型 [列级别约束条件] [默认值],

……

[表级别的约束条件]

);

show database database_name; 查看创建数据库语句

show table <表名>; 查看创建表语句

drop table <表名>; 删除该表【表中的数据一并删除】

drop database database_name; 删除该数据库【联通数据库中的其他表数据一并删除】

示例:

  1. 创建数据库
  2. mysql> CREATE DATABASE test_d;
  3. Query OK, 1 row affected (0.06 sec)
  4. 显示数据库
  5. mysql> show databases;
  6. +--------------------+
  7. | Database |
  8. +--------------------+
  9. | information_schema |
  10. | mysql |
  11. | performance_schema |
  12. | sakila |
  13. | sys |
  14. | test_d |
  15. | world |
  16. +--------------------+
  17. 7 rows in set (0.00 sec)
  18. 切换数据库
  19. mysql> use test_d
  20. Database changed
  21. 创建表 table1 设置 id 为主键
  22. mysql> CREATE TABLE table1
  23. -> (
  24. -> id int,
  25. -> name varchar(30),
  26. -> time date,
  27. -> primary key(id)
  28. -> );
  29. Query OK, 0 rows affected (0.20 sec)
  30. 查看当前数据中的表
  31. mysql> show tables;
  32. +------------------+
  33. | Tables_in_test_d |
  34. +------------------+
  35. | table1 |
  36. +------------------+
  37. 1 row in set (0.00 sec)
  38. 查看数据创建语句
  39. mysql> show create database test_d;
  40. +----------+-----------------------------------------------------------------------------------------------+
  41. | Database | Create Database |
  42. +----------+-----------------------------------------------------------------------------------------------+
  43. | test_d | CREATE DATABASE `test_d` /*!40100 DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci */ |
  44. +----------+-----------------------------------------------------------------------------------------------+
  45. 1 row in set (0.00 sec)
  46. 查看表创建语句
  47. mysql> show create table table1
  48. -> ;
  49. +--------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  50. | Table | Create Table |
  51. +--------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  52. | table1 | CREATE TABLE `table1` (
  53. `id` int(11) NOT NULL,
  54. `name` varchar(30) DEFAULT NULL,
  55. `time` date DEFAULT NULL,
  56. PRIMARY KEY (`id`)
  57. ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci |
  58. +--------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  59. 1 row in set (0.00 sec)
  60. 删除 table1
  61. mysql> drop table table1;
  62. Query OK, 0 rows affected (0.21 sec)
  63. 查看数据库中表数据
  64. mysql> show tables;
  65. Empty set (0.00 sec)
  66. 删除 test_d 数据库
  67. mysql> drop database test_d;
  68. Query OK, 0 rows affected (0.06 sec)
  69. 查看创建数据库语句
  70. mysql> show create database test_d;
  71. ERROR 1049 (42000): Unknown database 'test_d'
  72. 查看全部数据库
  73. mysql> show databases;
  74. +--------------------+
  75. | Database |
  76. +--------------------+
  77. | information_schema |
  78. | mysql |
  79. | performance_schema |
  80. | sakila |
  81. | sys |
  82. | world |
  83. +--------------------+
  84. 6 rows in set (0.00 sec)
  85. 退出
  86. mysql> exit
  87. Bye

发表评论

表情:
评论列表 (有 0 条评论,514人围观)

还没有评论,来说两句吧...

相关阅读