打开数据库:mysql -u root -p
数据库路径:D:\mysql-5.6.41-winx64\bin(我存放在这)
查看当前数据库:show databases;
创建数据库:create database xx;
删除数据库:drop database xx;
使用数据库:use xx
DB由很多table组成,table由很多不同数据行组成,数据列有不同数据类型
三种主要数据类型:
文本类:varchar,text,longtext
数字类:tinyint,int,bigint ,double
日期类:date,datetime
增删数据表table:
创建table:create table account(
id bigint(20),
createtime datetime,
ip varchar(255)
);
删除table
drop table xx;
查看table详细结构
describe account;
查看数据库有多少个table
show tables;
给数据库添加或删除列:
增加列:alter table [table_name] add [column_name] [data_type]
[not null//不为空] [default+默认值]
删除列:alter table [table_name] drop [column_name]
修改某个数据列的名字或者数据类型:
alter table [table_name] change [old_column_name] [new_column_name] [data_type]
修改表名:alter table [table_name] rename [new_table_name]
插入与查看表数据:
查看表数据:select * from table_name;
select col_name,col_name2,...from table_name;
插入数据:insert into [table_name] values(值1,值2,...)
insert into [table_name] (列1,列2,...) values(值1,值2,...)
where条件查询:
select * from table_name where col_name 运算符 值;//运算符有比较符号,between(在两个值范围内),like(按某模式查找)
可以组合筛选,利用and和or
where的null判断:(不能使用=或!=判断)
select * from table_name where col_name is (not) null
select distinct去除重复查询结果:
select distinct col_name from table_name
order by 对查询结果排序:
按照单一列名排序——select * from table_name [where 子句] order by col_name [asc#升序排序/desc#降序排序]//默认升,where与order by不能换位置
按多列排——与上不同就是这个要加上所需排序的多个列在后面
limit截取查询结果:
select * from table_name [where子句] [order by 子句] limit[offset,]rowcount
offset:查询起始位置 rowcount:从offset开始获取的记录条数
insert into 与select组合使用:
insert into [表名一]select 列一,列二 from [表名2]
insert into [表名一] (列一,列二)select 列三,列四 from [表名2]
undate 表数据:
修改单项:
update 表名 set 列名=xxx【where 字句】
修改多项:
updata 表名 set 列名1=xxx,列名2=xxx...[where字句]
where 的in:
select * from 表名 where 列名 in (value1,value2...)
select * from 表名 where 列名 in (select 列名 from 表名)
where 的between:
select * from 表名 where 列名 between 值1 and 值2
select * from 表名 where 列名 not between 值1 and 值2
where 的like(通常用在字符串模糊匹配):
select * from 表名 where 列名 [not] like pattern
pattern:匹配模式,比如‘abc’‘%abc’‘abc%’
%是一个通配符,理解上可以把他当成字符串



