http://www.cnblogs.com/hangxin1940/archive/2011/06/14/2081009.html
MySQL的安装与配置,不再赘述。
MySQL CRUD(增删改查)
列出所有数据库
1 |
show databases; |
01 |
--以mysql默认配置创建数据库 |
02 |
create database mydb1; |
03 |
04 |
--以utf8编码创建数据库 |
05 |
create database mydb2 character set utf8; |
06 |
07 |
--以utf8编码和utf8校对规则创建数据库 |
08 |
create database mydb3 character set utf8 collate utf8_general_ci; |
09 |
10 |
--现实创建数据库时的创建信息 |
11 |
show create database mydb2; |
1 |
drop database mydb1; |
修改数据库
1 |
--修改数据库,并把数据库编码变为gb2312 |
2 |
alter database mydb2 character set gb2312; |
创建表
01 |
--使用mydb3数据库 |
02 |
use mydb3; |
03 |
04 |
--创建一个名为employee的表 |
05 |
create table employee |
06 |
( |
07 |
id int , |
08 |
09 |
--可变字符,最大20个 |
10 |
name varchar (20), |
11 |
gender char (1), |
12 |
birthday date , |
13 |
entry_date date , |
14 |
job varchar (40), |
15 |
16 |
--货币格式,8位数字,2位小数 |
17 |
salary decimal (8,2), |
18 |
resume text |
19 |
20 |
--使用utf-8编码 |
21 |
) character set utf8; |
1 |
|
1 |
修改表
01 |
--显示表结构 |
02 |
desc employee; |
03 |
04 |
--显示表的创建语句 |
05 |
show create table employee; |
06 |
07 |
--表名改为user。 |
08 |
rename table employee to user ; |
09 |
10 |
--修改表的字符集为utf-8 |
11 |
alter table user character set utf8; |
12 |
13 |
--在原有的表结构上,增加一个名为image的二进制数据列 |
14 |
alter table employee add image blob; |
15 |
16 |
--列名name修改为username |
17 |
alter table user change column name username varchar (40); |
18 |
19 |
--修改job列,使其长度为60。 |
20 |
alter table employee modify job varchar (60); |
21 |
22 |
--删除sex列。 |
23 |
alter table employee drop gender; |
插入数据
1 |
insert into employee(id, name ) values (1, 'aaa' ); |
修改数据
01 |
--将所有员工薪水修改为5000元。 |
02 |
update employee set salary=5000; |
03 |
04 |
--将姓名为’zs’的员工薪水修改为3000元。 |
05 |
update employee set salary=3000 where name = 'aaa' ; |
06 |
07 |
--将姓名为’aaa’的员工薪水修改为4000元,job改为ccc。 |
08 |
update employee set salary=4000,entry_date= '1980-08-08' where name = 'aaa' ; |
09 |
10 |
--将wu的薪水在原有基础上增加1000元。 |
11 |
update employee set salary=salary+1000 where name = 'aaa' ; |
删除数据
1 |
--删除表中名称为’zs’的记录。 |
2 |
delete from employee where name = 'aaa' ; |
3 |
4 |
--删除表中所有记录。 |
5 |
delete from employee; --逐行删除 |
6 |
truncate table employee; --整体摧毁 |
查询数据
01 |
--查询表中的所有数据 |
02 |
select * from employee; |
03 |
04 |
--查询表中所有学生的信息。 |
05 |
select * from student; |
06 |
07 |
--查询表中所有学生的姓名和对应的英语成绩。 |
08 |
select name ,english from student; |
09 |
10 |
--以english为基准过滤表中重复数据。 |
11 |
select distinct english from student; |
12 |
13 |
--在所有学生的数学分数上加10分。 |
14 |
select name ,math+10 from student; |
15 |
16 |
--统计每个学生的总分。 |
17 |
select name ,(chinese+english+math) from student; |
18 |
19 |
--使用别名表示学生分数。 |
20 |
select name as 姓名,(chinese+english+math) as 总分 from student; |
21 |
select name 姓名,(chinese+english+math) 总分 from student; |
22 |
23 |
--查询姓名为wu的学生成绩 |
24 |
select * from student where name = '王五' ; |
25 |
26 |
--查询英语成绩大于90分的同学 |
27 |
select * from student where english>90; |
28 |
29 |
--查询总分大于200分的所有同学 |
30 |
select * from student where (chinese+math+english)>200; |
31 |
32 |
--查询英语分数在 80-90之间的同学。 |
33 |
select * from student where english>80 and english<90; |
34 |
select * from student where english between 80 and 90; |
35 |
36 |
--查询数学分数为89,90,91的同学。 |
37 |
select * from student where math in (89,90,91); |
38 |
39 |
--查询所有姓李的学生成绩。 |
40 |
select * from student where name like '李_' ; |
41 |
42 |
--查询数学分>80,语文分>80的同学。 |
43 |
select * from student where math>80 and chinese>80; |
44 |
45 |
46 |
--对数学成绩排序后输出。 |
47 |
select name ,math from student order by math; |
48 |
49 |
--对总分排序后输出,然后再按从高到低的顺序输出 |
50 |
select name 姓名,(chinese+math+english) 总分 from student order by 总分 desc ; |
51 |
52 |
--对姓李的学生成绩排序输出 |
53 |
select name ,(chinese+math+english) 总分 from student where name like '李%' order by 总分 desc ; |
54 |
55 |
--统计一个班级共有多少学生? |
56 |
select count (*) from student; |
57 |
select count ( name ) from student; |
58 |
59 |
--统计数学成绩大于90的学生有多少个? |
60 |
select count (math) from student where math>90; |
61 |
62 |
--统计总分大于250的人数有多少? |
63 |
select count (*) from student where (chinese+math+english)>250; |
64 |
65 |
--统计一个班级数学总成绩? |
66 |
select sum (math) from student; |
67 |
68 |
--统计一个班级语文、英语、数学各科的总成绩 |
69 |
select sum (chinese), sum (math), sum (english) from student; |
70 |
71 |
--统计一个班级语文、英语、数学的成绩总和 |
72 |
select sum (chinese+math+english) from student; |
73 |
74 |
--统计一个班级语文成绩平均分 |
75 |
select sum (chinese)/ count (chinese) from student; |
76 |
77 |
--求一个班级语文平均分? |
78 |
select avg (chinese) from student; |
79 |
80 |
--求一个班级总分平均分 |
81 |
select avg (chinese+math+english) from student; |
82 |
83 |
--求班级最高分和最低分 |
84 |
select max (chinese+math+english), min (chinese+math+english) from student; |
85 |
86 |
--对订单表中商品归类后,显示每一类商品的总价 |
87 |
select product, sum (price) from orders group by product; |
88 |
89 |
--查询购买了几类商品,并且每类总价大于100的商品 |
90 |
select product from orders group by product having sum (price)>100; |
数据库表设计的一般原则
在通常的java-web设计中,为了降低耦合度,会以javabean作为数据传递的媒介,java-bean最终会传递给显示层或数据操作层用以向用户展现或服务器存贮。
在java-bean中,存储了消息对象的各种属性,在数据库中,也以相应的对象为一个单独的表,表中的列(字段)即为java-bean中的主要属性。
示例一:
java-bean
01 |
//一个学生类 |
02 |
public Student { |
03 |
public int id; //这就相当于主键了 |
04 |
public String name; |
05 |
public String address; |
06 |
public String class ; |
07 |
//描述这个学生得上多少个老师的课程 |
08 |
public Set<Teacher> teachers; |
09 |
|
10 |
/*各种get set省略*/ |
11 |
/* |
12 |
* |
13 |
* |
14 |
*/ |
15 |
} |
在这个bean中,所有的属性描述了student的基本信息。现在根据web业务中的对象来设计数据中的student表,student表中的字段应与bean中的属性一致,因为他们代表了同一个东西。
表student
1 |
+------------+-------------+------+-----+---------+-------+ |
2 |
| Field | Type | Null | Key | Default | Extra | |
3 |
+------------+-------------+------+-----+---------+-------+ |
4 |
| id | int(11) | YES | | NULL | | |
5 |
| address | varchar(50) | YES | | NULL | | |
6 |
| class | varchar(20) | YES | | NULL | | |
7 |
| teacher_id | int(11) | YES | | NULL | | |
8 |
+------------+-------------+------+-----+---------+-------+ |
一个大致的样子出来了,但它是独立的,因为java-bean中直接包含了一个教师的列表,它指向了一个教师的集合。而这里的字段则只是一个值,这里就需要使用主键和外键了。
现在假设有teacher表(java-bean不再赘述,与student的bean差不多),结构如下:
1 |
+------------+-------------+------+-----+---------+-------+ |
2 |
| Field | Type | Null | Key | Default | Extra | |
3 |
+------------+-------------+------+-----+---------+-------+ |
4 |
| id | int(11) | YES | | NULL | | |
5 |
| address | varchar(50) | YES | | NULL | | |
6 |
| student_id | int(11)
share
|