01 准备工作

Posted Sumomo

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了01 准备工作相关的知识,希望对你有一定的参考价值。

使用mysql进行学习

1.样例表

下载地址:http://www.forta.com/books/0672336073/

 

2.MySQL安装

(1)下载

地址:http://dev.mysql.com/downloads/mysql/

ZIP Archive版是免安装的,只要解压就行了,和安装版的没什么不同,将下载好的文件mysql-5.7.16-winx64.zip解压到D:\\Program Files\\SQL

打开D:\\Program Files\\SQL\\mysql-5.7.16-winx64\\bin目录下mysql.exe。出来一个黑窗口就没反应了。

(2)配置默认文件

D:\\Program Files\\SQL\\mysql-5.7.16-winx64\\bin目录下,新建my.ini。复制下面的代码保存就ok了。

[mysql]

# 设置mysql客户端默认字符集

default-character-set=utf8

[mysqld]

#设置3306端口

port = 3306

# 设置mysql的安装目录

basedir=D:\\Program Files\\SQL\\mysql-5.7.16-winx64

# 设置mysql数据库的数据的存放目录

datadir=D:\\Program Files\\SQL\\mysql-5.7.16-winx64\\data

# 允许最大连接数

max_connections=200

# 服务端使用的字符集默认为8比特编码的latin1字符集

character-set-server=utf8

# 创建新表时将使用的默认存储引擎

default-storage-engine=INNODB

.ini文件是window里面的配置文件。保存里面各种默认的数据。安装版的是在安装的时候让你自己选然后系统给你保存进来,zip archive则是自己写。

注意my.ini文件必须在\\bin目录中。

(3)安装mysql服务

以管理员身份运行cmd,切换到D:\\Program Files\\SQL\\mysql-5.7.16-winx64\\bin目录。

输入:mysqld --defaults-file=my.ini --initialize-insecure并回车,无报错。

输入:mysqld install并回车。

输入 net start mysql 启动服务。(也可以手动开启)

安装成功。

输入mysql -u root -p,输入密码,初始为空可直接回车,mysql可以使用了。

PS:直接用mysqld install安装会出现错误。

安装参考:http://blog.csdn.net/u013067166/article/details/49951577

3.MySQL Workbench

MySQL Workbench MySQL AB 释放的可视数据库设计工具,是设计MySQL 数据库的专用工具。

下载地址:http://dev.mysql.com/downloads/workbench/

点击安装:

更改安装目录:D:\\Program Files\\MySQL\\MySQL Workbench 6.3 CE\\

打开后界面:

4.建立数据库

双击Local instance MySQL(已经配置好了)以打开SQL编辑器。

(1)表的创建

1)新建schme,设置名字为tysql

点击保存。

2)现在可以在编辑器窗口中更改SQL类型,但必须首先确保选中了新创建的数据库。如果被选中,它的名称将以粗体显示在标题栏中。如果tysql未被选中,在左边窗口中双击tysql

未被选中:

被选中:

右击选择send to SQL editor----create Statement,将create.txt中的代码粘贴到其中。

-- -----------------------------------------

-- Sams Teach Yourself SQL in 10 Minutes

-- http://forta.com/books/0672336073/

-- Example table creation scripts for MySQL.

-- -----------------------------------------

 

 

-- ----------------------

-- Create Customers table

-- ----------------------

CREATE TABLE Customers

(

cust_id char(10) NOT NULL ,

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

);

 

-- -----------------------

-- 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

);

 

 

-- -------------------

-- Create Orders table

-- -------------------

CREATE TABLE Orders

(

order_num int NOT NULL ,

order_date datetime NOT NULL ,

cust_id char(10) NOT NULL

);

 

-- ---------------------

-- Create Products table

-- ---------------------

CREATE TABLE Products

(

prod_id char(10) NOT NULL ,

vend_id char(10) NOT NULL ,

prod_name char(255) NOT NULL ,

prod_price decimal(8,2) NOT NULL ,

prod_desc text NULL

);

 

-- --------------------

-- Create Vendors table

-- --------------------

CREATE TABLE Vendors

(

vend_id char(10) NOT NULL ,

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

);

 

 

-- -------------------

-- Define primary keys

-- -------------------

ALTER TABLE Customers ADD PRIMARY KEY (cust_id);

ALTER TABLE OrderItems ADD PRIMARY KEY (order_num, order_item);

ALTER TABLE Orders ADD PRIMARY KEY (order_num);

ALTER TABLE Products ADD PRIMARY KEY (prod_id);

ALTER TABLE Vendors ADD PRIMARY KEY (vend_id);

 

 

-- -------------------

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

注意:必须将CREATE DATABASE `tysql` /*!40100 DEFAULT CHARACTER SET latin1 */;这行代码删除,否则执行会报错,提示tysqldatabase已经创建过了。

