1. 数据库的存储引擎
1.1 InnoDB(支持事务)
1.2 MyISAM(不支持事务)
2. 安装mysql
2.1
一定要选择字符编码为UTF-8,如果在后期数据库中出现了乱码,就需要考虑一下你的数据库是否设置了UTF-8
2.2 是为root用户设置密码
2.3 默认的mysql端口为3306
3. 连接mysql服务器
3.1
可以通过mysql自带的命令行客户端连接
开始--所有程序---mysql--mysql Server--MySQL Command
Line Client 直接让你输入root用户的密码
3.2
通过mysql.exe客户端连接
进入mysql的bin目录 mysql
-h127.0.0.1 -P3306 -uroot -padmin
-h连接的主机 -P连接的端口
-u用户名 -p密码
mysql
-uroot -padmin 可以使用这个命令,
默认连接到本机的3306端口上
3.2 可以通过Navicat for
MySQL客户端连接到服务器上面
要注意先要建立一个服务器连接的配置信息
4. show databases 查看当前数据库服务器上有哪些数据库
5. 使用一个数据库:
use 数据库名称;
6.查看指定的数据库中有哪些数据表: 执行该命令之前先用选择一个数据库
show tables
7查看表的结构: 执行该命令之前先用选择一个数据库
desc 表名
8. 创建数据库
create database
数据库名字;
9. 删除数据库
drop database 数据库名字
10. Mysql中常见的数据类型
char(size): 定长的字符串 size是指字符的个数并且指定最大字符的个数,
如果大于指定字符个数的话,不让保存,
如果小于或者使用指定的字符个数的话,还是用size个字符存放
当保存在一列中的数据的长度是固定的话,那么就使用char,
性别
varchar(size): 变长的字符串
size是指字符的个数并且指定最大字符的个数, 如果大于指定字符个数的话,不让保存,
如果小于或者使用指定的字符个数的话, 那么在硬盘上保存的就是实际的字符数
如果不固定的话就是用varchar
又使用了一个字节来记录字符的个数
date: 日期数据,MySQL用'YYYY-MM-DD'格式检索和显示DATE值
DATETIME : 日期数据,要比date数据更确切,包含时分秒。MySQL以'YYYY-MM-DD
HH:MM:SS'格式检索和显示DATETIME值
Int(size): 整型数据(size是显示的宽度)
double[(s,p)] 数字型,可存放实型和整型
,精度(p)和范围(s)
double(2,2) .99 double(3,2)
9.99
blob 存放图形、声音和影像,二进制对象,0-4GB
text 存放大文本文件, 0-4GB
11. 创建一个表
class Employee{
private int id;
//唯一
private
String name;
private Integer age;
private String sex;
private Date birthday;
private double salary;
}
按照这个类的结构来创建一个表
建表语法:
create table
table_name
(
column_name1
column_properties constraint_definition,
column_name2 column_properties
constraint_definition,
#列名 类型 约束 (最后一行没有逗号)
)
CREATE table employee(
id
int primary key
auto_increment,
name
varchar(10),
age
int,
sex
char(1),
birthday date,
salary
double(8,2)
)
12. 使用Navicat将先用的.sql文件中的sql语句导入
在navicat上面选择数据库 右键
运行批次任务文件 选择sql文件
13, 对product表中字段的解释
CREATE TABLE `product` (
`id` bigint(11) NOT NULL auto_increment,
//唯一的标示
`productName` varchar(50) default NULL,
货品名称
`dir_id` bigint(11) default NULL,
分类编号
`salePrice` double(10,2) default NULL,
零售价
`supplier` varchar(50) default NULL,
供应商
`brand` varchar(50) default NULL,
品牌
`cutoff` double(2,2) default NULL,
折扣(批发的折扣)
`costPrice` double(10,2) default NULL,
进价
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
如果在dos的命令行中出现了乱码,你需要在mysql中设置GBK
使用一下的命令
set names 'GBK';
14.最简单的select语句
SELECT {*, column [alias],...}
FROM table;
说明:
SELECT 选择查询列表
FROM 提供数据源(表、视图或其他的数据源)
如果为 * 和创建表时的顺序一致。
可以自己调整顺序,在select后边加上要查询的列名。
1,查询所有货品信息
SELECT * from
product
如果为 * 和创建表时的顺序一致。
2,查询所有货品的id,productName,salePrice
select salePrice,id,productName
from product
可以自己调整顺序,在select后边加上要查询的列名。
3. 查询所有货品的id,名称和批发价
select id,productName,salePrice*cutoff from product; 运算的过程中只能够使用同行的不同列之间进行运算
4. 查询所有货品的id,名称,和各进50个的成本价
select id,productName, costPrice*50 from product
5. 查询所有货品的id,名称,各进50个,并且每个货品的运费1元的成本
select id,productName,(costPrice+1)*50 from
product;
6. 查询所有货品的id,名称,和各进50个的成本价 使用别名
select id as 编号,productName as 货品名称, costPrice*50
as 50个的成本 from product
7. 查询所有货品的id,名称,各进50个,并且每个货品的运费1元的成本 使用别名
select id '编 号',productName 货品名称,(costPrice+1)*50
'成 本' from
product;
如果被查询的结果设置一个别名的话, as可以省略,
如果别名中有特殊符号的话,需要使用"" ''
括起来..
限定所选择的记录
使用WHERE子句限定返回的记录
SELECT [DISTINCT] {*, column
[alias], ...} FROM
table
[WHERE condition(s)];
WHERE子句在 FROM 子句后
8 查询货品零售价为119的所有货品信息
select *
from product where salePrice = 119
9 查询货品名称为罗技G9X 的所有货品信息
select *
from product where
productname='罗技G9X'
select * from product
where binary
productname='罗技g9x'
第一种方法:
要让mysql查询区分大小写,可以:
select * from some_table where binary str='abc'
select * from some_table where binary str='ABC'
第二方法:
在建表时时候加以标识
create table some_table(
str
char(20) binary
)
10 查询分类编号不等于2的货品信息
select * from product
where dir_id<>2
11.查询货品名称,零售价小于等于200的货品
select productName,salePrice
from product where salePrice<=200;
12.查询id,货品名称,批发价大于350的货品
select id,productName,
cutoff*salePrice as pfPrice from product where
cutoff*salePrice >350
(思考使用where后面使用别名不行,总结select和where的执行顺序)
where语句是先执行的, 把符合条件的行数据取出来
然后再执行select语句,再从符合条件的行数据中取出相应的列
使用BETWEEN运算符
使用BETWEEN运算符显示某一值域范围的记录,这个操作符最常见的使用在数字类型数据的范围上,但对于字符类型数据和日期类型数据同样可用
13. 选择id,货品名称,批发价在300-400之间的货品
select id ,productName, cutoff*salePrice as pfPrice
from product
where cutoff*salePrice BETWEEN 300 and 400
使用IN运算符
使用IN运算符获得匹配列表值的记录,在IN操作符后跟着一个值的列表,可以应用日期,字符串数据类型
14 选择id,货品名称,分类编号为2,3,4的所有货品
select id,productName,dir_id from product
where dir_id in(2,3,4)
使用LIKE运算符
使用LIKE运算符执行通配查询
查询条件可包含文字字符或数字
(%) 可表示零或多个字符
( _ ) 可表示一个字符
15 查询id,货品名称,货品名称匹配'%罗技M1__'
select id,productName from
product where productName like '%罗技M1__'
A:罗技%
B:罗技_%
select id,productName from product where productName like '罗技%'
select id,productName from product where productName like '罗技_%'
它不能够查询出货品名字仅仅为 '罗技' 的货品
16 .
选择id,货品名称,分类编号,零售价大于等于50并且货品名称匹配'%罗技M1__'
select
id,productName,dir_id,salePrice from
product where salePrice>=50
and productName like '%罗技M1__'
17 . 选择id,货品名称,批发价在300-400之间的货品
select id,productName, salePrice*cutoff as
pfPrice from product where
salePrice*cutoff>=300 and
salePrice*cutoff<=400
18. 选择id,货品名称,分类编号的货品
零售价大于等于250或者是成本大于等于200
select id,productName,dir_id,salePrice,costPrice from product
where salePrice>=250 or costPrice>=200
19. 选择id,货品名称,分类编号为2,4的所有货品
select id,productName,dir_id from
product where dir_id=2 or dir_id=4
20 . 选择id,货品名称,分类编号不为2,4的所有货品
select id,productName,dir_id from
product where not (dir_id=2 or dir_id=4)
select
id,productName,dir_id from product where
dir_id not in(2,4)
21 . 选择id,货品名称,批发价不在300-400之间的货品
select id,productName, salePrice*cutoff as
pfPrice from product where
not (salePrice*cutoff>=300
and salePrice*cutoff<=400)
select id,productName,
salePrice*cutoff as pfPrice from product where
salePrice*cutoff not
between 300
and 400
优先级 运算符
1 所有比较运算符
2 NOT
3 AND
4 OR
括号将跨越所有优先级规则
SELECT id,productName FROM product WHERE NOT productName LIKE
'%M%' OR dir_id = 2 AND salePrice > 100
货品名字不包含M 或者 ( 分类编号为2 并且
零售价大于100 )
SELECT id,productName FROM product WHERE NOT (productName LIKE
'%M%' OR dir_id = 2) AND salePrice > 100
货品名字不包含M 并且
分类编号不为2 并且
零售价大于100
SELECT id,productName FROM product WHERE NOT (productName LIKE
'%M%' OR dir_id = 2 AND salePrice > 100)
货品名字不包含M
并且 分类编号不为2 或者
零售价小于等100
对查询出数据的排序
使用ORDER BY 子句将记录排序
ASC: 升序,缺省
DESC: 降序
ORDER BY 子句出现在SELECT语句的最后
22 按照货品零售价排序查询出货品的所有信息
select
* from product
order by salePrice [DESC/ASC] 默认是ASC,升序
23. 选择id,货品名称,分类编号,零售价并且按零售价降序排序
select
id,productName,dir_id,salePrice from product
order by salePrice desc
24. 选择id,货品名称,分类编号,零售价先按分类编号排序,再按零售价排序
select
id,productName,dir_id,salePrice from product order by
dir_id,salePrice
25.
选择id,货品名称,分类编号,零售价先按分类编号降序排序,再按零售价降序排序
select
id,productName,dir_id,salePrice from product order by dir_id
desc,salePrice desc
26.查询M系列(货品名字中含有M的货品)并按照批发价排序(加上别名)
select *,cutoff*salePrice as
pfPrice from product where
productName like '%M%' order by pfPrice
27,查询分类为2并按照批发价排序(加上别名)
select *,cutoff*salePrice as
pfPrice from product where
dir_id=2 order by pfPrice
asc
order by 在可以使用别名,说明select语句优先于ordery
by执行
sql中的执行顺序
from where select order by
使用表连接从多个表中查询数据
SELECT table1.column, table2.column
FROM table1, table2
WHERE table1.column1 = table2.column2;
如果要为一个表创建一个外键, 指定当前表中的一列的值参考另外一个表(其他的表,也是可以自己)中一列的数据
创建外键的前提是 当前表的类型为InnoDB,和参考表都是要表的类型为InnoDB
alter table product engine='InnoDB'
alter table productdir engine='InnoDB'
28 查询所有的货品信息+ 货品分类信息
select * from
product,productdir
29 查询所有的货品信息以及每个货品对应的分类
select * from
product,productdir where product.dir_id=productdir.id
30 , 查询货品id,货品名称,货品所属分类名称
select
product.id,product.productName,productDir.dirName
from
product,productdir where
product.dir_id=productdir.id
31 查询零售价大于200的无线鼠标
select * from product,productdir where
product.dir_id=productdir.id and product.salePrice>200 and
productdir.dirName='无线鼠标'
32 查询零售价大于200的无线鼠标 , 使用表的别名
select * from product as p,productdir as pd where
p.dir_id=pd.id and p.salePrice>200 and pd.dirName='无线鼠标'
as
也是可以省略的
select * from product p,productdir
pd where p.dir_id=pd.id and
p.salePrice>200 and pd.dirName='无线鼠标'
33. 查询货品信息+分类信息+库存信息
select * from product
p ,productdir pd,productstock ps
32. 查询每一个货品对应的分类以及对应的库存
select
* from product
p ,productdir pd,productstock ps
where p.dir_id=pd.id and
p.id=ps.product_id
33.查询货品名称,零售价,批发价,货品分类,货品库存,库存成本总价
select
p.productName,p.salePrice,p.cutoff*p.salePrice as
pfPrice ,pd.dirName,ps.storeNum, p.costPrice*ps.storeNum
as totalCostPrice
from product p,productdir
pd,productstock ps where p.dir_id=pd.id and
p.id=ps.product_id
34.查询货品名称,零售价,货品分类,库存不足的无线鼠标
select
p.productname,p.salePrice,pd.dirName,ps.storeNum,ps.warningNum
from product p,productdir pd,productstock ps
where p.dir_id=pd.id and p.id=ps.product_id and ps.storenum<=
ps.warningNum and pd.dirName='无线鼠标'
35.按照库存数量排序,查询货品名称,成本价,数量,库存成本总价
select
p.productName, ps.storeNum,ps.storeNum*p.costPrice as totalPrice
from product p,productstock ps where
p.id=ps.product_id order by ps.storeNum
36.如果库存货品都销售完成,按照利润从高到低查询货品名称,零售价,货品分类
select p.productName,p.salePrice,pd.dirName,
(p.cutoff*p.salePrice- p.costPrice)*ps.storeNum as lirui
from product p,productdir
pd,productstock ps where p.dir_id=pd.id and p.id=ps.product_id
order by lirui desc
37. 查询分类的id和名称,父分类的名称
select pd1.id,pd1.dirName,pd2.dirName from productdir pd1,productdir pd2
where pd1.parent_id=pd2.id
37. 查询出当前分类表中的子父类
select * from productdir p where p.parent_id
is not null
37. 查询出当前分类表中的父类
select * from productdir p where p.parent_id
is null
INSERT 语句
INSERT INTO tablename [(column [, column...])]
VALUES (value [, value...]);
一次插入操作只插入一行
1、insert语句将values中给定的值插入到表中的各个字段中,每次使用insert语句只能插入一行数据。
insert into
productdir(id,dirName,parent_id) values(5,'老人鼠标',1)
insert into
productdir(id,dirName,parent_id)
values(6,'老人鼠标',1),(7,'老人鼠标',1)
2、在insert语句中,table后面的方括号内是要插入数据的字段列表,字段列表包含了所有需要在插入时插入值的字段名称,在插入时不需要值的字段可以不出现在字段列表中。
insert into
productdir(id,dirName) values(8,'老人鼠标')
3、values后面是值的列表,也就是要插入到表中的各个字段的值。如果前面列出了字段列表,那么列表必须与字段列表一一对应,包括了个数,数据类型,位置的对应,如果忽略了字段列表,那么列表必须与表中字段一一对应(创建表时字段的顺序)。
insert into productdir
values(9,'老人鼠标',null)
4、如果有些字段在插入的时候不能确定初始值,则需要插入空值表示。在字段中插入空值有两种方式,一种是在需要空值的地方使用null关键子代替;另一种是通过列出不完全的字段列表实现,在字段列表中没有出现的字段,在插入记录后自动被赋予空值。
insert into productdir
values(11,'老人鼠标',null)
insert into
productdir(id,dirName) values(12,'老人鼠标')
5、字符串类型的字段值必须用单引号括起来, 例如: ’GOOD DAY’
6 如果说一个表中的一列被设置为主键, 那么该列上面的值不能够重复
7.
如果说一个表中的一列被设置自动递增,那么当我们给该列上插入null或是不指定的值得情况下
那么它自动递增
insert into
productdir(id,dirName) values(null,'女人鼠标')
insert into
productdir(dirName) values('女人鼠标')
insert into productdir(id,dirName)
values(100,'女人鼠标')
UPDATE 语句
执行update语句之前
1. 先对数据进行备份
2. 再通过select语句查询出需要修改的数据.来确实这些数据确实是需要修改
UPDATE table
SET column = value [, column = value] …
[WHERE condition];
1、在update语句中,where子句是可选的部分,如果使用了where子句,则修改的数据是符合where条件的所有记录;如果省略了where子句,则全表的数据都会被修改。
2、set子句后面是对表中一个或者多个字段的修改,如果修改多个字段的值,使用逗号将多个字段分开
将零售价大于300的货品零售价上调0.2倍
update product set salePrice=salePrice*1.2
where salePrice>300
将零售价大于300的有线鼠标的货品零售价上调0.1倍
update
product p ,productdir pd set
p.salePrice=p.salePrice*1.1 where p.dir_id=pd.id
and p.salePrice>300 and pd.dirName LIKE
'%有线%';
select * from
product p ,productdir pd
where p.dir_id=pd.id and
p.salePrice>300 and
pd.dirName LIKE '%有线%';
DELETE 语句
DELETE [FROM] table
[WHERE condition];
在delete语句中,where子句是可选的部分,如果使用了where子句,则删除的数据是符合where条件的所有记录;如果省略了where子句,则全表的数据都会被删除。
删除 老人鼠标 分类
delete from productdir
where dirName like '%老人%'
删除大于库存数量大于100的库存,并且该库存的货品为无线鼠标
delete ps
from product p,productdir pd,productstock ps
where p.dir_id=pd.id and p.id=ps.product_id and
ps.storeNum>100 and pd.dirName like
'%无线%'
select * from product
p,productdir pd,productstock ps where p.dir_id=pd.id and
p.id=ps.product_id and ps.storeNum>100 and
pd.dirName like '%无线%'
mysql数据库的导出和导入
通过mysqldump 导出数据
导出 mysqldump -uroot -padmin
--database 数据库名字
> d/xxxxxxxx.sql
导入数据:
1.先创建与导出数据名字一样的数据库
2. mysql -uroot -padmin
通过mysqldump命令导出的数据库的名字 < d/xxxxxxxx.sql