hi,欢迎访问本站!
当前位置: 首页数据库正文

mysql创建,显示,删除数据表的方法

墨初 数据库 8208阅读

mysql数据库中的表操作是非常重要的,一般的表操作都是通过sql命令来完成的,下面的是mysql数据表的操作,大家可以参考一下。

mysql创建新的数据数据表

命令:

# 进入数据库后创建新表
use 数据库;
create table newdata(
#表字段
);
# 在指定数据库创建新表
create table database.newdata(
#表字段
);
# database 是一个数据库

例1:

MariaDB [tool]> use db_4; #进入数据库
Database changed
MariaDB [db_4]> create table newdata(
    -> name varchar(50)
    -> );
Query OK, 0 rows affected (0.025 sec)

例2:

# 在指定的数据库中创建新表,不用进入库里面
MariaDB [db_4]> create table tool.newdatas(
    -> name varchar(50)
    -> );
Query OK, 0 rows affected (0.009 sec)

mysql显示数据表的方法

命令:

#当前库下的所有表
show tables; 
#指定库下的所有表
show tables from [数据库];
#显示搜索数据表
show tables like '%like'; # _表示匹配一个字符,%表示匹配n个字符
#显示数据表的创建命令
show create table [表名]

例:

MariaDB [db_4]> show tables;
+----------------+
| Tables_in_db_4 |
+----------------+
| newdata        |
+----------------+
1 row in set (0.002 sec)

例2:

MariaDB [db_4]> show tables from tool;
+----------------+
| Tables_in_tool |
+----------------+
| newdatas       |
| user2          |
+----------------+
2 rows in set (0.002 sec)

例3:

MariaDB [db_4]> show create table newdata;
+---------+-------------------------------------------------------------------------------------------------------------------------------+
| Table   | Create Table                                                                                                                  |
+---------+-------------------------------------------------------------------------------------------------------------------------------+
| newdata | CREATE TABLE `newdata` (
  `name` varchar(50) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci |
+---------+-------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.006 sec)

mysql查看数据表内容

命令:

desc 表名;
describe 表名;
# desc 与 describe 的作用是一样的
show columns from 表名;

例:

MariaDB [db_4]> desc newdata;
+-------+-------------+------+-----+---------+-------+
| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| name  | varchar(50) | YES  |     | NULL    |       |
+-------+-------------+------+-----+---------+-------+
1 row in set (0.005 sec)

例2:

MariaDB [db_4]> show columns from newdata;
+-------+-------------+------+-----+---------+-------+
| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| name  | varchar(50) | YES  |     | NULL    |       |
+-------+-------------+------+-----+---------+-------+
1 row in set (0.003 sec)

mysql修改表名与参数

命令:

#修改表名
rename table [表名] to [新表名];
#修改表选项
alter table [表名] [参数];

例:

MariaDB [db_4]> rename table newdata to newdatas;
Query OK, 0 rows affected (0.011 sec)

例2:

MariaDB [db_4]> alter table newdatas charset utf8;
Query OK, 0 rows affected (0.011 sec)
Records: 0  Duplicates: 0  Warnings: 0

以上就是数据库中表的创建,显示与修改的方法,大家可以参考一下。

标签:
声明:无特别说明,转载请标明本文来源!