MySQL数据库案例(1)——创建商店数据库
创建一个商店的数据库,记录客户及购物情况
该篇主要分析一个简单案例: 思路如下:
- 创建一个商店的数据库,用来记录商品,客户,及购物情况;
- 该数据库下建立三个表:goods(商品信息表),customer(客户信息表),purchase(购物情况表);
- 商品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) 注意:purchase表中依靠 customer_id 和 goods_id 与customer 表和 goods 表建立联系。 此处使用外键。
//1,创建数据库store mysql> create database if not exists store charset=utf8; Query OK, 1 row affected (0.00 sec) //2,使用创建号数据库store mysql> use store; Database changed //3,创建goods表(商品信息表)——最好写备注 //3.1 goods_id(商品编号)设为主键 mysql> create table goods( -> goods_id int primary key comment 商品编号, -> goods_name varchar(30) not null comment 商品名称, -> unitprice int default 0 comment 商品单价, -> category varchar(10) comment 商品类别, -> provider varchar(60) not null comment 供应商 -> ); Query OK, 0 rows affected (0.04 sec) //3.2 查看goods表的结构 mysql> desc goods; +------------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +------------+-------------+------+-----+---------+-------+ | goods_id | int(11) | NO | PRI | NULL | | | goods_name | varchar(30) | NO | | NULL | | | unitprice | int(11) | YES | | 0 | | | category | varchar(10) | YES | | NULL | | | provider | varchar(60) | NO | | NULL | | +------------+-------------+------+-----+---------+-------+ 5 rows in set (0.01 sec) //4 创建customer表(客户信息表) //4.1 将customer_id(客户编号)设为主键 //4.2 要求客户姓名唯一,将customer_name 设为唯一键 //4.3 身份证号要求唯一,将cara_id 设为唯一键 mysql> create table customer( -> customer_id int primary key comment 客户编号, -> customer_name varchar(30) not null comment 客户姓名, -> address varchar(600) comment 地址, -> sex enum(男,女) not null comment 性别, -> card_id char(20) unique key comment 身份证号); Query OK, 0 rows affected (0.04 sec) //4.4 customer表创建成功,查看表的结构 mysql> desc customer; +---------------+-----------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +---------------+-----------------+------+-----+---------+-------+ | customer_id | int(11) | NO | PRI | NULL | | | customer_name | varchar(30) | NO | | NULL | | | address | varchar(600) | YES | | NULL | | | sex | enum(男,女) | NO | | NULL | | | card_id | char(20) | YES | UNI | NULL | | +---------------+-----------------+------+-----+---------+-------+ 5 rows in set (0.00 sec) //5. 创建购买purchase表(购买情况) //5.1 将订单号设为主键, //5.2 customer_id 与customer表建立联系,使用外键; //5.3 goods_id 与goods表建立联系,使用外键; mysql> create table purchase( -> order_id int primary key comment 订单号, -> customer_id int comment 客户编号, -> goods_id int comment 商品编号, -> foreign key(customer_id) references customer(customer_id), -> foreign key(goods_id) references goods(goods_id)); Query OK, 0 rows affected (0.04 sec) mysql> desc purchase; +-------------+---------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------------+---------+------+-----+---------+-------+ | order_id | int(11) | NO | PRI | NULL | | | customer_id | int(11) | YES | MUL | NULL | | | goods_id | int(11) | YES | MUL | NULL | | +-------------+---------+------+-----+---------+-------+ 3 rows in set (0.00 sec)
下一篇:
mysql忘记登录密码的解决方法