MySQL
1.下载
官网:http://dev.mysql.com/downloads/mysql/
2.安装
1 服务端安装 2 初始化 3 "E:\mysql\mysql-8.0.11-winx64\mysql-8.0.11-winx64\bin\mysqld" --initialize -insecure 4 运行: 5 E:\mysql\mysql-8.0.11-winx64\mysql-8.0.11-winx64\bin\mysqld(也可以配置环境变量) 6 windows服务:E:\mysql\mysql-8.0.11-winx64\mysql-8.0.11-winx64\bin\mysqld --install 7 E:\mysql\mysql-8.0.11-winx64\mysql-8.0.11-winx64\bin\mysqld --remove 8 9 net start MySQL 10 net stop MySQL 11 12 客户端安装 13 E:\mysql\mysql-8.0.11-winx64\mysql-8.0.11-winx64\bin\mysql(环境变量)
3.使用
1 用户管理 2 查看: 3 E:\mysql\mysql-8.0.11-winx64\mysql-8.0.11-winx64\bin\mysql -u root -p 4 show databases; 5 use mysql; 6 show tables; 7 select user,host from user; 8 创建: 9 create user 'name'@'192.168.0.1' identified by 'password' 10 create user 'name'@'192.168.%' identified by 'password' 11 create user 'name'@'%' identified by 'password' 12 删除: 13 drop user 'name'@'IP' 14 修改用户: 15 16 rename user 'name'@'IP' to 'new name'@'IP' 17 修改密码: 18 set password for 'name'@'IP' = Password('new password')
管理员:alter user 'root'@'localhost' identified with mysql_native_password by 'new word'; 19 退出: 20 quit 21 授权: 22 grant select,insert,update on db.t1 to 'alex'@'%' 23 grant all privileges on db.t1 to 'alex'@'%' 24 revoke all privileges on db.t1 to 'alex'@'%'
4.数据结构
bit[(M)] 二进制位(101001),m表示二进制位的长度(1-64),默认m=1 tinyint[(m)] [unsigned] [zerofill] 小整数,数据类型用于保存一些范围的整数数值范围: 有符号: -128 ~ 127. 无符号: 0 ~ 255 特别的: MySQL中无布尔值,使用tinyint(1)构造。 int[(m)][unsigned][zerofill] 整数,数据类型用于保存一些范围的整数数值范围: 有符号: -2147483648 ~ 2147483647 无符号: 0 ~ 4294967295 特别的:整数类型中的m仅用于显示,对存储范围无限制。例如: int(5),当插入数据2时,select 时数据显示为: 00002 bigint[(m)][unsigned][zerofill] 大整数,数据类型用于保存一些范围的整数数值范围: 有符号: -9223372036854775808 ~ 9223372036854775807 无符号: 0 ~ 18446744073709551615 decimal[(m[,d])] [unsigned] [zerofill] 准确的小数值,m是数字总个数(负号不算),d是小数点后个数。 m最大值为65,d最大值为30。 特别的:对于精确数值计算时需要用此类型 decaimal能够存储精确值的原因在于其内部按照字符串存储。 FLOAT[(M,D)] [UNSIGNED] [ZEROFILL] 单精度浮点数(非准确小数值),m是数字总个数,d是小数点后个数。 无符号: -3.402823466E+38 to -1.175494351E-38, 0 1.175494351E-38 to 3.402823466E+38 有符号: 0 1.175494351E-38 to 3.402823466E+38 **** 数值越大,越不准确 **** DOUBLE[(M,D)] [UNSIGNED] [ZEROFILL] 双精度浮点数(非准确小数值),m是数字总个数,d是小数点后个数。 无符号: -1.7976931348623157E+308 to -2.2250738585072014E-308 0 2.2250738585072014E-308 to 1.7976931348623157E+308 有符号: 0 2.2250738585072014E-308 to 1.7976931348623157E+308 **** 数值越大,越不准确 **** char (m) char数据类型用于表示固定长度的字符串,可以包含最多达255个字符。其中m代表字符串的长度。 PS: 即使数据小于m长度,也会占用m长度 varchar(m) varchars数据类型用于变长的字符串,可以包含最多达255个字符。其中m代表该数据类型所允许保存的字符串的最大长度,只要长度小于该最大值的字符串都可以被保存在该数据类型中。 注:虽然varchar使用起来较为灵活,但是从整个系统的性能角度来说,char数据类型的处理速度更快,有时甚至可以超出varchar处理速度的50%。因此,用户在设计数据库时应当综合考虑各方面的因素,以求达到最佳的平衡 text text数据类型用于保存变长的大字符串,可以组多到65535 (2**16 − 1)个字符。 mediumtext A TEXT column with a maximum length of 16,777,215 (2**24 − 1) characters. longtext A TEXT column with a maximum length of 4,294,967,295 or 4GB (2**32 − 1) characters. enum 枚举类型, An ENUM column can have a maximum of 65,535 distinct elements. (The practical limit is less than 3000.) 示例: CREATE TABLE shirts ( name VARCHAR(40), size ENUM('x-small', 'small', 'medium', 'large', 'x-large') ); INSERT INTO shirts (name, size) VALUES ('dress shirt','large'), ('t-shirt','medium'),('polo shirt','small'); set 集合类型 A SET column can have a maximum of 64 distinct members. 示例: CREATE TABLE myset (col SET('a', 'b', 'c', 'd')); INSERT INTO myset (col) VALUES ('a,d'), ('d,a'), ('a,d,a'), ('a,d,d'), ('d,a,d'); DATE YYYY-MM-DD(1000-01-01/9999-12-31) TIME HH:MM:SS('-838:59:59'/'838:59:59') YEAR YYYY(1901/2155) DATETIME YYYY-MM-DD HH:MM:SS(1000-01-01 00:00:00/9999-12-31 23:59:59 Y) TIMESTAMP YYYYMMDD HHMMSS(1970-01-01 00:00:00/2037 年某时)
5.MySQL语句
1 操作文件夹
2 create database db1;
3 create database db1 default charset utf-8; 4 show databases; 5 use db1; 6 drop database db1; 7 操作表 8 show tables; 9 create table t1(id int,name char(10)) default charset= utf8; 10 create table t1(id int,name char(10)) engine=innodb default charset= utf8; 11 create table t1(id int auto_increment,name char(10)) engine=innodb default charset= utf8; 12 create table t1( 13 列名 类名 null 14 列名 类名 not null 15 列名 类名 not null auto_increment primary key, 16 name char(10)) ... 17 auto_increment:表示自增 18 primary key:表示约束且不能为空;加速查找 19 20 清空表 21 delete from t1;(有痕迹) 22 truncate table t1; 23 删除表 24 drop table t1; 25 插入内容: 26 insert into t1(id,name) values(1,'alex'),(2,'zhang');
insert into t1(id,name) select user,name from t2; 27 查看内容: 28 select * from t1;
select * from t1 where id =1 or name='xx';
select id,name as cname from t2 where id=1 or name='xx'
others:
select * from tb1 where id != 1;
selece * from tb1 where id in(1,2,4);
select * from tb1 where id in(select id from t2);
select * from tb1 where id not in(1,2,4)
通配符:
select * from tb1 where name like "a%";
select * from tb1 where name like "a_"
分页:
select * from tb1 ilmit 10;
select * from tb1 limit 0,10;#从0开始往后读取10行
排序:
select * from tb1 order by id desc; #从大到小
select * from tb1 order by id asc;#从小到大
select * from tb1 order by id desc ,id desc;
***分组:
select count(id),max(id),part_id from t1 group by part_id;
count、sum、avg、max、min
对于聚合函数结果进行二次筛选时必须使用having ****
select count(id),part_id from userinfo5 group by part_id having count(id) > 1;
select count(id),part_id from userinfo5 where id > 0 group by part_id having count(id) > 1;
***连表操作:
select * from tb1,tb2;
select * from tb1,tb2 where tb1.id=tb2.id;
select * from tb1 left join tb2 on t1.id= t2.id;
left 左边全显示 right 右边全显示 innder 将出现null一行是隐藏
29 修改内容: 30 update t1 set age=18 where age=17;
31 others
创建新表
(select * form t1 ) as a
select id,1 from t1; 将会新增一列1
flush privileges,将数据读取到内存中,从而立即生效
浙公网安备 33010602011771号