一、数据操作

 

SRE实战 互联网时代守护先锋,助力企业售后服务体系运筹帷幄!一键直达领取阿里云限量特价优惠。

1.显示数据库

1 show databases;

2.创建数据库

1 #utf-8
2 create database 数据库名称 default charset utf8 collate  utf8_general_ci;

3 #gbk
4 create datahase 数据库名称 default charset utf8 gbk collate gbk_chinese_ci;

3.使用数据库

1 use 数据库名称

4.用户管理

 1 1.创建用户
 2     create user '用户名'@'IP地址' identified by ‘密码’;
 3 2.删除用户
 4     drop user '用户名'@ 'IP地址;
 5 3.修改用户
 6     rename user '用户名'@ 'IP地址'; to '新用户'@ 'IP地址;
 7 4.权限管理
 8 查看权限:show grant for ‘用户’@’IP地址’;
 9 授权:grant 权限 on 数据库.表 to ‘用户’@’IP地址’;
10 取消授权:revoke 权限 on 数据库.表. from ‘用户’@’IP地址’;
11 出grant外所有的权限:all privileges
12 插入权限:select,insert

特殊情况:忘记密码

1 # 启动免授权服务端
2 mysqld --skip-grant-tables
3 
4 # 客户端
5 mysql -u root -p
6 
7 # 修改用户名密码
8 update mysql.user set authentication_string=password('666') where user='root';
9 flush privileges;

二、数据表基本操作

1.创建表

 1 create table 表名(
 2     列名 类型 是否可以为空 auto_increment primary key,
 3     列名 类型 是否可以为空
 4 )engine=ioondb, default chartset=utf8;
 5 或者
 6 create table 表名(
 7     列名 类型 是否可以为空 ,
 8     列名 类型 是否可以为空,
 9     primary key(列名,列名)
10 )engine=ioondb, default chartset=utf8;

 

2.删除表

1 drop table 表名;

 3.清空表

1 delete from 表名;
2 truncate table 表名;

4.修改表

 1 添加列:alter table 表名 add 列名 类型
 2 删除列:alter table 表名 drop column 列名
 3 修改列:
 4         alter table 表名 modify column 列名 类型;  -- 类型
 5         alter table 表名 change 原列名 新列名 类型; -- 列名,类型
 6   
 7 添加主键:
 8         alter table 表名 add primary key(列名);
 9 删除主键:
10         alter table 表名 drop primary key;
11         alter table 表名  modify  列名 int, drop primary key;
12   
13 添加外键:alter table 从表 add constraint 外键名称(形如:FK_从表_主表) foreign key 从表(外键字段) references 主表(主键字段);
14 删除外键:alter table 表名 drop foreign key 外键名称;
15   
16 修改默认值:ALTER TABLE testalter_tbl ALTER i SET DEFAULT 1000;
17 删除默认值:ALTER TABLE testalter_tbl ALTER i DROP DEFAULT;

5.基本数据类型

MySQL的数据类型大致分为:数值、时间和字符串

