1.数据库的基本操作
-
显示当前的数据库
show databases;
![](https://img-blog.csdnimg.cn/direct/fd76c356a3f9453f98f34f6be400615e.png)
-
创建一个数据库
-
直接创建数据库
javacreate database 数据库名字;
-
如果系统没有 test2 的数据库,则创建一个名叫 test2 的数据库,如果有则不创建
create database if not exists test2;
-
如果系统没有 db_test 的数据库,则创建一个使用utf8mb4字符集的 db_test 数据库,如果有则
不创建
CREATE DATABASE IF NOT EXISTS db_test CHARACTER SET utf8mb4;
-
-
使用数据库
user 数据库名;
![](https://img-blog.csdnimg.cn/direct/9186bc4cdca0411397d4508eb99c9f8c.png)
-
删除数据库
drop database if exists 数据库名;
![](https://img-blog.csdnimg.cn/direct/74d19487839f492a8f0a0c175c67caba.png)
2.表的操作
-
需要操作数据库中的表时,需要先使用该数据库:
use 数据库名;
![](https://img-blog.csdnimg.cn/direct/e78e4ad3d45241c490e3b22b89a23d99.png)
-
创建表
create table table_name (
field1 datatype,
field2 datatype,
field3 datatype
);
//添加comment使用字段说明(相当于注释)
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 '供应商名称'
);
![](https://img-blog.csdnimg.cn/direct/9140e062286a4384894d5c074e982f92.png)
-
查看表结构
desc table_name;
![](https://img-blog.csdnimg.cn/direct/9fd23b5017574219be0bed511be682a4.png)
-
查寻表中的信息
select * from table_name;
![](https://img-blog.csdnimg.cn/direct/3326ebbbe70d485fbd593418c1bc2f81.png)
ps:此时还没有插入数据,所以表是空的
-
删除表
drop table if exists table_name;