mysql 循环建库_【MySQL必知必会01】建库-建表-数据准备

本文介绍了如何使用MySQL创建数据库、表以及填充数据。详细步骤包括创建`crashcourse`数据库,建立`customers`、`orderitems`、`orders`、`products`、`vendors`和`productnotes`等表,并设置了外键约束。此外,还提供了`customers`、`vendors`和`products`等表的数据插入示例。
摘要由CSDN通过智能技术生成

在这里提供一下【MySQL必知必会】系列学习所需要的表、数据,以供大家学习参考。

创建数据库:

#######Create customers table#######

create database crashcourse;

创建表:

#######首先选择当前数据库#######

use crashcourse;

#######Create customers table#######

CREATE TABLE customers(

cust_id      int      NOT NULL AUTO_INCREMENT,

cust_name    char(50)  NOT NULL ,

cust_address char(50)  NULL ,

cust_city    char(50)  NULL ,

cust_state  char(5)  NULL ,

cust_zip    char(10)  NULL ,

cust_country char(50)  NULL ,

cust_contact char(50)  NULL ,

cust_email  char(255) NULL ,

PRIMARY KEY (cust_id)

) ENGINE=InnoDB;

#######Create orderitems table#######

CREATE TABLE orderitems(

order_num  int          NOT NULL ,

order_item int          NOT NULL ,

prod_id    char(10)    NOT NULL ,

quantity  int          NOT NULL ,

item_price decimal(8,2) NOT NULL ,

PRIMARY KEY (order_num, order_item)

) ENGINE=InnoDB;

#######Create orders table#######

CREATE TABLE orders(

order_num  int      NOT NULL AUTO_INCREMENT,

order_date datetime NOT NULL ,

cust_id    int      NOT NULL ,

PRIMARY KEY (order_num)

) ENGINE=InnoDB;

#######Create products  table#######

CREATE TABLE products(

prod_id    char(10)      NOT NULL,

vend_id    int          NOT NULL ,

prod_name  char(255)    NOT NULL ,

prod_price decimal(8,2)  NOT NULL ,

prod_desc  text          NULL ,

PRIMARY KEY(prod_id)

) ENGINE=InnoDB;

#######Create vendors table#######

CREATE TABLE vendors(

vend_id      int      NOT NULL AUTO_INCREMENT,

vend_name    char(50) NOT NULL ,

vend_address char(50) NULL ,

vend_city    char(50) NULL ,

vend_state  char(5)  NULL ,

vend_zip    char(10) NULL ,

vend_country char(50) NULL ,

PRIMARY KEY (vend_id)

) ENGINE=InnoDB;

#######Create productnotes table#######

CREATE TABLE productnotes(

note_id    int          NOT NULL AUTO_INCREMENT,

prod_id    char(10)      NOT NULL,

note_date datetime      NOT NULL,

note_text  text          NULL ,

PRIMARY KEY(note_id),

FULLTEXT(note_text)

) ENGINE=MyISAM;

#######Define foreign keys#######

ALTER TABLE orderitems ADD CONSTRAINT fk_orderitems_orders FOREIGN KEY (order_num) REFERENCES orders (order_num);

ALTER TABLE orderitems ADD CONSTRAINT fk_orderitems_products FOREIGN KEY (prod_id) REFERENCES products (prod_id);

ALTER TABLE orders ADD CONSTRAINT fk_orders_customers FOREIGN KEY (cust_id) REFERENCES customers (cust_id);

ALTER TABLE products ADD CONSTRAINT fk_products_vendors FOREIGN KEY (vend_id) REFERENCES vendors (vend_id);

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值