MySQL小记 MySQL数据库--指令


开启、关闭服务

  • 启动服务
    net start mysql8
    net start mysql57
  • 使用账户密码进入 MySQL
    mysql -u root -p
  • 关闭服务
    net stop mysql8

操作——>数据库

操作实现
创建数据库create database db_name;
选择数据库use db_name;
查看数据库show databases;
删除数据库drop database db_name;

创建数据库

create database 数据库名;
命名规则:

  • 任意:字母、数字、_$。皆可作为开头
  • 不能纯数字
  • 大小写不敏感(Windows)。Linux大小写敏感
  • 最长64个字符

选择数据库

use 数据库名;

  • 选择之后才可以对其进行操作

查看数据库

show databases

  • 查看所有

删除数据库

drop database 数据库名;


数据类型

数字类型

  • 整数
类型范围说明单位
TINYINT-128~127; 0~255最小整数1字节
BIT-127~127;0~255最小整数1字节
BOOL-127~127;0~255最小整数1字节
SMALLINT-32768~32767;0~65535小型整数2字节
MEDIUMINT-8388608~8388607;0~16777215中型整数3字节
INT-2147483648~2147483647;
0~4294967295
标准整数4字节
BIGINT-9223372036854775808~9223372036854775807;
0~18446744073709551615
大型整数8字节
  • 浮点数
类型范围单位
FLOAT±3.402823466E388字节或4字节
DOUBLE±1.7976931348623157E308
±2.225073858502014E-308
8字节
DECIMAL可变自定义长度

字符串类型

  • 普通文本字符串
类型范围说明
[national]
char(M)
[binary | ASCII | unicode]
0-255个字符字符串长度固定为M
national 指定默认字符集。
binary 指定是否区分大小写。
ASCII 指定使用latin1字符集。
unicode 指定使用UCS字符集。
char0-255个字符与char(M)类似
[national]
varchar(M)
[binary]
0-255个字符长度可变,其余与char(M)类似
  • 可变类型
类型范围(字符数)
TINYBLOB1-255
TINYTEXT1-255
BLOM1 ~ (2^16-1)
TEXT1 ~ (2^16-1)
MEDIUNBLOB1 ~ (2^24-1)
MEDIUMTEXT1 ~ (2^24-1)
LONGBLOB1 ~ (2^32-1)
LONGTEXT1 ~ (2^32-1)
  • 特殊类型
类型最大值说明
Enum(“value”, “value2”,…)65535只可容纳所列值之一或NULL
Set(“value”,“value2”,…)64可以容纳一组值或NULL

选择原则

  • 速度。选固定列,CHAR
  • 空间。动态列,VARCHAR
  • 要将列中内容限制在一种选择,ENUM
  • 允许一列中多个条目,SET
  • 搜索内容不区分大小写,TEXT
  • 搜索内容区分大小写,BLOB

日期和时间类型

类型取值范围
DATE1000-01-01 9999-12-31
TIME-838:58:59 835:59:59
DATETIME1000-01-01 00:00:00
9999-12-31 23:59:59
TIMESTAMP1970-01-01 00:00:00
2037年某个时间
YEAR1901-2155

操作——>表

操作说明
create table table_name(
  列名1 属性1,…)
创建表
show columns from db.table
show columns from table from db
察看表结构
alter table _table cmd修改表结构
drop table _table
drop table if exists _table
删除数据表

创建表

// 示例
create table my_table{
--> id int(8) auto_increment primary key,
--> username varchar(30) not null,
--> password varchar(20) not null,
--> creattime datetime);

语法:

create[TEMPORARY] table [if not exists]
[(create_definition, ...)] [table_options] [select_statement]
参数说明
TEMPRORART表示创建临时表
IF NOT EXISTS避免表存在时报错
create_definition表的列属性。创建时至少包含1列
table_options表的一些特性参数
select_statementSELECT 语句描述部分。
可以快速创建表
  • create_definition
col_name type [NOT NULL | NULL] [DEFAULT default_value] [AUTO_INCREMENT]
		[PRIMARY KEY] [reference_definition]
参数说明
col_name字段名【列名】
type字段(数据)类型
NOT NULL | NULL该列是否允许空值(默认允许)。0和空格不算空值。
DEFALUT default_value默认值
AUTO_INCREMENT是否自动编号。每个列表只能有一列
PRIMARY KEY是否为主键。一个表只能有一个主键
reference_definition为字段添加注释

查找表

  • show
    show [full] colmuns from _table [from _db];
    或者
    show [full] colmuns from _db._table;
  • describe 可以简写为 desc
    describe _table;
    或者
    describe _table _colum;
