SHOW DATABASES;
CREATE DATABASE [IF NOT EXISTS] db_name [create_specification [,create_specification] ...]create_specification:[DEFAULT] CHARACTER SET charset_name[DEFAULT] COLLATE collation_name
- 大写的表示关键字
- [] 是可选项
- CHARACTER SET: 指定数据库采用的字符集
- COLLATE: 指定数据库字符集的校验规则
- 创建名为 db_test1 的数据库
CREATE DATABASE db_ test1;
说明:当我们创建数据库没有指定字符集和校验规则时,系统使用默认字符集:utf8,校验规则是:utf8_ general_ ci
-
如果系统没有 db_test2 的数据库,则创建一个名叫 db_test2 的数据库,如果有则不创建
CREATE DATABASE IF NOT EXISTS db_test2;
-
如果系统没有 db_test 的数据库,则创建一个使用 utf8mb4 字符集的 db_test 数据库,如果有则不创建
CREATE DATABASE IF NOT EXISTS db_test CHARACTER SET utf8mb4;
说明:MySQL的utf8编码不是真正的utf8,没有包含某些复杂的中文字符。MySQL真正的utf8是使用utf8mb4,建议大家都使用utf8mb4
use 数据库名 ;
DROP DATABASE [IF EXISTS] db_name;
- 数据库删除以后,内部看不到对应的数据库,里边的表和数据全部被删除
drop database if exists db_test1;drop database if exists db_test2;
数值类型可以指定为无符号(unsigned),表示不取负数。1字节(bytes)= 8bit。对于整型类型的范围:1. 有符号范围:-2^(类型字节数*8-1)到2^(类型字节数*8-1)-1,如int是4字节,就是-2^31到2^31-12. 无符号范围:0到2^(类型字节数*8)-1,如int就是2^32-1尽量不使用unsigned,对于int类型可能存放不下的数据,int unsigned同样可能存放不下,与其如此,还不如设计时,将int类型提升为bigint类型。
2.3 日期类型
3、表的操作
use db_test;
desc 表名 ;
3.2 创建表
CREATE TABLE table_name(field1 datatype,field2 datatype,field3 datatype);
create table stu_test(id int ,name varchar ( 20 ) comment ' 姓名 ' ,password varchar ( 50 ) comment ' 密码 ' ,age int ,sex varchar ( 1 ),birthday timestamp ,amout decimal ( 13 , 2 ),resume text);
DROP [TEMPORARY] TABLE [IF EXISTS] tbl_name [, tbl_name] ...
-- 删除 stu_test 表drop table stu_test;-- 如果存在 stu_test 表,则删除 stu_test 表drop table if exists stu_test;
- 操作数据库:
-- 显示show databases;-- 创建create database xxx;-- 使用use xxx;-- 删除drop database xxx;
- 常用数据类型:
- 操作表:
-- 查看show 表 ;-- 创建create table 表名 (字段 1 类型 1,字段 2 类型 2,...);-- 删除drop talbe 表名;
7、举例
- 有一个商店的数据,记录客户及购物情况,有以下三个表组成:
- SQL:
-- 创建数据库
create database if not exists bit32mall
default character set utf8 ;
-- 选择数据库
use bit32mall;
-- 创建数据库表
-- 商品
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 '供应商名称'
);
-- 客户
create table if not exists customer
(
customer_id int comment '客户编号',
name varchar(32) comment '客户姓名',
address varchar(256) comment '客户地址',
email varchar(64) comment '电子邮箱',
sex bit comment '性别',
card_id varchar(18) comment '身份证'
);
-- 购买
create table if not exists purchase
(
order_id int comment '订单号',
customer_id int comment '客户编号',
goods_id int comment '商品编号',
nums int comment '购买数量'
);