MySQL---数据库基础

1、 数据库的操作
1.1 显示当前的数据库
SHOW DATABASES;
1.2 创建数据库
语法:
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
1.3 使用数据库
use 数据库名 ;
1.4 删除数据库
语法:
DROP DATABASE [IF EXISTS] db_name;
说明 :
  • 数据库删除以后,内部看不到对应的数据库,里边的表和数据全部被删除
drop database if exists db_test1;
drop database if exists db_test2;
2、 常用数据类型
2.1 数值类型:
分为整型和浮点型:

扩展资料
数值类型可以指定为无符号(unsigned),表示不取负数。
1字节(bytes)= 8bit。
对于整型类型的范围:
        1. 有符号范围:-2^(类型字节数*8-1)到2^(类型字节数*8-1)-1,如int是4字节,就
是-2^31到2^31-1
        2. 无符号范围:0到2^(类型字节数*8)-1,如int就是2^32-1
尽量不使用unsigned,对于int类型可能存放不下的数据,int unsigned同样可能存放不下,与其如此,还不如设计时,将int类型提升为bigint类型。
2.2 字符串类型

 2.3 日期类型

 3、表的操作

需要操作数据库中的表时,需要先使用该数据库:
use db_test;
3.1 查看表结构
desc 表名 ;

3.2 创建表

语法:
CREATE TABLE table_name
(
field1 datatype,
field2 datatype,
field3 datatype
);
可以使用 comment 增加字段说明。
示例:
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
);
3.4 删除表
语法格式:
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;
6、 内容重点总结
  • 操作数据库:
-- 显示
show databases;
-- 创建
create database xxx;
-- 使用
use xxx;
-- 删除
drop database xxx;
  • 常用数据类型:
        INT:整型
        DECIMAL(M, D):浮点数类型
        VARCHAR(SIZE):字符串类型
        TIMESTAMP:日期类型
  • 操作表:
-- 查看
show ;
-- 创建
create table 表名 (
字段 1 类型 1,
字段 2 类型 2,
...
);
-- 删除
drop talbe 表名;

7、举例

  • 有一个商店的数据,记录客户及购物情况,有以下三个表组成:
        商品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)
  • 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 '购买数量'
);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值