一.数据库的操作
1.显示当前的数据库
show databases;
2.创建数据库
2.1创建名为test的数据库
create database test;
2.2 当名为test的数据库不存在时,创建名为test的数据库
create database if not exists test;
2.3 当名为test的数据库不存在时,创建名为test的使用utf8mb4字符集的数据库
create database if not exists test charset utf8mb4;
注意:MySQL中的utf8是utf8的阉割版本,缺少emoji。utf8mb4,才是完整版的utf8。
3.选中数据库
使用名为test的数据库
use test;
4.删除数据库
drop databases test;
drop databases if exist test;
二.常用数据类型
1. 数值类型:
2. 字符串类型
3.日期类型
三.数据表的操作
针对数据表的操作,前提是选中该数据库
1.创建表
create table table_name(表名 类型, 表名 类型);
2.查看该数据库中的所有表
show tables;
3.查看指定表的结构
desc 表名;
4.删除表
drop table 表名;
四.例题
有一个商店的数据,记录客户及购物情况,有以下三个表组成:
商品goods(商品编号goods_id,商品名goods_name, 单价unitprice, 商品类别category, 供
应商provider)
客户customer(客户号customer_id,姓名name,住址address,邮箱email,性别sex,身份证
card_id)
购买purchase(购买订单号order_id,客户号customer_id,商品号goods_id,购买数量nums)