First, the basic command
1. Start service (as an administrator enter cmd):
Format: net start service name
Example: net start mysql
2. Stop the service (as an administrator enter cmd):
Format: net stop service name
Example: net stop mysql
3. Connect database
Format: mysql -u username -p
Example: mysql -u root -p
Enter the password (setting at installation)
4. Log (disconnected):
quit; or exit;
5. View version
Example: select version ();
6. Check Time
Example: select now ();
7. remote connection:
Format: mysql -h ip address -u user name -p
Enter the other party mysql password
# Ajax and mysql interactive experiments
8. execute sql file (source command)
. 1) is first switched to the next to be executed to the database;
2) .source + sql file is located under the path
Example:
use student;
source E:\JavaEE\Apache_tomcat9\webapps\pic_num_ajax\WEB-INF\lib\book.sql
Second, the database operation
1. Create a database
Format: create database database name of the charset = utf8;
实例:create database kaka charset=utf8;
2. Delete the database
Format: drop database database name;
Example: drop database kaka;
3. Switch Database
Format: use database name;
示例: use kaka;
4. Check the database currently in use
Format: select database ();
Third, the operating table
1. Look around the table in the current database
Format: show tables;
2. Create a table
Format: create table table name (column and type)
Description: auto_increment show growth from
primary key represents the primary key
not null means no empty
isDelete bit default 0: represents the tombstone, 1 if the data is deleted
示例:create table kaka(id int auto_increment primary key,
name varchar(20) not null,age int not null,gender bit default 1,
address varchar(20),isDelete bit default 0);
3. Delete table
Format: drop table table name;
Example: drop table kaka;
4. Check the table structure
Format: desc table name;
Example: desc kaka;
5. Review the construction of the table statement
Format: show create table table name;
Example: show create table kaka;
6. Heavy naming
Format: rename table the original table to the new table name;
实例: rename table kaka to Jack;
7. Modify Table Structure
Format: alter table table name add | change | drop listed types;
示例:alter table kaka add isDelete bit default 0;
(Assumptions: kaka table did not isDelete column, after not propose to amend the table structure)
Fourth, data manipulation
1. increase
. A full row insertion:
Format: insert into table values (...);
Description: primary key column is automatically increased, but requires stations when fully inserted into the column,
Usually 0, the success of the actual data inserted prevail
Example: insert into kaka values (0, "kaka", 19,1, "Beijing", 0);
. B Default inserted:
格式:insert into 表名(列1,...列2,...) values(值1,值2,...)
示例:insert into kaka(name,age,address) values("李军",22,"西宁");
c.同时插入多条数据:
格式:insert into 表名 values(...),(...),...;
示例:insert into kaka values(0,"jiege",21,1,"南京",0),
(0,"huatian",21,1,"天津",0),(0,"xiaohong",21,0,"南京",0);
2.删
格式:delete from 表名;(全删) # 慎用
delete from 表名 where 条件;
示例:delete from kaka where id = 4;
3.改
格式:update 表名 set 列1=值1,列2=值2,...where 条件;
示例:update kaka set age=17 where id=2;
注意:如果没有条件,则全部age列都改为17,慎用!!!
4.查
说明:查询表中的全部数据
格式:select * from 表名;
示例:select * from kaka;
五、查
1.基本语法
格式:select * from 表名;
说明:a.from 关键字后面是表名,表示数据来源于这张表
b.select后面写表中的列名,如果是*表示在结果集中显
示表中所有的列
c.在select后面的列名部分,可以使用as为列名起别名,
这个别名显示在结果集中
d.如果要查询多个列,之间使用逗号分隔
示例:select from * kaka;
select name,age from kaka;
# 给列起的别名'a'
select name as a,age from kaka;
2.消除重复行
说明:在select后面列前面使用distinct可以消除重复行
示例:select gender from kaka;
select distinct gender from kaka;
3.条件查询
a.语法
select * from 表名 where 条件
b.比较运算符
等于 =
大于 >
小于 <
大于等于>=
小于等于<=
不等于 !=或<>
需求:查询id值大于3的数据
示例:select * from kaka where id>3;
c.逻辑运算符
and 并且
or 或者
not 非
需求:查询id值大于3的女同学的数据
示例:select * from kaka where id>3 and gender=0;
d.模糊查询
insert into kaka values(0,"张恒",22,1,"南京",0),
(0,"张三",20,1,"天津",0),(0,"张天宝",19,0,"南京",0);
like:
%:表示任意多个任意字符
_:表示任意一个字符
需求:查询姓张的所有学生
示例:select * from kaka where name like "张%";
select * from kaka where name like "张_";
e.范围查询
in 表示在一个非连续的范围内
between...and...表示在一个连续的范围内
需求:查询id号为2、4、6的学生
示例:selec * from kaka where id in (2,4,6);
需求:查询编号为6-8的学生
示例:select * from kaka where id between 6 and 8;
f.判断空
insert into kaka(name,age) values("特朗普",22);
注意:null与""是不同的
判断空:is null
判断非空:is not null
需求:查询没有地址的同学
实例:select * from kaka where address is null;
需求:查询有地址的同学
实例:select * from kaka where address is not null;
g.优先级
由高到低:(),not,比较运算符,逻辑运算符
and高于or,如果同时出现并且希望先选or,这是需要结合()来使用
4.聚合
为了快速统计数据,提供了5个聚合函数
a.count(*)表示极端总行数,括号可以写*和列名
需求:查询学生总数
示例:select count(*) from kaka;
b.max(列)表示求此列的最大值
需求:查询女生编号的最大值
示例:select max(id) from kaka where gender=0;
c.min(列)表示求此列的最小值
需求:查询女生编号的最小值
示例:select min(id) from kaka where gender=0;
d.sum(列)表示求此列的和
需求:查询女生的年龄和
示例:select sum(age) from kaka where gender=0;
e.avg(列)表示求此列的平均值
需求:查询女生年龄的平均值
示例:select avg(age) from kaka where gender=0;
5.分组
按照字段分组,表示此字段相同的数据会被放到一个集合。
分组后,只查询出相同的数据列,对于有差异的数据列无法
显示在结果中可以对分组后的数据进行统计,做聚合运算
语法:select 列1,列2,聚合...from 表名 group by 列1,
列2,列3,...
需求:查询男女(分组)生总数
示例:select gender,count(*) from kaka group by gender;
分组后的数据筛选:select 列1,列2,聚合...from 表名 group by 列1,
列2,列3,...having 列1,...聚合...
示例:select gender,count(*) from kaka group by gender
having gender;
where 与 having 的区别:
where:对原始数据的筛选,对from后面指定的进行筛选
having:对group by筛选出的集合再进行筛选
6.排序
语法:select * from 表名 order by 列1 asc|desc,列2 asc|desc,
...
说明:
a.将数据按照列1进行排序,如果某些列1的值相同,则按照列2
进行排序
b.默认按照升序排
c.asc升序
d.desc降序
需求:按年龄排序
示例:select * from kaka order by age;
需求:将没有被删除的数据按年龄排序
示例:select * from kaka isDelete=0 order by age desc;
7.分页
语法:select * from 表名 limit start ,count;
说明:start 索引从0开始
示例:select * from kaka limit 0,3;
select * from kaka where gender=1 limit 0,3;
六、关联
创建表语句:
1.create table class(id int auto_increment primary key,
name varchar(20) not null,stuNum int not null);
2.create table kakas(id int auto_increment primary key,
name varchar(20) not null,age int not null,gender bit
default 1,classid int not null,foreign key(classid)
references class(id));
插入一些数据:
insert into class values(0,"python01",60),(0,"python02",60),
(0,"python03",60);
insert into kakas values(0,"Tom",23,1,1);
insert into kakas values(0,"lili",22,0,2);
insert into kakas values(0,"Jack",18,1,2);
# inner join:表示关联
select kakas.name,class.name from class inner join
kakas on class.id=kakas.classid;
select kakas.name,class.name from class left join
kakas on class.id=kakas.classid;
select kakas.name,class.name from class right join
kakas on class.id=kakas.classid;
分类:
1.表A inner join 表B;
表A与表B匹配的行会出现在结果集中
2.表A left join 表B;
表A与表匹配的行会出现在结果集中,外加表A中独有的
数据,未对应的数据使用null填充
3.表A right join 表B;
表A与表匹配的行会出现在结果集中,外加表B中独有的
数据,未对应的数据使用null填充