时间:2021-07-01 10:21:17 帮助过:2人阅读
整数类型
根据存储数值的范围又分为:
有符号 无符号
tinyint 微小整型 -128~127 0~255
smallint
MEDIUMINT
int
bigint
create table t7(level tinyint);
create table t8(age tinyint unsigned);
insert into t7 values(170);
insert into t7 values(-17);
insert into t7 values(128);
insert into t8 values(170);
insert into t8 values(-170);
insert into t8 values(17.54);
insert into t8 values(17.44);
select * from t8;
select * from t7;
浮点型 float double
单精度 双精度
float(n,m)
double(n,m)
n 表示总位数
m 表小数位位数
整数.小数
1023.77
create table t9(
name char(10),
age int,
pay float(7,2)
);
insert into t9 values ("jim",21,118000.23);
insert into t9 values ("jim",21,118000);
数值类型的宽度与字符类型宽度的区别?
数值类型的宽度是显示宽度,不能够控制给字段赋值的大小,字段值的大小由字段类型决定。
create table t10(
name char(3),
id int(2)
);
insert into t10 values("lucy",1129);
insert into t10 values("tom",1129);
insert into t10 values("luc",1029);
create table t12(
level int(7) zerofill,
id int(3) zerofill
);
create table t13(
level int (1)
id int(1)
);
日期时间类型 (生日 注册时间 入职时间)
年 year YYYY 2016
01-69 20XX
70-99 19XX
00 0000
日期 date YYYYMMDD 20161219
时间 time HHMMSS 144518
日期时间 ( 约会时间 )
datetime / timestamp
YYYYMMDDHHMMSS
20170214183018
datetime 与 timestamp 的区别?
当不给timestamp类型的字段赋值时,用系统当前的时间给字段赋值。
create table t16(
time1 timestamp,
time2 datetime
);
insert into t16 values(20171219165200,20161219165200);
insert into t16 (time1) values(20191219165200);
insert into t16 (time2) values(20151219165200);
create table t15(
name char(10),
age tinyint(2) unsigned,
pay float(7,2),
up_class time,
birthday date,
s_year year,
meetting datetime
);
insert into t15 values("bob",21,18800.88,083000,20170101,1995,20170224203000);
insert into t15(name,s_year)values("lucy",13);
insert into t15(name,s_year)values("alic",70),("lilei",00);
select name,s_year from t15;
使用时间函数获取时间给日期时间类型字段赋值?
now() 获取当前系统时间
year() 获取年份
date() 获取日期
month() 获取月份
day() 获取日期(几号)
time() 获取时间
select now();
select year( now() );
select year( 20191224 );
select date( now() );
insert into t15 values("lili",21,18800.88,093000,20171008,1995,now());
insert into t15 values("jerry",29,28800.88,now(),now(),now(),now());
insert into t15 values("tom",21,18800.88,time(20171224201818),date(20171224201818),year(now()),now());
枚举类型 (爱好 性别 专业 )
字段的值只能在列举的范围内选择
enum(值列表) 单选
set(值列表) 多选
create table t177(
name char(10),
sex enum(0,1),
likes set("book","game","film","music")
);
create table t17(
name char(10),
sex enum("boy","girl","no"),
likes set("book","game","film","music")
);
desc t17;
insert into t17 values("bob","boy","woman,game");
insert into t17 values("bob","boy","book,game");
insert into t17 values("alic",3,"game");
select * from t17;
查看建表过程
show create table 表名;
create table 学生信息表2(
姓名 char(10),
年龄 int(2)
)DEFAULT CHARSET=utf8;
insert into 学生信息表2 values ("张三丰",21);
课后作业:
创建stuinfo表,设置合理的字段个数和字段类型。
+++++++++++++++++++++++++++++++++++++++++++++++
day01内容回顾:
提供数据库服务的软件有哪些?
哪些是开源软件 商业软件 是否跨平台?
mysql的发展史? 特点 ? 应用场景 分支版本
安装系统自带的mysql数据软件提供服务?
rpm -q mysql-server mysql
启动mysql数据库服务
service mysqld start|stop|status
3306
/etc/my.cnf
mysqld
mysql/mysql
tcp
/var/lib/mysql/
连接数据库服务器
mysql -hlocalhost -uroot -p123 库名
SQL命令 的使用规则?
管理库相关的命令?
show databases; (显示有哪些数据库)
use 库名;(进入一个数据库)
select database();(查看当前在哪一个库)
show tables; (查看名下有哪些表)
create database 库名;(创建一个数据库)
drop database 库名;(删除一个数据库)
管理表相关的命令?
create table 表(字段列表); mysql> create table t1(name int);创建一个表
select * from 表名; (查看表中的内容)
desc 表名; ( 打开该表)
delete from 表名; (删除表中的内容)
drop table 表名; (删除该表)
insert into 表名 values(字段值列表);
mysql数据类型?
字符类型 char varchar text blob
数值类型 tinyint smallint int bigint
float(n,m)
double(n,m)
阐述zerofill 和 unsigned 作用?
日期时间类型 year data time datetime timestmap
使用2位数给year类型字段赋值的规律?
01-69 20xx
70-99 19xx
00 0000
时间函数 now() year() date() day() month() time()
枚举类型 enum set
单选 多选
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
约束条件
作用:当向表中插入新记录时,限制如何给字段赋值,若建表时不设置约束条件,使用mysql的默认设置。
Null 是否允许为null (空) 不设置默认允许为空
not null 不允许为null
mysql> create table t18(
-> name char(10) not null,
-> age tinyint(2) unsigned default 23,
-> sex enum("boy","girl") default "boy",
-> likes set("it","book","work","film") default "it,book"
-> );
Query OK, 0 rows affected (0.67 sec)
create table t22(
name char(5) not null,
id int(2) not null
);
insert into t21 values(null,null);
insert into t22 values("",19);
insert into t22 values("NULL",19);
Key 是否是索引字段
默认不是索引字段
Default 字段是否有默认值,若没有设置默认值,默认值是null
作用:向表中插入新记录时,当不给记录的字段赋值时,使用字段的默认值给字段赋值,指定默认值时 要与字段的类型匹配。
字段名 类型(宽度) default 值
create table t23(
name char(10) not null ,
age tinyint(2) unsigned default 21,
sex enum("boy","girl") not null default "boy",
likes set("book","music","film","game") default "film,game"
);
insert into t23(name)values("bob");
insert into t23 values("tom",28,"girl","book");
insert into t23 values(null,28,"girl","book");
insert into t23 values("null",28,"girl","book");
insert into t23 values("",28,"girl","book");
Extra 额外设置(例如自增长 描述信息)
+++++++++++++++++++++++++++++++++++++++++++++++
修改表结构
alter table 表名 执行动作;
add 添加新字段
add 字段名 类型(宽度);
add 字段名 类型(宽度) 约束条件 ;
add 字段名 类型(宽度) 约束条件 first;
add 字段名 类型(宽度) 约束条件 after 字段名;
alter table t1 add class char(7) default "nsd1609" first,add tel char(11) ,add sex enum ("boy","girl") default "boy" after name;
drop 删除字段
drop 字段名
alter table t1 drop name,drop sex;
modify 修改字段类型
* 不能与字段已经存储的数据冲突
modify 字段名 类型(宽度) 约束条件;
mysql> alter table t1
-> modify
-> sex enum("boy","girl","no") not null default "no";
change 修改字段名
change 原字段名 新字段名 类型(宽度) 约束条件;
alter table t1 change tel iphone char(11);
修改表名
alter table 原表名 rename [to] 新表名;
alter table t1 rename t111;
+++++++++++++++++++++++++
day02
一、mysql索引
二、mysql存储引擎
一、mysql索引
什么是索引? 相当于 "书的目录"
总页数 1000页
1~1000
目录 1----30
第一章 31--217 网站搭建
35-40 数据加密 2
第2章 218--273 dhcp
。。。
第十章
正文
索引的优点 加快查询记录的速度.
索引的缺点 会减慢写的速度( insert update delete ).
占用物理存储空间.
在表里建索引 设置在字段上
stuinfo.frm stuinfo.ibd
name class sex age
jim
tom
lucy
jerry
bob
alic
aliccc
select name from userinfo where name like "j%";
mysql索引类型?
普通索引 index *
唯一索引 unique
主键 primary key *
外键 foreign key *
全文索引 fulltext
使用索引(查看 创建 使用规则 删除 )?
查看索引?
desc 表名;key
show index from 表名\G;
Table: user
Column_name: Host
Key_name: PRIMARY
Index_type: BTREE B+tree hash
二叉树
1-10
1-5 6-10
1-2.5 2.6-5
++++++++++++++++++++++
index普通索引的使用规则?
一个表中可以有多个INDEX字段
字段的值允许有重复,且可以赋NULL值
经常把做查询条件的字段设置为INDEX字段
INDEX字段的KEY标志是MUL
创建普通索引?
1 在已有表里创建index字段
create index 索引名 on 表名(字段名);
create index sex on t111(sex);
2 建表时创建index字段
create table 表名 (
字段名列表,
index(字段名),
index(字段名)
);
create table t24(
name char(10) ,
age tinyint(2) unsigned default 21,
sex enum("boy","girl") default "boy",
likes set("book","music","film","game") default "film,game",
index(name),
index(sex)
);
insert into t24(name)values("bob"),("bob"),(null),(null);
删除普通索引?
drop index 索引名 on 表名;
drop index sex on t24;
++++++++++++++++++++++++++++++++++++++++++++++
primary key 主键的使用规则?
一个表中只能有一个primary key字段
对应的字段值不允许有重复,且不允许赋NULL值
如果有多个字段都作为PRIMARY KEY,称为复合主键,必须一起创建。
主键字段的KEY标志是PRI
通常与 AUTO_INCREMENT 连用
经常把表中能够唯一标识记录的字段设置为主键字段[记录编号字段]
建表时创建主键字段
create table t25(
name char(10),
age int(2),
primary key(name)
);
create table t26(
name char(10) primary key,
age int(2)
);
删除主键
alter table 表名 drop primary key;
在已有表里创建主键
alter table 表名 add primary key(字段名);
复合主键的使用? 多个字段一起做主键是复合主键 必须一起创建。
*字段的值不允许同时相同。
create table t29(
host char(10),
db char(10),
user char(10),
primary key(host,db,user)
);
alter table t29 drop primary key;
alter table t29 add primary key(host,user,db);
insert into t29 values("2.1.1.1","game","tom");
insert into t29 values("2.1.1.1","bbsdb","tom");
insert into t29 values("2.1.1.1","game","jim");
通常和aUTO_INCREMENT 连用 实现字段值的字段增长
数值类型
主键
id name age
1 jim 21
2 jim 21
3 jim 21
create table t221(
id int(2) primary key auto_increment,
name char(10),
age int(2)
);
insert into t221(name,age)values("jim",21);
insert into t221(name,age)values("tom",19);
select * from t221;
insert into t221(id,name,age)values(7,"bob",19);
select * from t221;
经常把表中能够唯一标识记录的字段设置为主键字段[记录编号字段]
+++++++++++++++++++++++++++++++++++++++++++++
唯一索引 unique ?
字段的值可以为Null 但不可以重复
一个表里可以有多个unique字段
标志 UNI
姓名 身份证 考试证 护照 驾驶证
jim null
tom null
建表时创建
create table t29(
name char(10),
stu_id char(9),
age int(2),
unique(stu_id)
);
mysql> insert into t29 values ("lucy","nsd160903",18);
Query OK, 1 row affected (0.05 sec)
mysql> insert into t29 values ("lucy","nsd160901",18);
mysql> insert into t29 values ("lucy","nsd160903",18);
mysql> insert into t29 values ("lucy",null,18);
drop index 索引名 on 表名;
drop index stu_id on t29;
在已有表里创建unique字段
create unique index 索引名 on 表名(字段名);
create unique index stu_id on t29(stu_id);
++++++++++++++++
缴费表
jfb_id name pay
98 jim 20000
87 bob 18000
92 alic 20000
班级表
bjb_id name
98 jim
87 bob
+++++++++++++++++++++++++++++++++++++++++++++++++
外键 foreign key *
功能 让当前表某个字段的值,在另一个表某个字段值的范围内选择。
使用规则?
1 表的存储引擎必须是innodb
2 字段的数据类型要匹配
3 被参考的字段必须是key 中的一种 (primary key)
create table jfb(
jfb_id int(2) primary key auto_increment,
name char(10),
pay float(7,2)
)engine=innodb;
insert into jfb(name,pay)values("bob",18000),("lucy",17800),("alic",20000);
create table bjb(
bjb_id int(2),
name char(10),
foreign key(bjb_id) references jfb(jfb_id) on update cascade on delete cascade
)engine=innodb;
mysql> show create table bjb;
select * from bjb;
insert into bjb values(2,"lucy");
insert into jfb(name,pay)values("lilei",18000);
insert into bjb values(5,"lilei");
update jfb set jfb_id=8 where jfb_id=2;
delete from jfb where jfb_id=3;
++++++++++++++++++++++++++++++++++++++++++++++++++++++
使用外键的注意事项?
delete from jfb;
alter table jfb drop jfb_id;
drop table jfb;
删除外键
show create table 表名;
alter table 表名 drop foreign key 外键名;
alter table bjb drop foreign key bjb_ibfk_1;
++++++++++++++++++++++++++++++++++++++++++++++++++++++++
二、mysql存储引擎
什么是存储引擎?
表的处理器,是mysql数据库服务软件自动程序,不同处理器有不同的功能和数据存储方式。
查看数据库服务支持哪些存储引擎?
show engines;
InnoDB DEFAULT
修改mysql数据库服务默认使用的存储引擎?
vim /etc/my.cnf
[mysqld]
default-storage-engine=myisam
:wq
service mysql stop
service mysql start
建表时指定表使用的存储引擎
create table t31(name char(10))engine=memory;
修改表使用的存储引擎?
alter table 表名 engine=存储引擎名;
alter table t31 engine=innodb;
查看表使用的存储引擎?
show create table 表名;
工作中使用哪种存储引擎?
myisam
innodb
myisam的特点?
支持表级锁
不支持外键 、 事务 、事务回滚
独享表空间
t1.frm 表结构
t1.MYD 表记录
t1.MYI 表索引
innodb的特点?
支持行级锁
支持外键 、 事务 、事务回滚
共享表空间
t3.frm 表结构
t3.ibd 表记录+表索引
事务? 一次sql操作从开始到结束的过程。
事务回滚?执行一次事务,只要执行过程中,任何一步执行失败,就恢复之前所有的sql操作。
ATM
A ------------> B
A 登录 密码
转账 金额 10000
对方卡号 xxxxxx
确定
转账中...... A-1W B+1W
余额不足
转账成功
退卡
事务日志文件记录对所有inondb存储引擎的表执行过的sql命令。
ibdata1 记录sql命令产生的数据信息
ib_logfile0----|
|---> 记录SQL 命令
ib_logfile1----|
insert into t1 values(101),(202),(999);
ti.idb 真实的数据
select * from t1
id
101
202
999
锁机制是为了解决客户端的并发访问冲突问题。
锁粒度: 表级锁 行级锁 页级锁
锁类型:
读锁 (共享锁) select * from t1;
写锁 (互斥锁 排它锁)
insert into t1 values(22);
update t1 set id=102 where id=22;
delete from t1 where id=99;
建表时如何决定表使用的存储引擎?
执行写操作多的表适合使用inondb存储引擎,这样并发访问大。
执行读操作多的表适合使用myisam存储引擎.
mysql体系结构(mysql服务的工作过程)
连接池
sql接口
分析器
优化器
查询缓存
存储引擎
文件系统
管理工具
++++++++++++++++++++++++
day02内容回顾:
1约束条件:
是否允许为空 not null
是否是索引
默认值 default 值
额外设置
2修改表结构:
alter table 表名 执行动作;
add after first
modify
drop
change
3mysql 索引
什么是索引?
优点与缺点?
mysql索引类型
index primary key unique foreign key
索引的查看 创建 删除 使用规则
4 mysql存储引擎
修改mysql数据库服务默认使用的存储引擎
vim /etc/my.cnf
[mysqld]
default-storage-engine=存储引擎名
:wq
service mysql restart
查看存储引擎:
show engines;
show create table 表名;
create table 表名(字段列表)engine=存储
引擎名;
alter table 表名 engine=存储引擎名;
+++++++++++++++++++++++
day03
数据导入 : 把系统文件的内容存储到数据库的表里。
语法格式:
mysql> LOAD DATA INFILE ‘文件名‘ INTO TABLE 表名
FIELDS TERMINATED BY ‘分隔符‘ LINES TERMINATED BY ‘\n‘;
把系统用户信息存储到数据库服务器userdb库下的student表里。
cat /etc/passwd
用户名 密码 uid gid 描述信息 家目录 shell
create database userdb;
create table userdb.student(
name char(25),
password char(1),
uid smallint(2),
gid smallint(2),
comment varchar(50),
homedir char(30),
shell char(30),
index(name)
);
(不管什么数据如果要想导入到数据库中,一定要在安装数据库本机中导入,给别的用户授任何权限都只有增删该查,不能执行导入这命令)
mysql>load data infile "/etc/passwd" into table userdb.student fields terminated by ":" lines terminated by "\n";
-----------------------
mysql> select * from student;
mysql>alter table userdb.student add id int(2) zerofill primary key auto_increment first;
数据导入注意事项:
字段分隔符要与文件内的一致
指定导入文件的绝对路径
导入数据的表字段类型要与文件字段匹配
++++++++++++++++++++++++++++++++++++++
数据导出: 把表中的记录存储到系统文件里。
语法格式:
sql查询命令 INTO OUTFILE ‘文件名‘;
sql查询命令 INTO OUTFILE ‘目录名/文件名‘;
sql查询命令 INTO OUTFILE ‘目录名/文件名‘ fields terminated by "符号";
sql查询命令 INTO OUTFILE ‘目录名/文件名‘ lines terminated by "!!!";
sql查询命令 INTO OUTFILE ‘目录名/文件名‘ fields terminated by "符号" lines terminated by "符号";
mysql>select * from student into outfile "plj.txt";
mysql>select * from userdb.student into outfile "/tmp/plj8.txt";
#mkdir /mydata
#chown mysql /mydata
mysql>select * from userdb.student into outfile "/mydata/plj8.txt";
mysql>select name,uid,shell from userdb.student into outfile "/mydata/plj1.txt";
mysql>select name,uid,shell from userdb.student limit 4;
mysql>select name,uid,shell from userdb.student limit 4 into outfile "/mydata/plj3.txt" fields terminated by "##";
mysql>select name,uid,shell from userdb.student limit 4 into outfile "/mydata/plj7.txt" lines terminated by "!!!";
数据导出的注意事项:
导出的内容由SQL查询语句决定
若不指定路径,默认会放在执行导出命令时所在库对应的数据库目录下。
应确保mysql用户对目标文件夹有写权限。
目标位置文件具有唯一性
++++++++++++++++++++++++++++++++++++++++++++++++++++
管理表记录
插入新记录 insert into
一次插入一条记录 给记录的所有字段赋值
insert into 库.表 values(值列表);
一次插入多条记录 给记录的所有字段赋值
insert into 库.表 values(值列表),(值列表);
一次插入1条记录 给记录的指定字段赋值
insert into 库.表(字段名列表) values(值列表);
一次插入多条记录 给记录的指定字段赋值
insert into 库.表(字段名列表) values(值列表),(值列表);
insert into userdb.student values
(26,"bob","x",2077,2077,"this is student","/home/bob","/sbin/nologin");
insert into userdb.student values(27,"bob","x",2077,2077,"this is student","/home/bob","/sbin/nologin"),(28,"bob","x",2077,2077,"this is student","/home/bob","/sbin/nologin");
insert into userdb.student(name,uid,gid) values("alic",300,301);
insert into userdb.student(name,uid,gid) values("alic",300,301),("lucy",309,401);
++++++++++++++++++++++++
查询表记录 select
select 字段列表 from 表名;
select 字段列表 from 表名 where 表达式;
select * from student;
select id,name,homedir from student;
select 字段列表 from 表名 where 条件表达式;
select * from student where id=3;
select id,name,homedir from student where id=3;
条件的表示方式?
1 数值比较
字段名 符号 数字
= != > >= < <=
select * from student where id=5;
select * from student where id<=10;
select name,shell from student where uid=500;
select * from student where uid=500;
2 字符比较
字段名 符号 "值"
= !=
select name from student where name="zhangsan";
select * from student where name!="root";
3 范围内比较
between ... and ... 在...之间
in (值列表) 在....里
not in (值列表) 不在....里
select name,uid,homedir,shell from student where uid between 10 and 20;
select name from student where name in ("root","daemon","rsync");
select name,uid from student where uid in (2000,100,105,13);
select id,name,uid from student where id not in (1,10,20);
+++++++++++++++++++++++++++++++++++
4 匹配空 is null
匹配非空 is not null
insert into student(id,name)values(30,""),(31,null),(32,"null"),(33,NULL);
select id,name from student where name is null;
select id,name from student where name="null";
select id,name from student where name="";
select id,name from student where name is not null;
select id,name from student where shell is null;
select id,name from student where name like ‘%‘;(31不显示出来)
+++++++++++++++++++++++++++++++++++++++++++++++
逻辑比较(多个查询条件)
逻辑与 and 多个查询条件必须同时成立
逻辑或 or 多个查询条件某个条件成立就可以
逻辑非 ! 取反
and和or同时出现 默认先判断and再判断or, 或者在or判断加小括号就先判断or 再判断and.
select id,uid,name,homedir,shell from student where name="root" or uid=1 or shell="/bin/bash";
select name,uid from student where name="root" or name="bin" and uid=0
select name,uid from student where name="root" or name="bin" and uid=1 ;
select name,uid from student where (name="root" or name="bin" ) and uid=1 ;
+++++++++++++++++++++++++++++++++++++++++++++++++
6 四则运算 + - * / %
alter table student add age tinyint(2) default 21 after name;
alter table student add linuxsys int(2) default 60 after age, add linuxser int(2) default 60 after linuxsys;
select name,2016-age as s_year , age from student;
select name,year(now())-age as s_year , age from student;
select name,age,linuxsys,linuxser,(linuxsys+linuxser)/2 as pjf from student where name="root";
select name,age,linuxsys,linuxser,(linuxsys+linuxser) as zcj from student where name="root";
++++++++++++++++++++++++++++++++++++++++++++++++
7 模糊查询 like
where 字段名 like ‘表达式‘
_ 任意一个字符
% 零个或多个字符
select name from student where name like ‘___‘;
insert into student(name)values("a");
select name from student where name like ‘a%‘; 这是a开头的所有
select name from student where name like ‘_a_‘;
select name from student where name like ‘%a%‘; 只要还有a的都要
select id,name from student where name like ‘%‘; 所有的都出现
mysql> select name,uid from student where name like ‘_%_‘;两个或多个
mysql> select name,uid from student where name regexp ‘....‘; 出现名字是四个的或四个以上
mysql> select name,uid from student where name like ‘____‘; 出现名字是四个的
+++++++++++++++++++++++++++++++++++++++++++++++
8 在查询结果里过虑数据 having 条件
select id,name from student where name like ‘%‘ having id in (33,31);
select name from student where uid <500 having name="jim";
select name from stuent where uid<500 and name="jim";
select name from t1 where class="nsd1610" having name="lilei";
+++++++++++++++++++++++++++++++++++++
9 使用正则表达式做查询条件
^ $ . * [ ]
字段名 regexp ‘正则表达式‘
insert into student(name)values("plj9"),("pl8j"),("p7lj"),("6plj"),("1plj");
mysql> select name,uid from student where name regexp ‘^[0-9]‘;
select name,uid from student where name regexp ‘^a.*t$‘;
select name,uid from student where name regexp ‘^a.*t$‘ and shell="/sbin/nologin";
select name,uid from student where uid regexp ‘^..$‘;两位的
select name,uid from student where uid regexp ‘..‘;两位以上的都出现
mysql> select name,uid from student where uid regexp ‘...‘;三位的以上的都出现
mysql> select name,uid from student where uid regexp ‘^...$‘;三位的出现
mysql> select name,uid from student where name regexp ‘...‘;名字包含三个或三个以上的都出现
查询结果为四位数的:三种方式
select name,uid from student where uid regexp ‘^....$‘; ###
select name,uid from student where uid between 1000 and 9999;###
mysql> select name,uid from student where uid>1000;###
++++++++++++++++++++++++++++++++++++++++++++++++++++++
10 聚集函数:做数据统计的mysql服务自带的内置命令
max(字段名) 获取最大值
min(字段名) 获取最小值
avg(字段名) 获取平均值
sum(字段名) 求和
count(字段名) 获取字段值个数
select avg(linuxsys) from student;
select sum(linuxsys) from student;
select max(gid),min(uid) from student;
select count(name),count(id) from student; (id=57 name=55 是因为name有null)
select count(name) from student where shell is null;
select count(*) from student; 所有列个数
select count(id) from student; 所有列个数
mysql> select count(uid) from student where uid>=5 and uid<=10 ; (uid 大于5小于10的个数)
+++++++++++++++++++++++++++++++++++++
11 查询分组 group by 字段名
sql查询命令 group by 字段名;
select shell from student where uid<500 group by shell;
select 部门 from 员工信息表 where 性别="女" and 年龄<=25 and 工资>=10000 group by 部门;
12 查询排序 order by
sql查询命令 order by 字段名 排序方式
asc 升序(默认)
desc 降序
select uid from student where uid <500 order by uid;
select uid from student where uid <500 and shell!="/bin/bash" order by uid desc
++++++++++++++++++++++++++++++++++++++
13 limit 限制显示查询结果记录的行数。
SQL查询命令 limit 数字;
SQL查询命令 limit 起始行,共显示几行
第1行的编号是0(零)
select * from student limit 1;
select * from student limit 3;
select * from student limit 2,5;
select name,uid from student order by uid;
select name,uid from student order by uid desc limit 5;
select name,uid from student order by uid desc limit 5,10;
select * from student where uid is not null order by uid;
mysql> select * from student where uid is not null order by uid limit 2; ####
+++++++++++++++++++
mysql> select name from student where name order by name; ###
按名字排序:
++++++++++++++++++++
mysql> select name from student where name is not null having name="root"; (having.....过滤 查找,前面结果找)
+------+
| name |
+------+
| root |
| root |
mysql> select name from student where name is not null and name="root"; (查找 全表找)
+------+
| name |
+------+
| root |
| root |
++++++++++++++++++++++++++++++++++++++++++++
14 DISTINCT 不显示重复的值
select distinct shell from student; 每种出现一个
select shell from student group by shell;
select distinct shell from student where uid<500; (uid小于500的,每种出现一个)
++++++++++++++++++++++++++++++++
15 where 子查询
把内层查询结果做为外层的查询条件。
select 字段名列表 from 表名 条件 (select 字段名列表 from 表名);
insert into student(name,linuxsys)values("lili",38),("jerry",58),("lucy",59);
select avg(linuxsys) from student;
select name,linuxsys from student where linuxsys < (select avg(linuxsys) from student);
select id,name from student where name in (select userser from mysql.user where host="localhost");
mysql> select name from student
-> where
-> name in (select user from mysql.user where host="::1");
+------+
| name |
+------+
| root |
select lisi from 用户表 where lisi in(select 姓名 from db1.家庭地址表 where 城市="上海 ");
查找单课成绩小于这颗的平均分用户
select name,linuxsys from student where linuxsys < (select avg(linuxsys) from student);
+++++++++++++++++++++++++++++++++++++++++++
16 复制表(备份表 快速创建新表)
create table 新表名 sql查询命令;
复制全表(复制完后,新表不会继承索引)
create table 新表名 select * from 表名;
create table stu4 select * from student;
复制部分数据
create table 新表名 select 字段名列表
from 表名 where 条件;
create table stu3 select name,homedir,shell from student where uid<=10 ;
mysql> create table student5 select name,uid from student where uid>=20 ;
只复制表结构 (让后面的查找不成立为空,在复制就是空表)
create table 新表名 select * from 表名 where 1 = 2;
create table stu2 select * from student where 1 = 2;
mysql> create table student3 select * from student where 1 = 3;
+++++++++++++++++++++++
17 更新表记录update (修改记录字段的值)
批量修改
update 表名 set 字段名=值,字段名="值";
update student set age=18;
mysql> update student set linuxsys=100;
mysql> update student set linuxsys=05 where name="root";
修改符合条件的记录字段的值
update 表名 set 字段名=值,字段名="值" where 条件;
update student set name="zhangsan" where id=31;
update student set shell=null where id=31;
update student set shell="" where name="bin";
mysql> update student set linuxsys=null ;
mysql> update student set linuxsys=0 where name="root";
18 删除表记录 delete
删除表的所有记录。
delete from 表名;
只删除符合条件的记录
delete from 表名 where 条件;
delete from student where name is null;
delete from student where name="bob";
mysql> select * from student where name is null;
+----+------+------+----------+----------+----------+------+------+---------+---------+-------+
| id | name | age | linuxsys | linuxser | password | uid | gid | comment | homedir | shell |
+----+------+------+----------+----------+----------+------+------+---------+---------+-------+
| 53 | NULL | 21 | 100 | 70 | NULL | 1234 | NULL | NULL | NULL | NULL |
| 54 | NULL | 21 | 100 | 70 | NULL | 4565 | NULL | NULL | NULL | NULL |
+----+------+------+----------+----------+----------+------+------+---------+---------+-------+
2 rows in set (0.00 sec)
mysql> delete from student where name is null;
mysql> select * from student where name is null;
Empty set (0.00 sec)
19 多表查询:
select 字段名列表 from 表名列表;(笛卡尔集)
select 字段名列表 from 表名列表 where 条件;
*只显示与条件匹配记录字段的值。
create table t41 select name,uid from student limit 2;
create table t42 select name,uid,shell from student limit 4;
select * from t41,t42 where t41.uid = t42.uid;
select t41.name,t42.* from t41,t42 where t41.uid = t42.uid;
select t41.name,t42.name from t41,t42 where t41.uid = t42.uid;
+++++++++++++++++++++++++++++++++++++++++++++++++++
连接查询:
左连接查询(查询时以左边的表为主显示查询记录)
select 字段名列表 from 表1 left join 表2 on 条件;
右连接查询(查询时以右边的表为主显示查询记录,左表没有的记录用null与右表匹配)
select 字段名列表 from 表1 right join 表2 on 条件;
create table t43 select name,uid,shell from student limit 3;
create table t44 select name,uid,shell from student limit 5;
select * from t43 left join t44 on t43.uid = t44.uid;
select count( *) from t43 left join t44 on t43.uid = t44.uid;
select t43.name from t43 left join t44 on t43.uid = t44.uid;
select t43.* from t43 left join t44 on t43.uid = t44.uid;
select * from t43 right join t44 on t43.uid = t44.uid;
+++++++++++++++++++++++++
day03课程内容回顾:
数据导入
1 什么是数据导入
2 导入命令的语法格式
3 导入数据注意事项
数据导出
1 什么是数据导出
2 导出命令的语法格式
3 导出数据注意事项
管理表记录:
插入记录 insert into
查询记录 select
查询条件: 数值比较 字符比较
范围内查找
匹配空 匹配非空
逻辑比较
四则运算
模糊查询 like _ %
正则匹配 regexp ^ $ . * [ ]
聚集函数 max() min() avg()
sum() count()
查询排序
order by 字段名 desc/asc
查询分组
group by 字段名;
限制显示记录数
limit 数字
limit 数字1,数字2
在查询结果里过滤数据
having 条件
where嵌套查询
sql查询 where 字段 符号 (sql查询);
更新记录字段值
update 表名 set 字段名=值,字段名="值";
update 表名 set 字段名=值,字段名="值" where 条件;
update student set name="" where name="jim";
update student set shell=null where name="root";
update student set shell=null where id<=10;
update student set homedir="" where id<=10;
删除表记录
delete from 表名;
delete from 表名 where 条件;
复制表(快速创建新表 备份表)
*不会把原表的字段的索引属性复制给新表。
create table 新表名 sql查询 ;
+++++++++++++++++++++++++++++++++++++++++++++++++++++
day04
恢复数据库管理员从数据库服务器本机登录的密码。
#service mysql stop
#service mysql start --skip-grant-tables
#mysql
mysql> update mysql.user
-> set
-> password=password("654321")
-> where
-> host="localhost" and user="root";
mysql> flush privileges;
mysql> quit;
#service mysql stop
#service mysql start
#mysql -uroot -p654321
mysql>
修改数据库管理从本机登录的密码
[root@stu db100]#
mysqladmin -hlocalhost -uroot -p password "新密码"
Enter password: 旧密码
+++++++++++++++++++++++++++++++
用户授权及撤销
用户授权的作用:在数据库服务器上新添加一个连接数据库服务器的用户,并设置这个用连接到数据库服务器后的访问权限。
给谁授权? 使用者(网站服务器) -hip(ip是网站服务器主机ip) -u.. -p..
管理者(DAB)
* 默认只有数据库管理员root用户从服务器本机登录才有授权权限。
mysql> select user(); 查看当前登录用户
+----------------+
| user() |
+----------------+
| root@localhost |
+----------------+
mysql> show grants; 查看当前用户权限
____+_++++++++++++++++++++++++++++++++++++++++++++++++++
[root@19 ~]# mysql -uroot -p123456
mysql> grant all on *.* to jb@"localhost" identified by "123";
mysql> quit;
[root@19 ~]# mysql -ujb -p123
mysql> select user();
+--------------+
| user() |
+--------------+
| jb@localhost |
mysql> show grants;
+--------------------------------------------------------------------------------------------------------------------+
| Grants for jb@localhost |
+--------------------------------------------------------------------------------------------------------------------+
| GRANT ALL PRIVILEGES ON *.* TO ‘jb‘@‘localhost‘ IDENTIFIED BY PASSWORD ‘*23AE809DDACAF96AF0FD78ED04B6A265E05AA257‘ |
+--------------------------------------------------
++++++++++++++++++++++++++++++++++++++++++
授权的语法
mysql -hlocalhost -uroot -p999
mysql> grant 权限列表 on 数据库名 to 用户名@"客户端地址" identified by "密码" with grant option;
(客户端地址,ip 密码:登录时密码 它也有授权命令)
mysql> grant 权限列表 on 数据库名 to 用户名;
权限列表:
all 所有权限
select,update(name,age) 指定权限
usage 无权限
授权:
grant all on *.* to plj@"localhost" identified by "123456";
数据库名:
*.* 所有库和所有表
库名.* 一个库的权限
库名.表名 一张表的权限
用户名: 客户端连接数据库服务器时,使用的登陆名,授权时自定义即可,要有标识性。
客户端地址: 可选项
% 所有地址
172.40.50.117 一个IP地址
192.168.1.% 一个网段
pc100.tedu.cn 主机名
%.tedu.cn 域名
identified by "密码" 设置授权用户连接时使用的密码 可选项
with grant option 设置授权用户连接后,有授权权限 可选项
select user(); 显示登陆的用户名和客户端地址;
show grants; 登陆数据库服务器的用户查看自己的访问权限
例子
允许数据库管理员账号可以从117主机连接自己,连接后对所有库、表拥有完全权限,且有授权权限,连接的密码是plj123
grant all on *.* to root@"172.40.50.117" identified by "plj123" with grant option;
117:
mysql -h172.40.50.171 -uroot -pplj123
mysql>grant all on bbsdb.* to student@"%" identified by "123";
mysql>grant select,insert on userdb.student to studen2;
mysql>grant select,update(name,uid) on userdb.student to student3;
让jim用户可以在数据库本机登录数据库服务对库表有完全权限 登陆密码是123456
grant all on *.* to jim@"localhost" identified by "123456";
+++++++++++++++++
默认的4个数据库:
information_schema 虚拟库 +++存储在内存 不占用硬盘存储空间(在/var/lib/mysql下没有文件夹)+++
performance_schema 服务运行时的参数信息
mysql 授权库
test 公共库 只要用户能够连接到服务上对此库就有完全权限
++++++++++++++++++
授权信息存储在mysql库里
user 授权用户的访问权限
db 授权用户对库的访问权限
tables_priv 授权用户对表的访问权限
columns_priv 授权用户对字段的访问权限
查看已有的授权用户和连接的客户端地址:
select user,host from mysql.user;
mysql> select user from mysql.user;
+---------+
| user |
+---------+
| student |
| root |
| root |
| root |
| weadmin |
| weadmin |
| root |
| jb |
| plj |
| root |
+---------+
mysql> select user,host from mysql.user;
+---------+---------------+
| user | host |
+---------+---------------+
| student | % |
| root | 127.0.0.1 |
| root | 19.tedu.cn |
| root | 192.168.4.254 |
| weadmin | 192.168.4.254 |
| weadmin | 192.168.4.5 |
| root | ::1 |
| jb | localhost |
| plj | localhost |
| root | localhost |
+---------+---------------+
查看已有授权用户的访问权限:
show grants for 用户@"客户端地址";
mysql> show grants for student@"%";
没有明确授权时,用户不能管理test库,
mysql> delete from mysql.db where user="";
Query OK, 0 rows affected (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
撤销用户的权限:
revoke 权限列表 on 库名 from 用户@"客户端地址";
revoke grant option on *.* from ‘root‘@‘172.40.50.117‘;
revoke drop,delete on *.* from ‘root‘@‘172.40.50.117‘;