00 MySQL必知必会涉及的数据库

建表语句:

########################################
# MySQL Crash Course
# http://www.forta.com/books/0672327120/
# Example table creation scripts
########################################

########################
# 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);

插入数据:

########################################
# MySQL Crash Course
# http://www.forta.com/books/0672327120/
# Example table population scripts
########################################

##########################
# Populate customers table
##########################
INSERT INTO customers(cust_id, cust_name, cust_address, cust_city, cust_state, cust_zip, cust_country, cust_contact, cust_email)
VALUES(10001, ‘Coyote Inc.‘, ‘200 Maple Lane‘, ‘Detroit‘, ‘MI‘, ‘44444‘, ‘USA‘, ‘Y Lee‘, ‘[email protected]‘);
INSERT INTO customers(cust_id, cust_name, cust_address, cust_city, cust_state, cust_zip, cust_country, cust_contact)
VALUES(10002, ‘Mouse House‘, ‘333 Fromage Lane‘, ‘Columbus‘, ‘OH‘, ‘43333‘, ‘USA‘, ‘Jerry Mouse‘);
INSERT INTO customers(cust_id, cust_name, cust_address, cust_city, cust_state, cust_zip, cust_country, cust_contact, cust_email)
VALUES(10003, ‘Wascals‘, ‘1 Sunny Place‘, ‘Muncie‘, ‘IN‘, ‘42222‘, ‘USA‘, ‘Jim Jones‘, ‘[email protected]‘);
INSERT INTO customers(cust_id, cust_name, cust_address, cust_city, cust_state, cust_zip, cust_country, cust_contact, cust_email)
VALUES(10004, ‘Yosemite Place‘, ‘829 Riverside Drive‘, ‘Phoenix‘, ‘AZ‘, ‘88888‘, ‘USA‘, ‘Y Sam‘, ‘[email protected]‘);
INSERT INTO customers(cust_id, cust_name, cust_address, cust_city, cust_state, cust_zip, cust_country, cust_contact)
VALUES(10005, ‘E Fudd‘, ‘4545 53rd Street‘, ‘Chicago‘, ‘IL‘, ‘54545‘, ‘USA‘, ‘E Fudd‘);

########################
# Populate vendors table
########################
INSERT INTO vendors(vend_id, vend_name, vend_address, vend_city, vend_state, vend_zip, vend_country)
VALUES(1001,‘Anvils R Us‘,‘123 Main Street‘,‘Southfield‘,‘MI‘,‘48075‘, ‘USA‘);
INSERT INTO vendors(vend_id, vend_name, vend_address, vend_city, vend_state, vend_zip, vend_country)
VALUES(1002,‘LT Supplies‘,‘500 Park Street‘,‘Anytown‘,‘OH‘,‘44333‘, ‘USA‘);
INSERT INTO vendors(vend_id, vend_name, vend_address, vend_city, vend_state, vend_zip, vend_country)
VALUES(1003,‘ACME‘,‘555 High Street‘,‘Los Angeles‘,‘CA‘,‘90046‘, ‘USA‘);
INSERT INTO vendors(vend_id, vend_name, vend_address, vend_city, vend_state, vend_zip, vend_country)
VALUES(1004,‘Furball Inc.‘,‘1000 5th Avenue‘,‘New York‘,‘NY‘,‘11111‘, ‘USA‘);
INSERT INTO vendors(vend_id, vend_name, vend_address, vend_city, vend_state, vend_zip, vend_country)
VALUES(1005,‘Jet Set‘,‘42 Galaxy Road‘,‘London‘, NULL,‘N16 6PS‘, ‘England‘);
INSERT INTO vendors(vend_id, vend_name, vend_address, vend_city, vend_state, vend_zip, vend_country)
VALUES(1006,‘Jouets Et Ours‘,‘1 Rue Amusement‘,‘Paris‘, NULL,‘45678‘, ‘France‘);