MySQL数据库操作(一) Mysql 第1张
  1   bit[(M)]
  2             二进制位(101001),m表示二进制位的长度(1-64),默认m=1
  3 
  4         tinyint[(m)] [unsigned] [zerofill]
  5 
  6             小整数,数据类型用于保存一些范围的整数数值范围:
  7             有符号:
  8                 -128127.
  9             无符号:
 10                 0255
 11 
 12             特别的: MySQL中无布尔值,使用tinyint(1)构造。
 13 
 14         int[(m)][unsigned][zerofill]
 15 
 16             整数,数据类型用于保存一些范围的整数数值范围:
 17                 有符号:
 18                     -21474836482147483647
 19                 无符号:
 20                     04294967295
 21 
 22             特别的:整数类型中的m仅用于显示,对存储范围无限制。例如: int(5),当插入数据2时,select 时数据显示为: 00002
 23 
 24         bigint[(m)][unsigned][zerofill]
 25             大整数,数据类型用于保存一些范围的整数数值范围:
 26                 有符号:
 27                     -92233720368547758089223372036854775807
 28                 无符号:
 29                     018446744073709551615
 30 
 31         decimal[(m[,d])] [unsigned] [zerofill]
 32             准确的小数值,m是数字总个数(负号不算),d是小数点后个数。 m最大值为65,d最大值为30。
 33 
 34             特别的:对于精确数值计算时需要用此类型
 35                    decaimal能够存储精确值的原因在于其内部按照字符串存储。
 36 
 37         FLOAT[(M,D)] [UNSIGNED] [ZEROFILL]
 38             单精度浮点数(非准确小数值),m是数字总个数,d是小数点后个数。
 39                 无符号:
 40                     -3.402823466E+38 to -1.175494351E-38,
 41                     0
 42                     1.175494351E-38 to 3.402823466E+38
 43                 有符号:
 44                     0
 45                     1.175494351E-38 to 3.402823466E+38
 46 
 47             **** 数值越大,越不准确 ****
 48 
 49         DOUBLE[(M,D)] [UNSIGNED] [ZEROFILL]
 50             双精度浮点数(非准确小数值),m是数字总个数,d是小数点后个数。
 51 
 52                 无符号:
 53                     -1.7976931348623157E+308 to -2.2250738585072014E-308
 54                     0
 55                     2.2250738585072014E-308 to 1.7976931348623157E+308
 56                 有符号:
 57                     0
 58                     2.2250738585072014E-308 to 1.7976931348623157E+308
 59             **** 数值越大,越不准确 ****
 60 
 61 
 62         char (m)
 63             char数据类型用于表示固定长度的字符串,可以包含最多达255个字符。其中m代表字符串的长度。
 64             PS: 即使数据小于m长度,也会占用m长度
 65         varchar(m)
 66             varchars数据类型用于变长的字符串,可以包含最多达255个字符。其中m代表该数据类型所允许保存的字符串的最大长度,只要长度小于该最大值的字符串都可以被保存在该数据类型中。
 67 
 68             注:虽然varchar使用起来较为灵活,但是从整个系统的性能角度来说,char数据类型的处理速度更快,有时甚至可以超出varchar处理速度的50%。因此,用户在设计数据库时应当综合考虑各方面的因素,以求达到最佳的平衡
 69 
 70         text
 71             text数据类型用于保存变长的大字符串,可以组多到65535 (2**161)个字符。
 72 
 73         mediumtext
 74             A TEXT column with a maximum length of 16,777,215 (2**241) characters.
 75 
 76         longtext
 77             A TEXT column with a maximum length of 4,294,967,295 or 4GB (2**321) characters.
 78 
 79 
 80         enum
 81             枚举类型,
 82             An ENUM column can have a maximum of 65,535 distinct elements. (The practical limit is less than 3000.)
 83             示例:
 84                 CREATE TABLE shirts (
 85                     name VARCHAR(40),
 86                     size ENUM('x-small', 'small', 'medium', 'large', 'x-large')
 87                 );
 88                 INSERT INTO shirts (name, size) VALUES ('dress shirt','large'), ('t-shirt','medium'),('polo shirt','small');
 89 
 90         set
 91             集合类型
 92             A SET column can have a maximum of 64 distinct members.
 93             示例:
 94                 CREATE TABLE myset (col SET('a', 'b', 'c', 'd'));
 95                 INSERT INTO myset (col) VALUES ('a,d'), ('d,a'), ('a,d,a'), ('a,d,d'), ('d,a,d');
 96 
 97         DATE
 98             YYYY-MM-DD(1000-01-01/9999-12-31 99 
100         TIME
101             HH:MM:SS('-838:59:59'/'838:59:59'102 
103         YEAR
104             YYYY(1901/2155105 
106         DATETIME
107 
108             YYYY-MM-DD HH:MM:SS(1000-01-01 00:00:00/9999-12-31 23:59:59    Y)
109 
110         TIMESTAMP
111 
112             YYYYMMDD HHMMSS(1970-01-01 00:00:00/2037 年某时)
View Code

 三、表内容操作 

1.增

1 insert into 表名 (列名,列名...) values (值,值,值...);
2 insert into 表名 (列名,列名...) values (值,值) ,(值,值)...;

2.删

1 delete from 表;
2 delete fromwhere id =1 and name ='yan';

3.改

1 update 表 set name = 'yan' where id>1 or name = 'xxx';

4.查

1 select * from 表;
2 select id,name from 表;
3 select id,name fromwhere id>10 or name = 'yan';
4 select id,name as cname fromwhere id>10 or name = 'yan';

其他语句:

MySQL数据库操作(一) Mysql 第3张
 1 条件:
 2 select * from 表where id >1 and name != ‘yan’ and num=12;
 3 select * from 表where id in (1,5,12);
 4 select * from 表where id not in (1,5,12);
 5 select * from 表where id in (select id from db1);
 6 select * from 表where id between 5 and 110;
 7 
 8 通配符
 9 select * from 表名 where name like 'ya% ;  -ya开头的所有(多个字符)
10 select * from 表名 where name like 'ya_';    -ya开头的所有(一个字符)
11 限制
12 select * from 表 limit5;    -前5行
13 select * from 表 limit3,5;    -从三行到第5行
14 select * from 表 limit3 offset 4;    -从第4行到第5行
15 排序
16 select * from 表 order by 列 asc;    -根据“列”从小到大排列
17 select * from 表 order by 列 besc;    -根据“列”从大到小排列
18 select * from 表 order by id desc limit 10;    -先排序再找
19 select * from 表 order by 列表1 desc,列表2 asc;     - 根据 “列1” 从大到小排列,如    果相同则按列2从小到大排序
View Code
1 连表
2 select * from 表1 left join 表2 on 表1.id = 表2.name;

5.查看表结构

1 desc 表名;

6.查看表创建语句

1 show create table 表名\G;

7.自增步长

1 1.基于会话级别
2 show session variaoles like 'auto_inc%';      查看全局变量
3 set session auto_increment_increment=2;    设置会话步长
4 2.基于全局级别:
5 show global variables like 'auto_inc%';    查看全局变量
6 set global auto_increment_increment=2;   设置会话步长

 

扫码关注我们
微信号:SRE实战
拒绝背锅 运筹帷幄