当前位置:Gxlcms > 数据库问题 > Mysql操作初级

Mysql操作初级

时间:2021-07-01 10:21:17 帮助过:3人阅读

1.远程登陆mysql服务器 2 mysql -uroot -p -h192.168.137.10 -P3306 3 4 2.查询数据库 5 show databases; 6 默认数据库: 7 mysql - 用户权限相关数据 8 test - 用于用户测试数据 9 information_schema - MySQL本身架构相关数据 10 11 3.查看当前用户 12 select user(); 13 14 4.查看当前所在数据库 15 select database(); 16 17 5.查看数据库版本 18 select version(); 19 20 6.查看数据库状态 21 show status; 当前会话状态 22 show global status; 全局数据库状态 23 show slave status\G; 查看主从数据库状态信息 24 25 7.查询数据库参数 26 show variables; 27 28 8.修改数据库参数 29 show variables like max_connect%; 30 set global max_connect_errors = 1000;(重启数据库会失效,要在配置文件中修改) 31 32 9.查看当前数据库队列 33 show processlist; 34 35 10.创建数据库 36 CREATE DATABASE 数据库名称 DEFAULT CHARSET utf8 COLLATE utf8_general_ci; # 默认编码格式utf8 37 COLLATE utf8_general_ci用于数据库中的排序规则 38 CREATE DATABASE 数据库名称 DEFAULT CHARACTER SET gbk COLLATE gbk_chinese_ci; # 默认编码格式gbk 39 40 11.进入数据库 41 USE db_name;

 

用户管理

 1 1.创建用户
 2 create user 用户名@IP地址 identified by 密码;
 3 
 4 2.删除用户
 5 drop user 用户名@IP地址;
 6 
 7 3.修改用户
 8 rename user 用户名@IP地址 to 新用户名@IP地址;
 9 
10 4.修改密码
11 set password for 用户名@IP地址 = Password(新密码);
12 
13 PS:用户权限相关数据保存在mysql数据库的user表中,所以也可以直接对其进行操作(不建议)

 

授权管理

1 1.查看权限
2 show grants for 用户@IP地址
3 
4 2.授权
5 grant  权限 on 数据库.表 to 用户@IP地址
6 
7 3.取消权限
8 revoke 权限 on 数据库.表 from 用户@IP地址

 

权限种类:

 1 privileges  除grant外的所有权限
 2 select          仅查权限
 3 select,insert   查和插入权限
 4 ...
 5 usage                   无访问权限  #默认新建用户权限
 6 alter                   使用alter table
 7 alter routine           使用alter procedure和drop procedure
 8 create                  使用create table
 9 create routine          使用create procedure
10 create temporary tables 使用create temporary tables
11 create user             使用create user、drop user、rename user和revoke  all privileges
12 create view             使用create view
13 delete                  使用delete
14 drop                    使用drop table
15 execute                 使用call和存储过程
16 file                    使用select into outfile 和 load data infile
17 grant option            使用grant 和 revoke
18 index                   使用index
19 insert                  使用insert
20 lock tables             使用lock table
21 process                 使用show full processlist
22 select                  使用select
23 show databases          使用show databases
24 show view               使用show view
25 update                  使用update
26 reload                  使用flush
27 shutdown                使用mysqladmin shutdown(关闭MySQL)
28 super                   ??使用change master、kill、logs、purge、master和set global。还允许mysqladmin????调试登陆
29 replication client      服务器位置的访问
30 replication slave       由复制从属使用
31 
32 对于目标数据库以及内部其他:
33         数据库名.*           数据库中的所有
34         数据库名.表          指定数据库中的某张表
35         数据库名.存储过程     指定数据库中的存储过程
36         *.*                所有数据库
37 
38 用户名@IP地址         用户只能在改IP下才能访问
39 用户名@192.168.1.%   用户只能在改IP段下才能访问(通配符%表示任意)
40 用户名@%             用户可以再任意IP下访问(默认IP地址为%)
41 
42 grant all privileges on db1.tb1 TO 用户名@IP
43 
44 grant select on db1.* TO 用户名@IP
45 
46 grant select,insert on *.* TO 用户名@IP
47 
48 revoke select on db1.tb1 from 用户名@IP
49 
50 flush privileges,将数据读取到内存中,从而实现不重启即可生效。

 

4.数据表操作

创建表

 1 create table 表名(
 2     列名  类型  是否可以为空,
 3     列名  类型  是否可以为空
 4 )ENGINE=InnoDB DEFAULT CHARSET=utf8
 5 
 6 是否可空,null表示空,非字符串
 7             not null    - 不可空
 8             null        - 可空
 9 
