Mysql数据表的操作(一)

xiaoxiao2021-02-28  118

一、数据库及数据表的创建与删除

//创建数据库test2 mysql> create database test2; Query OK, 1 row affected //遍历显示所有的数据库 mysql> show databases; +——————–+ | Database | +——————–+ | information_schema | | book | | mysql | | performance_schema | | sys | | test | | test2 | +——————–+ 7 rows in set //删除test2数据库 mysql> drop database test2; Query OK, 0 rows affected

mysql> show databases; +——————–+ | Database | +——————–+ | information_schema | | book | | mysql | | performance_schema | | sys | | test | +——————–+ 6 rows in set //使用test数据库 mysql> use test; Database changed //在数据库下创建数据表 mysql> create table tb_emp2 ( id int(11), name varchar(25), deptid int(11), salary float ); Query OK, 0 rows affected //遍历显示这个数据库下所有的数据表 mysql> show tables; +—————-+ | Tables_in_test | +—————-+ | tb_emp1 | | tb_emp2 | +—————-+ 2 rows in set

二、对表的操作

1.查看表结构

mysql> desc tb_emp1; +——–+————-+——+—–+———+——-+ | Field | Type | Null | Key | Default | Extra | +——–+————-+——+—–+———+——-+ | id | int(11) | YES | | NULL | | | name | varchar(25) | YES | | NULL | | | deptid | int(11) | YES | | NULL | | | salary | float | YES | | NULL | | +——–+————-+——+—–+———+——-+ 4 rows in set

2.查看表详细结构

show create table tb_emp1或者show create table tb_emp1\G后者显示结果便于直观

mysql> show create table tb_emp1; +———+——————————————————————————————————————————————————————————————-+ | Table | Create Table | +———+——————————————————————————————————————————————————————————————-+ | tb_emp1 | CREATE TABLE tb_emp1 ( id int(11) DEFAULT NULL, name varchar(25) DEFAULT NULL, deptid int(11) DEFAULT NULL, salary float DEFAULT NULL ) ENGINE=InnoDB DEFAULT CHARSET=latin1 | +———+——————————————————————————————————————————————————————————————-+ 1 row in set

3.修改数据表名

mysql> alter table tb_emp1 rename tb_emp3; Query OK, 0 rows affected

mysql> show tables; +—————-+ | Tables_in_test | +—————-+ | tb_emp2 | | tb_emp3 | +—————-+ 2 rows in set

4.修改字段数据类型

mysql> desc tb_emp3; +——–+————-+——+—–+———+——-+ | Field | Type | Null | Key | Default | Extra | +——–+————-+——+—–+———+——-+ | id | int(11) | YES | | NULL | | | name | varchar(30) | YES | | NULL | | | deptid | int(11) | YES | | NULL | | | salary | float | YES | | NULL | | +——–+————-+——+—–+———+——-+ 4 rows in set

5.修改字段名

mysql> alter table tb_emp3 change deptid loc varchar(50); Query OK, 0 rows affected Records: 0 Duplicates: 0 Warnings: 0

6.添加字段

mysql> alter table tb_emp3 add managerID int(10); Query OK, 0 rows affected Records: 0 Duplicates: 0 Warnings: 0

给表的第一列添加一个字段

mysql> alter table tb_emp3 add column2 int(11) first;

Query OK, 0 rows affected Records: 0 Duplicates: 0 Warnings: 0

给表的指定行添加字段

mysql> alter table tb_emp3 add column3 int (20) after name; Query OK, 0 rows affected Records: 0 Duplicates: 0 Warnings: 0

删除字段

mysql> alter table tb_emp3 drop name; Query OK, 0 rows affected Records: 0 Duplicates: 0 Warnings: 0

修改字段在表中的排列位置

mysql> alter table tb_emp3 modify column3 int(20) first;

Query OK, 0 rows affected Records: 0 Duplicates: 0 Warnings: 0

修改字段到指定列之后

mysql> alter table tb_emp3 modify column3 int(20) after salary;

Query OK, 0 rows affected Records: 0 Duplicates: 0 Warnings: 0

7.删除数据表

删除没有被关联的表

mysql> drop table if exists tb_emp3; Query OK, 0 rows affected

转载请注明原文地址: https://www.6miu.com/read-64285.html

最新回复(0)