Linux数据库管理与应用(MySQL已经改名为mariadb)
l 检查安装源上有什么mariadb相关的软件
yum list|grep mariadb
l 安装源上mariadb
yum install mariadb-libs mariadb mariadb-devel mariadb-embedded mariadb-server mariadb-test
l 启动MySQL:
service mariadb start
ps ax 检查是否有mysql进程(要有2个进程)
l 设置数据库超级管理员密码:
mysql -u root mysql
set password=password('gdupt111111');
l 退出mysql:
quit
l 进入mysql:
mysql -u root -p
l MySQL系统常用快捷命令
help ? 帮助
status \s 显示MySQL状态信息
quit \q 退出mysql
l 查看数据库
show databases;
l 新建数据库:
create database test01 charset=utf8;
l 为test01数据库开通访问的账号及密码:
允许在本机中用账号dbadmin密码123456操作test01数据库:
grant all on test01.* to dbadmin@localhost identified by '123456';
grant all on test01.* to dbadmin@127.0.0.1 identified by '123456';
允许IP地址为111.45.2.%的终端用账号dbadmin密码123456操作test01数据库:
grant all on test01.* to dbadmin @'111.45.2.%' identified by '123456';
允许所有IP地址的终端用账号dbadmin密码123456操作test01数据库:
grant all on test01.* to dbadmin @'%.%.%.%' identified by '123456';
更新权限:
flush privileges;
l 删除数据库:(慎用)
drop database test01;
l 进入数据库:
use test01;
l 建立student数据库,在该数据库中建立studentinfo表:
create database student charset=utf8;
use student;
# 然后建立学生信息表studentinfo:
create table studentinfo
(
id int(11),
name varchar(20),
sex varchar(4),
yuwen float,
shuxue float,
yingyu float
);
l 删除表:(慎用)
drop table 表名;
l 查看数据库及字符编码
show create database student;
l 修改数据库字符编码
alter database student default character set gbk collate gbk_bin;
l 显示表:
show tables;
l 查看表详细信息:
describe studentinfo;
l mysql常用的数据类型
整数类型:int、bigint
浮点数类型:float、double
日期类型:
year 1999
date YYYY-MM-DD current_date获取日期
time HH:MM:SS current_time或者now()获取时间
datetime YYYY-MM-DD HH:MM:SS now()获取
字符串:
char 不变长
varchar 可变长
binary 二进制数据
text 大文本数据(例如:文章内容、评论等)
longtext 大文本数据
blob类型:
blob 特殊二进制(例如:图片、pdf文档)
longblob
l 修改表名
alter table 旧表名 rename to 新表名;
例:
alter table studentinfo rename to student;
l 修改字段名
alter table 表名 change 旧字段名 新字段名 新数据类型;
l 修改字段的数据类型
alter table 表名 modify 字段名 数据类型;
l 在表中添加字段
alter table 表名 add 新字段名 数据类型;
l 删除字段
alter table 表名 drop 字段名;
l 修改字段的排列位置
alter table 表名 modify 字段名1 数据类型 first|after 字段名2
约束
l 主键约束primary key
字段名 数据类型 primary key
例:
create table example01
(
id int primary key,
name varchar(32)
);
主键约束字段不能重复值并且不为null。
l 非空约束not null
字段名 数据类型 not null
例:
create table example02
(id int primary key,
name varchar(32) not null
);
l 唯一约束unique
字段名 数据类型 unique
例:
create table example03
(
id int primary key,
stu_id int unique,
name varchar(32) not null
);
l 默认约束default
字段名 数据类型 default 默认值
例:
create table example04
(
id int primary key,
stu_id int unique,
name varchar(32) not null,
chengji float default 0
);
l 设置字段值自动增加
字段名 数据类型 auto_increment
例:
create table example05
(
id int primary key auto_increment,
stu_id int unique,
name varchar(32) not null,
chengji float default 0
);
索引
l 创建索引
例:
create table example06
(
id int primary key auto_increment,
name varchar(32) ,
index(name)
);
l 删除索引
alter table 表名 drop index 索引名;
插入
l 插入一条数据:
insert into studentinfo (id,name,sex,yuwen,shuxue,yingyu) values(101,’zhangsan’,’nv’,99,88,77);
l 同时插入多条数据
insert into studentinfo (id,name,sex,yuwen,shuxue,yingyu) values(102,’zhangyu’,’nv’,86,51,72),(103,’lisi’,’nan’,44,55,66),(104,’wangwu’,’nan’,70,68,91),(105,’zhaoliu’,’nan’,45,68,71),(106,’liangming’,’nv’,55,68,34),(107,’chenhong’,’nv’,67,45,38);
查询
l 查询所有字段
select * from studentinfo;
l 查询指定字段
select 字段名1,字段名2,…… from 表名;
l 条件查询
select 字段名1,字段名2,…… from 表名 where 条件表达式;
l 多条件查询
select 字段名1,字段名2,…… from 表名 where 条件表达式1 and 条件表达式2 and ……;
select 字段名1,字段名2,…… from 表名 where 条件表达式1 or 条件表达式2 or ……;
l 带between and关键字查询
select 字段名1,字段名2,…… from 表名 where 字段名[not] between 值1 and 值2;
l 空值查询
select 字段名1,字段名2,…… from 表名 where 字段名 is [not] null;
l 带like关键字的匹配查询
select 字段名1,字段名2,…… from 表名 where 字段名[not] like ‘匹配字符串’; (注:%可以匹配任意长度,_匹配单个字符)
l 聚合函数查询
select count(*) from 表名;
select sum(字段名) from 表名; 求和
select avg(字段名) from 表名; 求平均值
select max(字段名) from 表名; 求最大值
select min(字段名) from 表名; 求最小值
l 查询结果排序
select 字段名1,字段名2,…… from 表名 order by 字段名 asc|desc; (升序|降序)
l 限制查询结果数量
select 字段名1,字段名2,…… from 表名 limit 记录数量;
l 修改数据:
update studentinfo set name='zhangsi',yuwen=66 where id=101;
update studentinfo set yingyu=100 where id>102;
l 删除数据:
delete from studentinfo where id=103;
l 清除表中所有数据
truncate table 表名;