安装
centos6安装
#注意大小写
yum -y install MariaDB-Galera-server MariaDB-client galera
#开启mysql
service mysql start
#进行初始化安装设置
mysql_secure_installation
设置密码(其他一路回车)
#进入mysql
mysql –uroot –p密码;
centos7安装
yum –y install mariadb mariadb-service
#开启mysql
systemctl start mariadb
启动
#设置开机自
systemctl enable mariadb
#进行初始化安装设置
mysql_secure_installation
设置密码(其他一路回车)
#进入mysql
mysql –uroot –p密码;
数据库常识
create datebase wzsheng; #创建wzsheng数据库
drop database DB; #删除用户的数据库
use wzsheng; #进入wzsheng数据库
show tables; #显示表
select * from wu; #显示wu表所有列
create user wu@localhost identified by "cnhope"; #创建wu用户密码为cnhope
rename user wu to zhi; #将用户wu改为zhi名
drop user zhi; #删除zhi用户
grant select,insert,update,delete on wzsheng.* to zhi@localhost; #用户授权
revoke select on wzsheng.* from zhi@localhost; #回收用户权限
show grants for zhi; #查看zhi用户权限
flush privileges; #刷新权限
破解/设置密码
#修改用户当前密码
set password=password('cnhope');
#修改root用户密码
update mysql.user set Password=password('123456') where user='root';
#修改指定用户密码
update mysql.user set password=password('新密码') where user="test" and host="localhost";
#破解mysql密码
vim /etc/my.cnf
[mysqld]
skip-grant-tables
service mysql restart
#mysql
use mysql;
update user set password=password(‘cnhope’) where user=’root’;
exit
service mysql restart
创建/更新 表
MariaDB [wzsheng]> create table wu
-> (
-> wu_id int not null auto_increment,
-> wu_name char(50) not null,
-> wu_address char(100) null,
-> wu_city char(50) null,
-> primary key (wu_id) #主键,多个列则用, 隔开
-> )engine=innodb; #引擎类型
show create table sheng; #查看创建sheng表时格式
# 更新表 alter table
#向表wu添加个wu_sex列
MariaDB [wzsheng]> alter table wu add wu_sex char(20);
向条wu删除wu_sex列
MariaDB [wzsheng]> alter table wu drop column wu_sex;
# 重命名表
MariaDB [wu]> rename table zhi to wu;
重名名多个表则直接用, 隔开
插入数据
# 方法一:
MariaDB [wu]> insert into wu
-> values(null,
-> 'wu',
-> 'shengzheg',
-> 'sz');
# 方法二:
MariaDB [wzs]> insert into customers(cust_name,
-> cust_address,
-> cust_city,
-> cust_state,
-> cust_zip) #定义要插入的行数,没有的在表显示null
-> values('zhi',
-> 'jingtian',
-> 'shengzheng',
-> 'hahah',
-> 'jg');
# 插入多行
MariaDB [wzs]> insert into customers(cust_name,
-> cust_address,
-> cust_city,
-> cust_state)
-> values( #按上面顺序一次填写
-> 'sheng',
-> 'ketang',
-> 'shangwei',
-> 'hahah'),
-> ( //第二行
-> 'xue',
-> 'daxuecheng',
-> 'guangzhou',
-> 'student');
# 将表数据插入另一个表 将custnew中的数据插入 customers 中
创建一个表wu
MariaDB [wzs]> create table wu(
-> wu_id int not null auto_increment,
-> wu_name char(50),
-> wu_address varchar(50),
-> wu_city varchar(50),
-> wu_state char(30),
-> wu_zip varchar(50),
-> wu_country char(50),
-> cust_contact char(40),
-> wu_email varchar(50),
-> primary key(wu_id))engine=myisam;
#将customers数据导入表wu中
MariaDB [wzs]> insert into wu(wu_id,
-> wu_name,
-> wu_address,
-> wu_city,
-> wu_state,
-> wu_zip,
-> wu_country,
-> cust_contact,
-> wu_email)
-> select cust_id,
-> cust_contact,
-> cust_email,
-> cust_name,
-> cust_address,
-> cust_city,
-> cust_state,
-> cust_zip,
-> cust_country from customers;
知识点:
insert low_priority into wu //降低insert语句的优先级
更新/ 删除数据
# update
MariaDB [wzsheng]> update customers #1.更新表
->set cust_email = 'wu@com' 2.列名=更新值
-> where cust_id = 10005; 3.更新过滤条件
update 更新多行时若 有一个列名写错 则全部不更新
update ignore 则会 继续跟新 除了写出的列名外 (并且会显示那列错误)
delete
delete 只删除行
MariaDB [wu]> delete from wu where wu_id=4;
没加where 则删除表全部数据 但不会删除表
delete from wu; //删除表所有行:一个一个删除或者
truncate table wu; //速度快:删除原来的表再重新建一个
update 删除指定的列
MariaDB [wu]> update wu set wu_city=' ' where wu_id=1;
MariaDB [wu]> update wu set wu_city= ' '; 没有where则更新整个表的列
在操作中 delete和update最好先用 select 调试下
查询
# distinct 返回唯一值
select distinct wu_id from products;
# limit限制显示数量
select name from wu limit 5 #显示name信息不多与5行
select name from wu limit 5,5 #在表中name列第5行起(就从6行开始) 显示5条信息
#order by显示顺序
select name from wu order by name #从wu表中显示name并且name中按字母顺序排序显示
select id,name from wu order by id,name #先按id排序( 从小到大)当遇到相同则按 name开头字母顺序排序
#desc 降序
select id,name from wu order by id desc,name; #id按降序排序
MariaDB [wzs]> select prod_price from products order by prod_price desc limit 1; #显示最大的数
# where指定搜索条件
#select 目标 from 表 where 条件
select id from product where name = "RT-AC68U";
#order by 使用在where 后面
select name , price from wu where id = 2 or id =3 order by name; #在表wu中显示满足id为2 或 3并且按name顺序 显示name和price数据
#WHERE子句操作符
![](https://oscimg.oschina.net/oscnet/3568b41fc6dda8f5521cc9566a4b3c0c7d9.jpg)
select id, price form wu where id=3 and price <=10; #显示要满足ID=3 和price=10的数据
select id, price form wu where id=3 or id=10; #选择id是3或者10
#数据过滤
#and 优先集比or高 所以 or 需要加 ()
MariaDB [wzs]> select * from products where (vend_id=1002 or vend_id=1003) and prod_price <=10;
#in 指定条件范围
select id,name from wu where id in(1002,1003) ;
#not 否定 条件
select id,name from wu WHERE id NOT IN (1002,1003) ORDER BY name; #不显示ID=1002和1003
MariaDB [wzs]> select prod_name,prod_price from products where prod_price between 5 and 10;
#检查是否存在空值
#显示prod_price的null的行
MariaDB [wzs]> select prod_name from products where prod_price is null;
#通配符使用like
#% 可以匹配0个或多个字符,但是单独 % 不能匹配
SELECT id,name FROM wu WHERE name like zhi%; # 搜索zhi开头的name数据
%zhi% s%e
_下滑线只能匹配单个字符
#正则表达式 regexp
like(匹配整个列, 如果被匹配的文本在列值
中出现, LIKE将不会找到它)和regexp(在列值内进行匹配) 区别
#like 只显示 +通配符的数据 若直接+确切数据则不显示
而 REGEXP 都行
显示含1000的name数据
SELECT name FROM wu WHERE name LIKE/REGEXP '1000' 只有REGEXP语句才有显示数据
#区分大小写 binary
WHRER name REGEXP BINARY 'Jet .00';
#OR 或 |
SELECT name FROM wu WHERE name ERGEXP '100|200' ORDER BY name 显示含100或200 name 数据
#[123]zhi 是OR 的另一种形式
查询 1zhi 或 2zhi 或 3zhi
[1-9] 1到9 数据
[a-z] a到z 字母
\\f 换页
\\n 换行
\\r 回车
\\t 制表
\\v 纵向制表
*0个或多个
+ 一个或多个 = {1,}
? 0个或多个 = {0,1}
{n} 指定数目匹配
{n,} 不少于指定数目匹配
{n,m} 匹配n到m范围 m不超过255
#显示1到5的Ton
MariaDB [wzs]> select prod_name from products where prod_name regexp '[1-5] Ton' order by prod_name;
#格式\\( \\)
SELECT name FROM wu WHERE name REGEXP '\\([0-9] wu?\\)' ORDER BY name
可显示 1 wus 2wu 等
定位符
^ 文本的开始 和 否定
$ 文本的结尾
[[:<:]] 词的开始
[[:>:]] 词的结尾
#拼接字段 concat() 别名:AS
#需要单个值,而表中数据存储在两个列vend_name和vend_country中
SELECT concat(name ,'(',country,')') from wu order by name;
#显示 zhisheng(Chian) 表名显示为(name ,'(',country,')')
#AS别名 为修改表名
SELECT CONCAT(name ,'(',country,')') AS name_tile from wu order by name;
#显示 zhisheng(Chian)表名为 name_tile
运算符
+ - × /
SELECT id ,number, price,number*price AS allnumber FROM wu WHERE order_num = 2005
#显示为 其中allnumber 为 number 和 price 乘积
使用数据处理函数
// Upper 将文本转换文大写
MariaDB [wzsheng]> select vend_name ,Upper(vend_name)
-> as vend_name_upcase
-> from vendors
-> order by vend_name;
// soundex() 匹配所有发音类似于Y.Lie的联系名:
MariaDB [wzsheng]> select cust_name,cust_contact
-> from customers
-> where soundex(cust_contact) = soundex('Y Lie');
// 日期date(order_date) 时间 time()
MariaDB [wzsheng]> select cust_id,order_num
-> from orders
-> where date(order_date) = '2005-09-01';
汇总数据
//AVG()
MariaDB [wzsheng]> select avg(prod_price)
-> as avg_price
-> from products;
//count()
MariaDB [wzsheng]> select count(*)
-> as num_cust
-> from customers;
//电子邮件客户数量
MariaDB [wzs]> select count(cust_email) from customers;
//COUNT(*)对表中行的数目进行计数,null空值还是not null
//COUNT(column)对特定列中具有值的行进行计数,忽略空值
MariaDB [wzsheng]> select count(*) as mun_items,
-> min(prod_price) as price_min,
-> max(prod_price) as price_max,
-> avg(prod_price) as price_avg
-> from products;
//sum
MariaDB [wzs]> select sum(quantity*item_price) as total_price
-> from orderitems
-> where order_num=20005;
//分组数据联合
//GROUP BY 必须出现在where子句之后 order by 之前
//where 显示vend_id = 1003的信息
MariaDB [wzsheng]> select count(*) as num_prods
-> from products
-> where vend_id = 1003 ;
//创建分组
//group by显示一组vend_id
//group by句必须出现在WHERE子句之后, ORDER BY子句之前
MariaDB [wzsheng]> select vend_id,count(*) asnum_prods
-> from products
-> group by vend_id;
//在尾部+ WITH ROLLUP, 可以得到每个分组以及每个分组汇总级别(针对每个分组)的值
//过滤分组
//having 过滤分组 WHERE过滤行( WHERE没有分组的概念) 且having可完全取代where
MariaDB [wzsheng]> select cust_id, count(*) as orders
-> from orders
-> group by cust_id
-> having count(*) >=2;
此处不能用where,该过滤是基于分组聚集值而不是特定行值。
//having(分组后过滤) WHERE(分组前过滤)
MariaDB [wzsheng]> select vend_id,count(*) as num_prods
-> from products
-> where prod_price >= 10 在分组前过滤掉price>=10
-> group by vend_id
-> having count(*) >= 2;
MariaDB [wzsheng]> select vend_id,count(*) as num_prods
-> from products
-> group by vend_id
-> having count(*) >= 2;
//分组和排序
//group by 和order by 联合使用
MariaDB [wzsheng]> select order_num,sum(quantity*item_price) as ordertotal
-> from orderitems
-> group by order_num
-> having sum(quantity*item_price) >=50
-> order by ordertotal;