#########################
# Populate products table
#########################
INSERT INTO products(prod_id, vend_id, prod_name, prod_price, prod_desc)
VALUES(‘ANV01‘, 1001, ‘.5 ton anvil‘, 5.99, ‘.5 ton anvil, black, complete with handy hook‘);
INSERT INTO products(prod_id, vend_id, prod_name, prod_price, prod_desc)
VALUES(‘ANV02‘, 1001, ‘1 ton anvil‘, 9.99, ‘1 ton anvil, black, complete with handy hook and carrying case‘);
INSERT INTO products(prod_id, vend_id, prod_name, prod_price, prod_desc)
VALUES(‘ANV03‘, 1001, ‘2 ton anvil‘, 14.99, ‘2 ton anvil, black, complete with handy hook and carrying case‘);
INSERT INTO products(prod_id, vend_id, prod_name, prod_price, prod_desc)
VALUES(‘OL1‘, 1002, ‘Oil can‘, 8.99, ‘Oil can, red‘);
INSERT INTO products(prod_id, vend_id, prod_name, prod_price, prod_desc)
VALUES(‘FU1‘, 1002, ‘Fuses‘, 3.42, ‘1 dozen, extra long‘);
INSERT INTO products(prod_id, vend_id, prod_name, prod_price, prod_desc)
VALUES(‘SLING‘, 1003, ‘Sling‘, 4.49, ‘Sling, one size fits all‘);
INSERT INTO products(prod_id, vend_id, prod_name, prod_price, prod_desc)
VALUES(‘TNT1‘, 1003, ‘TNT (1 stick)‘, 2.50, ‘TNT, red, single stick‘);
INSERT INTO products(prod_id, vend_id, prod_name, prod_price, prod_desc)
VALUES(‘TNT2‘, 1003, ‘TNT (5 sticks)‘, 10, ‘TNT, red, pack of 10 sticks‘);
INSERT INTO products(prod_id, vend_id, prod_name, prod_price, prod_desc)
VALUES(‘FB‘, 1003, ‘Bird seed‘, 10, ‘Large bag (suitable for road runners)‘);
INSERT INTO products(prod_id, vend_id, prod_name, prod_price, prod_desc)
VALUES(‘FC‘, 1003, ‘Carrots‘, 2.50, ‘Carrots (rabbit hunting season only)‘);
INSERT INTO products(prod_id, vend_id, prod_name, prod_price, prod_desc)
VALUES(‘SAFE‘, 1003, ‘Safe‘, 50, ‘Safe with combination lock‘);
INSERT INTO products(prod_id, vend_id, prod_name, prod_price, prod_desc)
VALUES(‘DTNTR‘, 1003, ‘Detonator‘, 13, ‘Detonator (plunger powered), fuses not included‘);
INSERT INTO products(prod_id, vend_id, prod_name, prod_price, prod_desc)
VALUES(‘JP1000‘, 1005, ‘JetPack 1000‘, 35, ‘JetPack 1000, intended for single use‘);
INSERT INTO products(prod_id, vend_id, prod_name, prod_price, prod_desc)
VALUES(‘JP2000‘, 1005, ‘JetPack 2000‘, 55, ‘JetPack 2000, multi-use‘);

#######################
# Populate orders table
#######################
INSERT INTO orders(order_num, order_date, cust_id)
VALUES(20005, ‘2005-09-01‘, 10001);
INSERT INTO orders(order_num, order_date, cust_id)
VALUES(20006, ‘2005-09-12‘, 10003);
INSERT INTO orders(order_num, order_date, cust_id)
VALUES(20007, ‘2005-09-30‘, 10004);
INSERT INTO orders(order_num, order_date, cust_id)
VALUES(20008, ‘2005-10-03‘, 10005);
INSERT INTO orders(order_num, order_date, cust_id)
VALUES(20009, ‘2005-10-08‘, 10001);

###########################
# Populate orderitems table
###########################
INSERT INTO orderitems(order_num, order_item, prod_id, quantity, item_price)
VALUES(20005, 1, ‘ANV01‘, 10, 5.99);
INSERT INTO orderitems(order_num, order_item, prod_id, quantity, item_price)
VALUES(20005, 2, ‘ANV02‘, 3, 9.99);
INSERT INTO orderitems(order_num, order_item, prod_id, quantity, item_price)
VALUES(20005, 3, ‘TNT2‘, 5, 10);
INSERT INTO orderitems(order_num, order_item, prod_id, quantity, item_price)
VALUES(20005, 4, ‘FB‘, 1, 10);
INSERT INTO orderitems(order_num, order_item, prod_id, quantity, item_price)
VALUES(20006, 1, ‘JP2000‘, 1, 55);
INSERT INTO orderitems(order_num, order_item, prod_id, quantity, item_price)
VALUES(20007, 1, ‘TNT2‘, 100, 10);
INSERT INTO orderitems(order_num, order_item, prod_id, quantity, item_price)
VALUES(20008, 1, ‘FC‘, 50, 2.50);
INSERT INTO orderitems(order_num, order_item, prod_id, quantity, item_price)
VALUES(20009, 1, ‘FB‘, 1, 10);
INSERT INTO orderitems(order_num, order_item, prod_id, quantity, item_price)
VALUES(20009, 2, ‘OL1‘, 1, 8.99);
INSERT INTO orderitems(order_num, order_item, prod_id, quantity, item_price)
VALUES(20009, 3, ‘SLING‘, 1, 4.49);
INSERT INTO orderitems(order_num, order_item, prod_id, quantity, item_price)
VALUES(20009, 4, ‘ANV03‘, 1, 14.99);