3)执行Query----Execute(All or selection)

4)Schemas刷新一下,可看到有5个新建的table

创建成功,关闭SQL Statement窗口,保存为create.sql

(2)表的填充

1)右击选择send to SQL editor----create Statement,粘贴populate.txt中的代码。

-- -------------------------------------------

-- Sams Teach Yourself SQL in 10 Minutes

-- http://forta.com/books/0672336073/

-- Example table population scripts for MySQL.

-- -------------------------------------------

 

 

-- ------------------------

-- 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(\'1000000001\', \'Village Toys\', \'200 Maple Lane\', \'Detroit\', \'MI\', \'44444\', \'USA\', \'John Smith\', \'sales@villagetoys.com\');

INSERT INTO Customers(cust_id, cust_name, cust_address, cust_city, cust_state, cust_zip, cust_country, cust_contact)

VALUES(\'1000000002\', \'Kids Place\', \'333 South Lake Drive\', \'Columbus\', \'OH\', \'43333\', \'USA\', \'Michelle Green\');

INSERT INTO Customers(cust_id, cust_name, cust_address, cust_city, cust_state, cust_zip, cust_country, cust_contact, cust_email)

VALUES(\'1000000003\', \'Fun4All\', \'1 Sunny Place\', \'Muncie\', \'IN\', \'42222\', \'USA\', \'Jim Jones\', \'jjones@fun4all.com\');

INSERT INTO Customers(cust_id, cust_name, cust_address, cust_city, cust_state, cust_zip, cust_country, cust_contact, cust_email)

VALUES(\'1000000004\', \'Fun4All\', \'829 Riverside Drive\', \'Phoenix\', \'AZ\', \'88888\', \'USA\', \'Denise L. Stephens\', \'dstephens@fun4all.com\');

INSERT INTO Customers(cust_id, cust_name, cust_address, cust_city, cust_state, cust_zip, cust_country, cust_contact)

VALUES(\'1000000005\', \'The Toy Store\', \'4545 53rd Street\', \'Chicago\', \'IL\', \'54545\', \'USA\', \'Kim Howard\');

 

-- ----------------------

-- Populate Vendors table

-- ----------------------

INSERT INTO Vendors(vend_id, vend_name, vend_address, vend_city, vend_state, vend_zip, vend_country)

VALUES(\'BRS01\',\'Bears R Us\',\'123 Main Street\',\'Bear Town\',\'MI\',\'44444\', \'USA\');

INSERT INTO Vendors(vend_id, vend_name, vend_address, vend_city, vend_state, vend_zip, vend_country)

VALUES(\'BRE02\',\'Bear Emporium\',\'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(\'DLL01\',\'Doll House Inc.\',\'555 High Street\',\'Dollsville\',\'CA\',\'99999\', \'USA\');

INSERT INTO Vendors(vend_id, vend_name, vend_address, vend_city, vend_state, vend_zip, vend_country)

VALUES(\'FRB01\',\'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(\'FNG01\',\'Fun and Games\',\'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(\'JTS01\',\'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(\'BR01\', \'BRS01\', \'8 inch teddy bear\', 5.99, \'8 inch teddy bear, comes with cap and jacket\');

INSERT INTO Products(prod_id, vend_id, prod_name, prod_price, prod_desc)

VALUES(\'BR02\', \'BRS01\', \'12 inch teddy bear\', 8.99, \'12 inch teddy bear, comes with cap and jacket\');

INSERT INTO Products(prod_id, vend_id, prod_name, prod_price, prod_desc)

VALUES(\'BR03\', \'BRS01\', \'18 inch teddy bear\', 11.99, \'18 inch teddy bear, comes with cap and jacket\');

INSERT INTO Products(prod_id, vend_id, prod_name, prod_price, prod_desc)

VALUES(\'BNBG01\', \'DLL01\', \'Fish bean bag toy\', 3.49, \'Fish bean bag toy, complete with bean bag worms with which to feed it\');

INSERT INTO Products(prod_id, vend_id, prod_name, prod_price, prod_desc)

VALUES(\'BNBG02\', \'DLL01\', \'Bird bean bag toy\', 3.49, \'Bird bean bag toy, eggs are not included\');

INSERT INTO Products(prod_id, vend_id, prod_name, prod_price, prod_desc)

VALUES(\'BNBG03\', \'DLL01\', \'Rabbit bean bag toy\', 3.49, \'Rabbit bean bag toy, comes with bean bag carrots\');

INSERT INTO Products(prod_id, vend_id, prod_name, prod_price, prod_desc)

VALUES(\'RGAN01\', \'DLL01\', \'Raggedy Ann\', 4.99, \'18 inch Raggedy Ann doll\');

INSERT INTO Products(prod_id, vend_id, prod_name, prod_price, prod_desc)

VALUES(\'RYL01\', \'FNG01\', \'King doll\', 9.49, \'12 inch king doll with royal garments and crown\');

INSERT INTO Products(prod_id, vend_id, prod_name, prod_price, prod_desc)

VALUES(\'RYL02\', \'FNG01\', \'Queen doll\', 9.49, \'12 inch queen doll with royal garments and crown\');

 

-- ---------------------

-- Populate Orders table

-- ---------------------

INSERT INTO Orders(order_num, order_date, cust_id)

VALUES(20005, \'2012-05-01\', \'1000000001\');

INSERT INTO Orders(order_num, order_date, cust_id)

VALUES(20006, \'2012-01-12\', \'1000000003\');

INSERT INTO Orders(order_num, order_date, cust_id)

VALUES(20007, \'2012-01-30\', \'1000000004\');

INSERT INTO Orders(order_num, order_date, cust_id)

VALUES(20008, \'2012-02-03\', \'1000000005\');

INSERT INTO Orders(order_num, order_date, cust_id)

VALUES(20009, \'2012-02-08\', \'1000000001\');

 

-- -------------------------

-- Populate OrderItems table

-- -------------------------

INSERT INTO OrderItems(order_num, order_item, prod_id, quantity, item_price)

VALUES(20005, 1, \'BR01\', 100, 5.49);

INSERT INTO OrderItems(order_num, order_item, prod_id, quantity, item_price)

VALUES(20005, 2, \'BR03\', 100, 10.99);

INSERT INTO OrderItems(order_num, order_item, prod_id, quantity, item_price)

VALUES(20006, 1, \'BR01\', 20, 5.99);

INSERT INTO OrderItems(order_num, order_item, prod_id, quantity, item_price)

VALUES(20006, 2, \'BR02\', 10, 8.99);

INSERT INTO OrderItems(order_num, order_item, prod_id, quantity, item_price)

VALUES(20006, 3, \'BR03\', 10, 11.99);

INSERT INTO OrderItems(order_num, order_item, prod_id, quantity, item_price)

VALUES(20007, 1, \'BR03\', 50, 11.49);

INSERT INTO OrderItems(order_num, order_item, prod_id, quantity, item_price)

VALUES(20007, 2, \'BNBG01\', 100, 2.99);

INSERT INTO OrderItems(order_num, order_item, prod_id, quantity, item_price)

VALUES(20007, 3, \'BNBG02\', 100, 2.99);

INSERT INTO OrderItems(order_num, order_item, prod_id, quantity, item_price)

VALUES(20007, 4, \'BNBG03\', 100, 2.99);

INSERT INTO OrderItems(order_num, order_item, prod_id, quantity, item_price)

VALUES(20007, 5, \'RGAN01\', 50, 4.49);

INSERT INTO OrderItems(order_num, order_item, prod_id, quantity, item_price)

VALUES(20008, 1, \'RGAN01\', 5, 4.99);

INSERT INTO OrderItems(order_num, order_item, prod_id, quantity, item_price)

VALUES(20008, 2, \'BR03\', 5, 11.99);

INSERT INTO OrderItems(order_num, order_item, prod_id, quantity, item_price)

VALUES(20008, 3, \'BNBG01\', 10, 3.49);

INSERT INTO OrderItems(order_num, order_item, prod_id, quantity, item_price)

VALUES(20008, 4, \'BNBG02\', 10, 3.49);

INSERT INTO OrderItems(order_num, order_item, prod_id, quantity, item_price)

VALUES(20008, 5, \'BNBG03\', 10, 3.49);

INSERT INTO OrderItems(order_num, order_item, prod_id, quantity, item_price)

VALUES(20009, 1, \'BNBG01\', 250, 2.49);

INSERT INTO OrderItems(order_num, order_item, prod_id, quantity, item_price)

VALUES(20009, 2, \'BNBG02\', 250, 2.49);

INSERT INTO OrderItems(order_num, order_item, prod_id, quantity, item_price)

VALUES(20009, 3, \'BNBG03\', 250, 2.49);

2)执行,点击Query----Execute(All or selection)

3)关闭,保存为populate.sql

(3)测试

在Query窗口,或者send to SQL editor----create StatementSQL File窗口

输入:SELECT * FROM Customers;

执行:Query----Execute(All or selection)

 

以上是关于01 准备工作的主要内容,如果未能解决你的问题,请参考以下文章

背词典-01 准备工作

01-项目总体结构搭建与准备工作01

代码生成器-01准备工作(未完成)

数字化样机01-准备工作

01-hibernate注解:类级别注解准备工作

Spring Security应用开发(01)准备工作:下载编译和JAR包