当前位置:Gxlcms > 数据库问题 > MySQL基础入门

MySQL基础入门

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

all 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 由复制从属使用

 

2、对于数据库及内部其他权限如下:

            数据库名.*           数据库中的所有
            数据库名.表          指定数据库中的某张表
            数据库名.存储过程     指定数据库中的存储过程
            *.*                所有数据库

3、对于用户和IP的权限如下:

1             用户名@IP地址         用户只能在改IP下才能访问
2             用户名@192.168.1.%   用户只能在改IP段下才能访问(通配符%表示任意)
3             用户名@%             用户可以再任意IP下访问(默认IP地址为%)

4、查看权限:

show grants for ‘用户‘@‘IP地址‘ 

5、授权

grant  权限 on 数据库.表 to   ‘用户‘@‘IP地址‘

6、取消授权

revoke 权限 on 数据库.表 from ‘用户‘@‘IP地址‘

 

五、MySQl数据库表操作

1、查看表

show tables;                    # 查看数据库全部表

select * from 表名; # 查看表所有内容

2、创建表

create table 表名(
    列名  类型  是否可以为空,
    列名  类型  是否可以为空
)ENGINE=InnoDB DEFAULT CHARSET=utf8

创建表实例好详解

技术图片
CREATE TABLE `tab1` (
  `nid` int(11) NOT NULL auto_increment,                   # not null表示不能为空,auto_increment表示自增
  `name` varchar(255) DEFAULT zhangyanlin,                 # default 表示默认值
  `email` varchar(255),
  PRIMARY KEY (`nid`)                                      # 把nid列设置成主键
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

附注:

  • 默认值,创建列时可以指定默认值,当插入数据时如果未主动设置,则自动添加默认值
  • 自增,如果为某列设置自增列,插入数据时无需设置此列,默认将自增(表中只能有一个自增列)注意:1、对于自增列,必须是索引(含主键)2、对于自增可以设置步长和起始值
  • 主键,一种特殊的唯一索引,不允许有空值,如果主键使用单个列,则它的值必须唯一,如果是多列,则其组合必须唯一。

3、删除表

drop table 表名

4、清空表内容

delete from 表名
truncate table 表名

5、修改表

技术图片
添加列:   alter table 表名 add 列名 类型
删除列:   alter table 表名 drop column 列名
修改列:
          alter table 表名 modify column 列名 类型;  -- 类型
          alter table 表名 change 原列名 新列名 类型; -- 列名,类型
  
添加主键:
          alter table 表名 add primary key(列名);
删除主键:
          alter table 表名 drop primary key;
          alter table 表名  modify  列名 int, drop primary key;
  
添加外键: alter table 从表 add constraint 外键名称(形如:FK_从表_主表) foreign key 从表(外键字段) references 主表(主键字段);
删除外键: alter table 表名 drop foreign key 外键名称
  
修改默认值:ALTER TABLE testalter_tbl ALTER i SET DEFAULT 1000;
删除默认值:ALTER TABLE testalter_tbl ALTER i DROP DEFAULT;

5、基本数据类型

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

技术图片
  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:59102 
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 年某时)
MySQL数据库的数据类型

 

六、MySQL表内数据操作

表内容操作无非就是增删改查,当然用的最多的还是查。

1、增

insert into 表 (列名,列名...) values (值,值,...)
insert into 表 (列名,列名...) values (值,值,...),(值,值,值...)
insert into 表 (列名,列名...) select (列名,列名...) from 表
例:
insert into tab1(name,email) values(‘tt‘,‘ztt8851@163.com‘)

2、删

delete from 表                                      # 删除表里全部数据
delete from 表 where id=1 and name=‘tt‘   # 删除ID =1 和name=‘tt‘ 那一行数据

3、改

update 表 set name = ‘tt‘ where id>1

4、查

select * from 表
select * from 表 where id > 1
select nid,name,gender as gg from 表 where id > 1

 

4.1、条件判断where

    select * from 表 where id > 1 and name != ‘aylin‘ and num = 12;
    select * from 表 where id between 5 and 16;
    select * from 表 where id in (11,22,33)
    select * from 表 where id not in (11,22,33)
    select * from 表 where id in (select nid from 表)

4.2、通配符like

    select * from 表 where name like ‘zhang%‘  # zhang开头的所有(多个字符串)
    select * from 表 where name like ‘zhang_‘  # zhang开头的所有(一个字符)

4.3、限制limit

    select * from 表 limit 5;            - 前5行
    select * from 表 limit 4,5;          - 从第4行开始的5行
    select * from 表 limit 5 offset 4    - 从第4行开始的5行

4.4、排序asc,desc

    select * from 表 order by 列 asc              - 根据 “列” 从小到大排列
    select * from 表 order by 列 desc             - 根据 “列” 从大到小排列
    select * from 表 order by 列1 desc,列2 asc    - 根据 “列1” 从大到小排列,如果相同则按列2从小到大排序

4.5、分组group by

技术图片
    select num from 表 group by num
    select num,nid from 表 group by num,nid
    select num,nid from 表  where nid > 10 group by num,nid order nid desc
    select num,nid,count(*),sum(score),max(score),min(score) from 表 group by num,nid
    select num from 表 group by num having max(id) > 10
 
    特别的:group by 必须在where之后,order by之前

MySQL基础入门

标签:安装   创建   多列   amp   opened   分组   view   rac   prim   

人气教程排行