#############################
# Populate productnotes table
#############################
INSERT INTO productnotes(note_id, prod_id, note_date, note_text)
VALUES(101, ‘TNT2‘, ‘2005-08-17‘,
‘Customer complaint:
Sticks not individually wrapped, too easy to mistakenly detonate all at once.
Recommend individual wrapping.‘
);
INSERT INTO productnotes(note_id, prod_id, note_date, note_text)
VALUES(102, ‘OL1‘, ‘2005-08-18‘,
‘Can shipped full, refills not available.
Need to order new can if refill needed.‘
);
INSERT INTO productnotes(note_id, prod_id, note_date, note_text)
VALUES(103, ‘SAFE‘, ‘2005-08-18‘,
‘Safe is combination locked, combination not provided with safe.
This is rarely a problem as safes are typically blown up or dropped by customers.‘
);
INSERT INTO productnotes(note_id, prod_id, note_date, note_text)
VALUES(104, ‘FC‘, ‘2005-08-19‘,
‘Quantity varies, sold by the sack load.
All guaranteed to be bright and orange, and suitable for use as rabbit bait.‘
);
INSERT INTO productnotes(note_id, prod_id, note_date, note_text)
VALUES(105, ‘TNT2‘, ‘2005-08-20‘,
‘Included fuses are short and have been known to detonate too quickly for some customers.
Longer fuses are available (item FU1) and should be recommended.‘
);
INSERT INTO productnotes(note_id, prod_id, note_date, note_text)
VALUES(106, ‘TNT2‘, ‘2005-08-22‘,
‘Matches not included, recommend purchase of matches or detonator (item DTNTR).‘
);
INSERT INTO productnotes(note_id, prod_id, note_date, note_text)
VALUES(107, ‘SAFE‘, ‘2005-08-23‘,
‘Please note that no returns will be accepted if safe opened using explosives.‘
);
INSERT INTO productnotes(note_id, prod_id, note_date, note_text)
VALUES(108, ‘ANV01‘, ‘2005-08-25‘,
‘Multiple customer returns, anvils failing to drop fast enough or falling backwards on purchaser. Recommend that customer considers using heavier anvils.‘
);
INSERT INTO productnotes(note_id, prod_id, note_date, note_text)
VALUES(109, ‘ANV03‘, ‘2005-09-01‘,
‘Item is extremely heavy. Designed for dropping, not recommended for use with slings, ropes, pulleys, or tightropes.‘
);
INSERT INTO productnotes(note_id, prod_id, note_date, note_text)
VALUES(110, ‘FC‘, ‘2005-09-01‘,
‘Customer complaint: rabbit has been able to detect trap, food apparently less effective now.‘
);
INSERT INTO productnotes(note_id, prod_id, note_date, note_text)
VALUES(111, ‘SLING‘, ‘2005-09-02‘,
‘Shipped unassembled, requires common tools (including oversized hammer).‘
);
INSERT INTO productnotes(note_id, prod_id, note_date, note_text)
VALUES(112, ‘SAFE‘, ‘2005-09-02‘,
‘Customer complaint:
Circular hole in safe floor can apparently be easily cut with handsaw.‘
);
INSERT INTO productnotes(note_id, prod_id, note_date, note_text)
VALUES(113, ‘ANV01‘, ‘2005-09-05‘,
‘Customer complaint:
Not heavy enough to generate flying stars around head of victim. If being purchased for dropping, recommend ANV02 or ANV03 instead.‘
);
INSERT INTO productnotes(note_id, prod_id, note_date, note_text)
VALUES(114, ‘SAFE‘, ‘2005-09-07‘,
‘Call from individual trapped in safe plummeting to the ground, suggests an escape hatch be added.
Comment forwarded to vendor.‘
);

时间: 2024-07-30 08:08:51

00 MySQL必知必会涉及的数据库的相关文章

mysql学习--mysql必知必会1

?? 例如以下为mysql必知必会第九章開始: 正則表達式用于匹配特殊的字符集合.mysql通过where子句对正則表達式提供初步的支持. keywordregexp用来表示后面跟的东西作为正則表達式处理. (.)是正則表達式的一个符号,表示匹配随意一个字符: mysql> select prod_name -> from products -> where prod_name regexp '.000' -> order by prod_name; +--------------

