**
MySQL表的增删改查
**
1. CRUD
注释:在SQL中可以使用“–空格+描述”来表示注释说明
CRUD 即增加(Create)、查询(Retrieve)、更新(Update)、删除(Delete)四个单词的首字母缩写。
2.创建一个新的数据库
语法:create database 数据库名称;
create database if not exists 数据库名称;
mysql> create database apple;
Query OK, 1 row affected (0.00 sec)
显示所有数据库:
语法:show databases;
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| apple |
| java1 |
| java33 |
| mysql |
| performance_schema |
| sakila |
| sys |
| world |
+--------------------+
9 rows in set (0.00 sec)
显示已经创建成功一个新的数据库apple
查看当前在那个数据库底下:
使用语句:select database();
mysql> select database();
+------------+
| database() |
+------------+
| java33 |
+------------+
1 row in set (0.00 sec)
查询该数据库所有表
语法
show tables;
+------------------+
| Tables_in_java33 |
+------------------+
| class |
| course |
| goods |
| orange |
| score_table |
| student |
| studentc |
+------------------+
3. 新增(Create)
1、表的创建:
语法:create table 表名(
字段名1 类型,
字段名2 类型 comment ‘字段备注’
)
注:最后一个字段定义完后不需要再加“,”号否则报错
使用语句:desc 表名;查询表结构
mysql> create table if not exists goods
-> (
-> goods_id int comment '商品编号',
-> goods_name varchar(32) comment '商品名称',
-> unitprice int comment '单价,单位分',
-> category varchar(12) comment '商品分类',
-> provider varchar(64) comment '供应商名称'
-> );
Query OK, 0 rows affected (0.02 sec)
mysql> desc goods;
+------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+------------+-------------+------+-----+---------+-------+
| goods_id | int(11) | YES | | NULL | |
| goods_name | varchar(32) | YES | | NULL | |
| unitprice | int(11) | YES | | NULL | |
| category | varchar(12) | YES | | NULL | |
| provider | varchar(64) | YES | | NULL | |
+------------+-------------+------+-----+---------+-------+
5 rows in set (0.00 sec)
4.查询表的结构
- 使用语句:desc 表名;查询表结构
- 查询表的所有信息:
1.方法1 show create table 表名;
mysql>show create table class;
+-------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+-------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| class | CREATE TABLE `class` (
`id` int(11) NOT NULL AUTO_INCREMENT COMMENT '班级编号',
`classname` varchar(250) COLLATE utf8mb4_unicode_ci NOT NULL COMMENT '班级名称',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci |
2.方法2 show full columns form 表名;
mysql> show full columns from goods;
show full columns from class;
+-----------+--------------+--------------------+------+-----+---------+----------------+---------------------------------+----------+
| Field | Type | Collation | Null | Key | Default | Extra | Privileges | Comment |
+-----------+--------------+--------------------+------+-----+---------+----------------+---------------------------------+----------+
| id | int(11) | NULL | NO | PRI | NULL | auto_increment | select,insert,update,references | 班级编号 |
| classname | varchar(250) | utf8mb4_unicode_ci | NO | | NULL | | select,insert,update,references | 班级名称 |
+-----------+--------------+--------------------+------+-----+---------+----------------+---------------------------------+----------+
5.删除表
语法:drop table 表名;
drop table if exists 表名;
drop table if exists goods;
6.修改表结构
添加字段:
语法:alter table 表名 add column 列名 列类型 备注;
删除字段:
语法:alter table 表名 drop column 列名;
修改字段:
语法:语法:alter table 表名 change 原字段名 新字段名 类型;
修改表名:
alter table 旧表名 rename 新表名;
修改表编码格式:
语法:alter table convert to character set utf8mb4;