1、查询所有的数据库
- mysql> show databases;
2、创建数据库:
- mysql> create database day26;
或者不换行直接:create database day2601 default character set utf8;
3、删除数据库
-drop database day26;
4、查看当前数据库编码
- mysql> use day26;
5、修改数据库编码
- mysql> alter database day26 character set gbk;
6、修改某一个值
-mysql> set character_set_client=gbk;
7、删除数据库
- mysql> drop database day2601;
/*
* 数据的管理:
mysql> create database day27;
Query OK, 1 row affected (0.00 sec)
mysql> use day27;
Database changed
mysql> create table student(
-> id int,
-> name varchar(30),
-> gender varchar(2),
-> age int,
-> english varchar(30),
-> chinese varchar(30),
-> address varchar(300)
-> );
Query OK, 0 rows affected (0.07 sec)
1.增删改数据
1.1 添加数据
insert [into] table (clo_name,…) values (col_value,…….)
mysql> insert into student values (1,’alex’,’1’,20,’90’,’100’,’sichuang’);
Query OK, 1 row affected (0.03 sec)
mysql> insert into student(id,name) values (5,’ouzheng’);
Query OK, 1 row affected (0.03 sec)
--错误的使用方式:
mysql> insert into student values (3,'lily','0',18,'99','100');
ERROR 1136 (21S01): Column count doesn't match value count at row 1
1.2 修改数据
update tablename set col_name=col_value,……where id=_id
mysql> update student set gender=1 where id=5;
Query OK, 1 row affected (0.04 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> update student set gender=1,age=18,chinese=’100’,english=’100’ where id=5;
Query OK, 1 row affected (0.03 sec)
Rows matched: 1 Changed: 1 Warnings: 0
– 这种 方式尽量少用。
mysql> update student set gender=0;
Query OK, 3 rows affected (0.03 sec)
Rows matched: 4 Changed: 3 Warnings: 0
1.3 删除数据
delete from table where id=_id
mysql> delete from student where id=3;
Query OK, 1 row affected (0.04 sec)
– 建议少用
mysql> delete from student;
Query OK, 3 rows affected (0.03 sec)
TRUNCATE [TABLE] tbl_name : 用于完全清空一个表。
mysql> truncate student;
Query OK, 0 rows affected (0.02 sec)
delete 与 truncate的区别:
delete后面是可以带条件 ,也可以不带条件。 只能删除表的数据,不能删除表中的约束。delete删除的数据是可以回滚(事务)
truncate后面是为能带条件的。即可以删除表中的数据,又可以删除表的约束。truncate删除的数据不能回滚。
mysql> create table test(
-> id int primary key auto_increment,
-> name varchar(30)
-> );
Query OK, 0 rows affected (0.06 sec)
mysql> insert into test(name) values ('lily');
Query OK, 1 row affected (0.03 sec)
mysql> insert into test(name) values ('jim');
Query OK, 1 row affected (0.03 sec)
mysql> insert into test(name) values ('hanmeimei');
Query OK, 1 row affected (0.03 sec)
mysql> select * from test;
+----+-----------+
| id | name |
+----+-----------+
| 1 | lily |
| 2 | jim |
| 3 | hanmeimei |
+----+-----------+
3 rows in set (0.00 sec)
–只能删除表的数据,不能删除表中的约束。
mysql> delete from test;
Query OK, 3 rows affected (0.03 sec)
mysql> select * from test;
Empty set (0.00 sec)
mysql> insert into test(name) values ('yangjinliang');
Query OK, 1 row affected (0.03 sec)
mysql> select * from test;
+----+--------------+
| id | name |
+----+--------------+
| 4 | yangjinliang |
+----+--------------+
1 row in set (0.00 sec)
--即可以删除表中的数据,又可以删除表的约束。
mysql> truncate test;
Query OK, 0 rows affected (0.03 sec)
mysql> select * from test;
Empty set (0.00 sec)
mysql> insert into test(name) values ('lily');
Query OK, 1 row affected (0.03 sec)
mysql> insert into test(name) values ('lily');
Query OK, 1 row affected (0.04 sec)
mysql> insert into test(name) values ('lily');
Query OK, 1 row affected (0.03 sec)
mysql> select * from test;
+----+------+
| id | name |
+----+------+
| 1 | lily |
| 2 | lily |
| 3 | lily |
+----+------+
3 rows in set (0.00 sec)
*/
/*
* 表的管理
*
* 首先第一步:选择要操作的数据库
* mysql> use day26;
Database changed
*
*
* 1、查看所有表
* mysql> show tables;
Empty set (0.00 sec)
2、创建表
mysql> create table student(
-> sid int,
-> sname varchar(20),
-> sage int,
-> jsp varchar(30),
-> servlet varchar(30)
-> );
Query OK, 0 rows affected (0.06 sec)
*
* 多个字段之间使用逗号分隔,最后一个字段不加逗号。
*
* 3、查看表结构
* mysql> desc student;
+———+————-+——+—–+———+——-+
| Field | Type | Null | Key | Default | Extra |
+———+————-+——+—–+———+——-+
| sid | int(11) | YES | | NULL | |
| sname | varchar(20) | YES | | NULL | |
| sage | int(11) | YES | | NULL | |
| jsp | varchar(30) | YES | | NULL | |
| servlet | varchar(30) | YES | | NULL | |
+———+————-+——+—–+———+——-+
5 rows in set (0.00 sec)
*
*
* 4、修改表
* 1) 添加字段
* mysql> alter table student add column sgender varchar(2);
Query OK, 0 rows affected (0.19 sec)
Records: 0 Duplicates: 0 Warnings: 0
*
* column可以省略
*
*
*
* 2) 修改字段类型
* mysql> alter table student modify column sgender int;
Query OK, 0 rows affected (0.13 sec)
Records: 0 Duplicates: 0 Warnings: 0
column可以省略
*
* 3) 修改字段名称
* mysql> alter table student change column sgender gender varchar(2);
Query OK, 0 rows affected (0.13 sec)
Records: 0 Duplicates: 0 Warnings: 0
*
*
* column可以省略
4) 删除字段
* mysql> alter table student drop column sgender;
Query OK, 0 rows affected (0.24 sec)
Records: 0 Duplicates: 0 Warnings: 0
to可以省略
5、删除表
mysql> drop table student;
Query OK, 0 rows affected (0.03 sec)
删除数据库
mysql> drop database day2601;
*
*
*/
/*
* 查询:(select)
*
* select * from table where id=_id
*
* 1.1 查询所有列
* mysql> select * from student;
*
* 1.2 查询指定列
* mysql> select name,gender,age from student;
*
* 1.3 查询时指定列的别名(as关键字,as是可以省略的)
* mysql> select name as ‘姓名’,gender as ‘性别’,age ‘年龄’ from student;
*
* 1.4 查询时添加常量列
* mysql> select name as ‘姓名’,gender as ‘性别’,age ‘年龄’,’java225班’ as ‘年级’ from student;
*
* 1.5 查询的合并列
* mysql> select name,age,(english+chinese) as ‘总成绩’ from student;
* 请注意:同类型的字段才可以进行合并!
*
* 1.6 查询时去掉重复的记录
* mysql> select distinct * from student;
*
* 2.1 带条件查询(where)
*
* 2.1.1 逻辑条件(and[与] or[或])
* 查询id=2,并且姓名jim的学生
* mysql> select * from student where id=2 and name=’jim’;
*
* 查询id=2,或姓名lily的学生
* mysql> select * from student where id=2 or name=’lily’;
*
* 2.1.2 比较查询(> < >= <= = <> between and)
* 查询english的分数大于79的学生
* mysql> select * from student where english>=79;
*
* 查询english的分数大于79并且小于90的学生
* mysql> select * from student where english>=79 and english<80;
*
* mysql> select * from student where english between 70 and 90;
*
* mysql> select * from student where id<>1;
*
* 2.1.3 判空条件(null 字符串) is null / is not null / <>”
* 查询地址为空的学生
* mysql> select * from student where address is null;
*
*
*
* mysql> select * from student where name <>”;
+——+——–+——–+——+———+———+———+
| id | name | gender | age | english | chinese | address |
+——+——–+——–+——+———+———+———+
| 1 | alex | 1 | 20 | 89 | 90 | cisuag |
| 2 | jim | 1 | 10 | 79 | 97 | cisuag |
| 3 | lily | 0 | 20 | 69 | 37 | cisuag |
| 5 | hwadee | NULL | NULL | NULL | NULL | NULL |
+——+——–+——–+——+———+———+———+
4 rows in set (0.00 sec)
mysql> select * from student where name='';
+------+------+--------+------+---------+---------+---------+
| id | name | gender | age | english | chinese | address |
+------+------+--------+------+---------+---------+---------+
| 6 | | NULL | NULL | NULL | NULL | NULL |
+------+------+--------+------+---------+---------+---------+
1 row in set (0.00 sec)
2.1.4 模糊查询 (like)
通常的替换符的两个: % _
%: 表示任意多个字符
_: 表示的是一个字符
查询姓名含有'l'的学生
mysql> select * from student where name like '%l%';
+------+------+--------+------+---------+---------+---------+
| id | name | gender | age | english | chinese | address |
+------+------+--------+------+---------+---------+---------+
| 1 | alex | 1 | 20 | 89 | 90 | cisuag |
| 3 | lily | 0 | 20 | 69 | 37 | cisuag |
+------+------+--------+------+---------+---------+---------+
查询姓名中第二个字符是l的学生
mysql> select * from student where name like '_l%';
+------+-------+--------+------+---------+---------+---------+
| id | name | gender | age | english | chinese | address |
+------+-------+--------+------+---------+---------+---------+
| 1 | alex | 1 | 20 | 89 | 90 | cisuag |
| 7 | hleeo | NULL | NULL | NULL | NULL | NULL |
+------+-------+--------+------+---------+---------+---------+
2 rows in set (0.00 sec)
3、聚合查询
常用的聚合函数: sum() avg() max() min() count()
查询学生english的总成绩
mysql> select sum(english) as '英语总成绩' from student;
+------------+
| 英语总成绩 |
+------------+
| 237 |
+------------+
1 row in set (0.00 sec)
查询学生english的平均成绩
mysql> select avg(english) as '英语平均成绩' from student;
+--------------+
| 英语平均成绩 |
+--------------+
| 79 |
+--------------+
1 row in set (0.00 sec)
查询english的最高分
mysql> select max(english) as '最高分' from student;
+--------+
| 最高分 |
+--------+
| 89 |
+--------+
1 row in set (0.00 sec)
查询english的最低分
mysql> select min(english) as '最低分' from student;
+--------+
| 最低分 |
+--------+
| 69 |
+--------+
1 row in set (0.00 sec)
统计当前有多少学生参加english考试
mysql> select count(english) from student;
+----------------+
| count(english) |
+----------------+
| 3 |
+----------------+
1 row in set (0.00 sec)
count函数是不统计null的数据
*/
public class Demo2 {
}
/*
* 分页查询(limit 起始行,查询几行)
假设一个8个数据,将数据分4页,每页两条数据
select * from 表 limit (当前页-1)*每页显示的数量,每页显示的数量。
mysql> select * from student limit 0,2; 注意索引值是从0开始的!!!
+——+——+——–+——+———+———+———+
| id | name | gender | age | english | chinese | address |
+——+——+——–+——+———+———+———+
| 1 | alex | 1 | 20 | 89 | 90 | cisuag |
| 2 | jim | 1 | 10 | 79 | 97 | cisuag |
+——+——+——–+——+———+———+———+
2 rows in set (0.00 sec)
mysql> select * from student limit 2,2; (2=(2-1)*2)
+——+——–+——–+——+———+———+———+
| id | name | gender | age | english | chinese | address |
+——+——–+——–+——+———+———+———+
| 3 | lily | 0 | 20 | 69 | 37 | cisuag |
| 5 | hwadee | NULL | NULL | NULL | NULL | NULL |
+——+——–+——–+——+———+———+———+
2 rows in set (0.00 sec)
mysql> select * from student limit 4,2; (4=(3-1)*2)
+——+——-+——–+——+———+———+———+
| id | name | gender | age | english | chinese | address |
+——+——-+——–+——+———+———+———+
| 6 | | NULL | NULL | NULL | NULL | NULL |
| 7 | hleeo | NULL | NULL | NULL | NULL | NULL |
+——+——-+——–+——+———+———+———+
2 rows in set (0.00 sec)
mysql> select * from student limit 6,2; (6=(4-1)*2)
+——+——–+——–+——+———+———+———+
| id | name | gender | age | english | chinese | address |
+——+——–+——–+——+———+———+———+
| 7 | ihleeo | NULL | NULL | NULL | NULL | NULL |
+——+——–+——–+——+———+———+———+
1 row in set (0.00 sec)
/*
* 查询排序:(order by)
* 查询以姓名的降序(desc)排序输出 升序(asc): 默认
* mysql> select * from student order by name desc;
+——+——–+——–+——+———+———+———+
| id | name | gender | age | english | chinese | address |
+——+——–+——–+——+———+———+———+
| 3 | lily | 0 | 20 | 69 | 37 | cisuag |
| 2 | jim | 1 | 10 | 79 | 97 | cisuag |
| 7 | ihleeo | NULL | NULL | NULL | NULL | NULL |
| 5 | hwadee | NULL | NULL | NULL | NULL | NULL |
| 7 | hleeo | NULL | NULL | NULL | NULL | NULL |
| 1 | alex | 1 | 20 | 89 | 90 | cisuag |
| 6 | | NULL | NULL | NULL | NULL | NULL |
+——+——–+——–+——+———+———+———+
7 rows in set (0.00 sec)
mysql> select * from student order by name;
+——+——–+——–+——+———+———+———+
| id | name | gender | age | english | chinese | address |
+——+——–+——–+——+———+———+———+
| 6 | | NULL | NULL | NULL | NULL | NULL |
| 1 | alex | 1 | 20 | 89 | 90 | cisuag |
| 7 | hleeo | NULL | NULL | NULL | NULL | NULL |
| 5 | hwadee | NULL | NULL | NULL | NULL | NULL |
| 7 | ihleeo | NULL | NULL | NULL | NULL | NULL |
| 2 | jim | 1 | 10 | 79 | 97 | cisuag |
| 3 | lily | 0 | 20 | 69 | 37 | cisuag |
+——+——–+——–+——+———+———+———+
7 rows in set (0.00 sec)
如果对多个字段进行排序,那么按照字段的先后顺序进行
mysql> select * from student order by name desc,age asc;
+——+——–+——–+——+———+———+———+
| id | name | gender | age | english | chinese | address |
+——+——–+——–+——+———+———+———+
| 3 | lily | 0 | 20 | 69 | 37 | cisuag |
| 2 | jim | 1 | 10 | 79 | 97 | cisuag |
| 7 | ihleeo | NULL | NULL | NULL | NULL | NULL |
| 5 | hwadee | NULL | NULL | NULL | NULL | NULL |
| 7 | hleeo | NULL | NULL | NULL | NULL | NULL |
| 1 | alex | 1 | 20 | 89 | 90 | cisuag |
| 6 | | NULL | NULL | NULL | NULL | NULL |
+——+——–+——–+——+———+———+———+
7 rows in set (0.00 sec)
*/
/*
* 分组查询:(group by )
* 查询男生和女生各有多少人。
* 查询结果类似于:
* 男: 10
* 女: 4
*
* 1、先对学生按照性别进行分组
* mysql> select * from student group by gender;
+——+——+——–+——+———+———+———+
| id | name | gender | age | english | chinese | address |
+——+——+——–+——+———+———+———+
| 3 | lily | 0 | 20 | 69 | 37 | cisuag |
| 1 | alex | 1 | 20 | 89 | 90 | cisuag |
+——+——+——–+——+———+———+———+
2 rows in set (0.00 sec)
mysql> select gender,count(*) from student group by gender;
+--------+----------+
| gender | count(*) |
+--------+----------+
| 0 | 1 |
| 1 | 3 |
+--------+----------+
2 rows in set (0.00 sec)
查询出来性别为0的有1个人,性别为1的有3个人
2、 分组查询后再进行筛选(group by 和 having 联合使用):
查询出人数大于2的性别。
mysql> select gender, count(*) from student group by gender having count(*)>2 ;
+--------+----------+
| gender | count(*) |
+--------+----------+
| 1 | 3 |
+--------+----------+
1 row in set (0.00 sec)
*
*
* mysql> select gender, count() from student group by gender having(count()>2) ;
+--------+----------+
| gender | count(*) |
+--------+----------+
| 1 | 4 |
+--------+----------+
1 row in set (0.00 sec)
mysql> select gender, count(*) from student group by gender having(count(*))>2 ;
+--------+----------+
| gender | count(*) |
+--------+----------+
| 1 | 4 |
+--------+----------+
1 row in set (0.00 sec)
/*
* 把一个数据库中的数据复制到另外一个数据库中,但表的结构要一样。
*
* 不同的数据库语法是不一样的(Oracle SQL server Mysql等)
*
* 一、如果目标的表是存在的时候
* 语法: insert into 目标表 select * from 源表;
*
* 1、表结构相同,也在同一个数据库中
* mysql> desc student;
+———+————–+——+—–+———+——-+
| Field | Type | Null | Key | Default | Extra |
+———+————–+——+—–+———+——-+
| id | int(11) | YES | | NULL | |
| name | varchar(30) | YES | | NULL | |
| gender | varchar(2) | YES | | NULL | |
| age | int(11) | YES | | NULL | |
| english | varchar(30) | YES | | NULL | |
| chinese | varchar(30) | YES | | NULL | |
| address | varchar(300) | YES | | NULL | |
+———+————–+——+—–+———+——-+
7 rows in set (0.00 sec)
mysql> create table stu(
-> id int,
-> name varchar(30),
-> gender varchar(2),
-> age int,
-> english varchar(30),
-> chinese varchar(30),
-> address varchar(300)
-> );
Query OK, 0 rows affected (0.08 sec)
mysql> select * from stu;
Empty set (0.00 sec)
mysql> select * from student;
+------+-----------+--------+------+---------+---------+---------+
| id | name | gender | age | english | chinese | address |
+------+-----------+--------+------+---------+---------+---------+
| 1 | alex | 1 | 20 | 89 | 90 | cisuag |
| 2 | jim | 1 | 10 | 79 | 97 | cisuag |
| 3 | lily | 1 | 20 | 69 | 37 | cisuag |
| 5 | hwadee | 0 | NULL | 60 | 80 | NULL |
| 6 | hwadee001 | 1 | NULL | 70 | 90 | NULL |
+------+-----------+--------+------+---------+---------+---------+
5 rows in set (0.00 sec)
(重)mysql> insert into stu select * from student; (完全复制,源表重复的记录还是会再复制的)
Query OK, 5 rows affected (0.03 sec)
Records: 5 Duplicates: 0 Warnings: 0
mysql> select * from stu;
+------+-----------+--------+------+---------+---------+---------+
| id | name | gender | age | english | chinese | address |
+------+-----------+--------+------+---------+---------+---------+
| 1 | alex | 1 | 20 | 89 | 90 | cisuag |
| 2 | jim | 1 | 10 | 79 | 97 | cisuag |
| 3 | lily | 1 | 20 | 69 | 37 | cisuag |
| 5 | hwadee | 0 | NULL | 60 | 80 | NULL |
| 6 | hwadee001 | 1 | NULL | 70 | 90 | NULL |
+------+-----------+--------+------+---------+---------+---------+
5 rows in set (0.00 sec)
*
*
* (重)mysql> insert into stu select * from student; (完全复制,源表中的重复的记录不再复制)
*
* mysql> insert into stu select distinct * from student;
Query OK, 5 rows affected (0.04 sec)
Records: 5 Duplicates: 0 Warnings: 0
mysql> insert into stu select distinct * from student;
Query OK, 5 rows affected (0.03 sec)
Records: 5 Duplicates: 0 Warnings: 0
1、表结构相同,不在同一个数据库中(在表的前面加上数据库的名称)
mysql> use day27;
Database changed
mysql> show tables;
+-----------------+
| Tables_in_day27 |
+-----------------+
| teacher |
+-----------------+
1 row in set (0.00 sec)
mysql> create table stu(
-> id int,
-> name varchar(30),
-> gender varchar(2),
-> age int,
-> english varchar(30),
-> chinese varchar(30),
-> address varchar(300)
-> );
Query OK, 0 rows affected (0.07 sec)
mysql> show tables;
+-----------------+
| Tables_in_day27 |
+-----------------+
| stu |
| teacher |
+-----------------+
2 rows in set (0.00 sec)
mysql> select * from stu;
Empty set (0.00 sec)
//全复制源表中的重复项
mysql> insert into day27.stu select * from day28.student;
Query OK, 6 rows affected (0.03 sec)
Records: 6 Duplicates: 0 Warnings: 0
mysql> select * from stu;
+------+-----------+--------+------+---------+---------+---------+
| id | name | gender | age | english | chinese | address |
+------+-----------+--------+------+---------+---------+---------+
| 1 | alex | 1 | 20 | 89 | 90 | cisuag |
| 2 | jim | 1 | 10 | 79 | 97 | cisuag |
| 3 | lily | 1 | 20 | 69 | 37 | cisuag |
| 5 | hwadee | 0 | NULL | 60 | 80 | NULL |
| 6 | hwadee001 | 1 | NULL | 70 | 90 | NULL |
| 1 | alex | 1 | 20 | 89 | 90 | cisuag |
+------+-----------+--------+------+---------+---------+---------+
6 rows in set (0.00 sec)
//不复制源表是的重复数据项
mysql> insert into day27.stu select distinct * from day28.student;
*/