mysql学习--mysql必知必会

上图为数据库操作分类: 以下的操作参考(mysql必知必会) 创建数据库 执行脚本建表: mysql> create database mytest; Query OK, 1 row affected (0.07 sec) mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | mytest | | performan

《mysql必知必会》笔记-部分

<mysql必知必会>笔记-部分 注释: 开始时整理笔记,记录自己看书过程的疑问和重点,后来发现记录没太多意义,还不如直接去翻原书,所以就放弃了. 因此,这个笔记只有一部分,不过依旧分享出来. 1.语言分类: 客户机可以是MySQL提供的工具.脚本语言(如perl).Web应用开发语言(如ASP.ColdFusion.JSP和PHP).程序设计语言(如C.C+kJava)等. 2.之前找不到表的原因: 虽然SQL是不区分大小写的,但有些标识符(如数据库名.表名.列名)可能不同:在MySQL4.

《 MySQL必知必会》笔记_持续更新

< MySQL必知必会> structured querylanguage,MySQL是一个数据库系统管理软件软件,也可以理解为为数据库服务器.读音,my sequel,/?si?kw?l / see kuo 第1.2.3章 schema,提要纲要,column列.row行 数据库:一某种有组织的方式来储存的数据集合 DBMS databasemanegersystem数据库管理系统 table,表,某种特殊类型数据的结构化清单 一个数据中,表名称有唯一性 表的特性,我们用schema[模式]

MySQL必知必会1

MySQL必知必会 ? 了解SQL 什么是数据库:数据库(database)保存有阻止的数据的容器,可以把数据库想象成一个文件柜. 什么是表:表(table) 某种特定类型结构的结构化清单,数据库中的表的名字是唯一的. 什么是列:列(column)表中的一个字段.所有表都是有一个或多个列组成的,理解列的最好办法是将数据库表想象为一个网格,网格中每一列存储着一条特定信息.例如,编号,地址,邮政编码. 什么是行:行(row) 表中的数据是按行存储的,所保存的每个记录存储在自己的行内,例如,每一行存储

MySQL必知必会-官方数据库表及SQL脚本导入生成

最近在复习SQL语句,看的是MySQL必知必会这本书,但是发现附录中只有表设计,没有表的具体数据.所以在学习相应的语句中体验不是很好,去网上查了数据库的内容,自己慢慢导入到了数据库中.把表放出来作为参照,SQL脚本语句放在最后,可以直接导到自己的数据库. customer表 cust_id cust_name cust_address cust_city cust_state cust_zip cust_country cust_contact cust_email 10001 Coyote I

MySQL必知必会 学习笔记(一)

第一章  了解SQL 模式:   关于数据库和表的布局以及特性的信息.[描述表可以存储什么样的数据,数据如何分解,各部分信息如何命名等等,可以用来描述数据库中特定的表以及整个数据库(和其中表的关系)]. 第二章 MySQL简介 MySQL是一种DBMS,即它是一种数据库软件.基于客户机----服务器的数据库. MySQL工具: 1.mysql 命令行实用程序 2.MySQL Administrator 3.MySQL query Browser 第四章 检索数据 LIMIT 5 表示MySQL返

mysql 必知必会总结

以前 mysql 用的不是很多, 2 天看了一遍 mysql 必知必会又复习了一下基础.  200 页的书,很快就能看完, 大部分知识比较基础, 但还是了解了一些以前不知道的知识点.自己做一个备份,随时查看. 命令:sql 不区分大小写,语句大写,列.表名小写是一种习惯连接命令:mysql -u user_name –h example.mysql.alibabalabs.com –P3306 –pxxxxquithelp show; // 查看所有 show 命令show databases;

《MySQL必知必会》读书笔记_4

PS:一个实际的存储过程案例 CREATE DEFINER=`root`@`localhost` PROCEDURE `sp_delete_article_by_id`(IN `id` int) BEGIN #Routine body goes here... DECLARE temp INT; SET @aid = id; SELECT COUNT(*) FROM gk_reply_article WHERE a_id = @aid INTO temp; IF(temp <> 0) THEN

《MySQL 必知必会》读书总结

这是 <MySQL 必知必会> 的读书总结.也是自己整理的常用操作的参考手册. ? ? 使用 MySQL 连接到 MySQL shell>mysql -u root -p Enter password:****** 显示数据库 mysql>SHOW DATABASES; 选择数据库 mysql>USE mytest; 显示数据库中的表 mysql>SHOW TABLES; 显示表列 mysql>SHOW COLUMNS FROM tmall_user; mysql