mysql> desc _mytable

修改表

alter [IGNORE] table _table alter_spec[,alter_spec]...

alter_spec 可以有多个,由 , 分开


  • 添加新字段

add [column] create_definition [first | after column_name]

alter table _table 
	add address varchar(50) unicode;

  • 添加索引名称

add index [index_name] (index_col_name, ...)


  • 添加主键名称

add primary key (index_col_name, ...)


  • 添加唯一索引

add unique [index_name] (index_col_name,...)


  • 修改字段名

alter [column] col_name {set default | drop default}


  • 修改字段类型

change [column] old_col_name create_definition

alter table _table
	change column
	id fid int(30) not null auto_increment primary key;

  • 修改子句定义字段

modify [column] create_definition

alter table _table
	modify age int(2) not null default 0;

  • 删除字段

drop [column] col_name

alter table _table
	drop address;

  • 删除主键名称

drop primary key


  • 删除索引名称

drop index index_name


  • 更改表名

rename [as] new_tbl_name

alter table _table 
   rename mytable;

table_option


删除表

drop table [if exists] _table;

操作——>记录

操作说明
insert into _table (cols,…) values(vals,…);
insert into _table values(vals,…);
插入记录
select (cols,…) from _table where condition;
select * from table;
查询
update _table set
   col_1=val_1, col_2=val_2,…
   where codition;
修改记录
delete from _table where condition;删除记录

添加记录

insert into _table(clo_1, clo_2,...)
	values(val_1, val_2,...);

或者

insert into _table values(val_1, val_2,...);

查找记录

select selection_list from _table where condition;
select * from _table;

例如:

select id,name from _table where name='Fry';

修改记录

update _table set 
	col_name=new_value,
	col_name2=new_value2,...
	where condition;

例如:

update _table set
	name='Bender',
	address='USA'
	where id=1001;

删除记录

delete from _table where condition;

例如:

delete from _table where id=1001;

详细查找

复杂 select 语法:

语法说明
select selection_list选择要查询的列
from _table指定数据表
where primary_condition查询时必须满足的条件
group by grouping_columns对结果的分组规则
order by sorting_columns对结果的排序规则
having secondary_constraint查询时满足的第二条件
limit count限定输出的记录数量

selection_list

  • * 查询所有字段
  • 多个字段,用 , 分开
select * from _table;
select id,name from _table;

table_list

  • 多个表间用 , 分隔,表名.字段名
select _table.id mytable.name, age
	from _table, mytable
	where _table.id = mytable.id 
	and _table.age=18;

where

条件示例
=id=10
>id>0
<id<0
>=id>=10
<=id<=10
!=id!=10
is nullid is null
is not nullid is not null
betweenid between 1 and 10010
inid in (10010, 10011)
not inid not in (10010, 10011)
likename like(‘%ar%’)
not likename not like(‘%ar%’)
regexpname regexp ‘F.+’   正则表达式

like 运算符

  • 模糊查询
  • % 匹配0或多个字符
  • _ 匹配0或一个字符
select id,name from _table
	where name like('%ar%');

类似:Mark, Far, array, ar…都可以被匹配


distinct

  • 去除结果中重复行
select distinct name from _table;

concat() 函数

  • 联合多个字段,构成一个字符串
select id, concat(name,":", age) as info, job 
	from _table;
  • 结果中将会出现一列 info 形式: name: age

使用函数、表达式

名称说明
avg(col_name)平均值
count(col_name)统计非空字段。前有 distinct 则不统计重复。count(*) 统计包含空值
min(col_name)最小
max(col_name)最大
std(col_name)标准背离值
stdtev(col_name)与std相同
sum(col_name)总和
select *, (age * 0.9) as '年轻10%' from _table;
  • age * 0.9 计算年龄90%
  • as 更改显示名称

group by

  • 通常与 avg(), sum() 一起使用,能发挥最大作用
select avg(age),name from _table group by name;
  • 上述语句可以列出表中:同名者的平均年龄,以及姓名

排序

  • 默认升序
  • 降序:order by col_name desc

按 id 降序

select * from _table order by id desc;

having

  • 对查询结果进一步筛选
  • where 在分组前应用
  • having 在分组后应用
select id, avg(age), nama from _table
	group by name
	having avg(age)>18;

limit 子句

  • 限制查询输出条数
select id, name, age from _table
	where age>18
	limit 5;
  • 返回5条age>18的信息

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

薛定谔的壳

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值