10  默认值,创建列时可以指定默认值,当插入数据时如果未主动设置,则自动添加默认值
11             create table tb1(
12                 nid int not null defalut 2,
13                 num int not null
14             )
15 
16  自增,如果为某列设置自增列,插入数据时无需设置此列,默认将自增(表中只能有一个自增列)
17             create table tb1(
18                 nid int not null auto_increment primary key,
19                 num int null
20             )
21 22             create table tb1(
23                 nid int not null auto_increment,
24                 num int null,
25                 index(nid)
26             )
27             注意:1、对于自增列,必须是索引(含主键)。
28                  2、对于自增可以设置步长和起始值
29                      show session variables like auto_inc%;
30                      set session auto_increment_increment=2;
31                      set session auto_increment_offset=10;
32 
33                      shwo global  variables like auto_inc%;
34                      set global auto_increment_increment=2;
35                      set global auto_increment_offset=10;
36 
37 主键,一种特殊的唯一索引,不允许有空值,如果主键使用单个列,则它的值必须唯一,如果是多列,则其组合必须唯一。
38             create table tb1(
39                 nid int not null auto_increment primary key,
40                 num int null
41             )
42 43             create table tb1(
44                 nid int not null,
45                 num int not null,
46                 primary key(nid,num)
47             )
48 
49  外键,一个特殊的索引,只能是指定内容
50             creat table color(
51                 nid int not null primary key,
52                 name char(16) not null
53             )
54 
55             create table fruit(
56                 nid int not null primary key,
57                 smt char(32) null ,
58                 color_id int not null,
59                 constraint fk_cc foreign key (color_id) references color(nid)
60             )

 

删除表

 1 drop table 表名 

清空表

  • delete from 表名 # 删除比较大数据量表时,速度比较慢,推荐用truncate
  • truncate table 表名 # 清空表内容,只保留表结构。对于大表清空速度很快

修改表

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

 

基本数据类型

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

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

 

5.表内容操作

1、增

1 insert into 表 (列名,列名...) values (值,值,值...)
2 
3 insert into 表 (列名,列名...) values (值,值,值...),(值,值,值...)
4 
5 insert into 表 (列名,列名...) select (列名,列名...) from

 

2、删

1 delete from2   -----------------------------------------
3 delete from 表 where id=1 and name=xiaomin

 

3、改

 1 update 表 set name = alex where id>1 

4、查

1 select * from2   ------------------------
3 select * from 表 where id > 1
4   ------------------------
5 select nid,name,gender as gg from 表 where id > 1

 

5、其他

 1 a、条件
 2     select * from 表 where id > 1 and name != alex and num = 12;
 3 
 4     select * from 表 where id between 5 and 16;
 5 
 6     select * from 表 where id in (11,22,33)
 7     select * from 表 where id not in (11,22,33)
 8     select * from 表 where id in (select nid from 表)
 9 
10 b、通配符
11     select * from 表 where name like ale%  - ale开头的所有(多个字符串)
12     select * from 表 where name like ale_  - ale开头的所有(一个字符)
13 
14 c、限制
15     select * from 表 limit 5;            - 前5行
16     select * from 表 limit 4,5;          - 从第4行开始的5行
17     select * from 表 limit 5 offset 4    - 从第4行开始的5行
18 
19 d、排序
20     select * from 表 order by 列 asc              - 根据 “列” 从小到大排列
21     select * from 表 order by 列 desc             - 根据 “列” 从大到小排列
22     select * from 表 order by 列1 desc,列2 asc    - 根据 “列1” 从大到小排列,如果相同则按列2从小到大排序
23 
24 e、分组
25     select num from 表 group by num
26     select num,nid from 表 group by num,nid
27     select num,nid from 表  where nid > 10 group by num,nid order nid desc
28     select num,nid,count(*),sum(score),max(score),min(score) from 表 group by num,nid
29 
30     select num from 表 group by num having max(id) > 10
31 
32     特别的:group by 必须在where之后,order by之前
33 
34 f、连表
35     无对应关系则不显示
36     select A.num, A.name, B.name
37     from A,B
38     Where A.nid = B.nid
39 
40     无对应关系则不显示
41     select A.num, A.name, B.name
42     from A inner join B
43     on A.nid = B.nid
44 
45     A表所有显示,如果B中无对应关系,则值为null
46     select A.num, A.name, B.name
47     from A left join B
48     on A.nid = B.nid
49 
50     B表所有显示,如果B中无对应关系,则值为null
51     select A.num, A.name, B.name
52     from A right join B
53     on A.nid = B.nid
54 
55 g、组合
56     组合,自动处理重合
57     select nickname
58     from A
59     union
60     select name
61     from B
62 
63     组合,不处理重合
64     select nickname
65     from A
66     union all
67     select name
68     from B

 

Mysql操作初级

标签:

人气教程排行