原文地址 :https://gitee.com/gaohaixiang192/study-node/blob/master/mysql-sql.md
库操作
查看现有库
show databases;
切换到指定库
use sys;
确认当前所在库
select database();
建库
create database testdb;
create database testdb character set utf8 collate utf8_bin;
删库
drop database testdb;
表操作
查看 testdb 库中的表
use testdb;
show tables;
查看 testtable 表结构
desc testtable;
desc testtable ;
desc testdb.testtable;
新建表
create table testtable(
name char(16) not null,
password char(48) default '',
parimary kye(name)
)default charset=utf8;
查看表的实际创建指令
show create table testdb.testtable;
删除表
drop table testtable;
各种时间函数的使用
1)使用now()查看当前的日期和时间
mysql> SELECT now();
+
| now() |
+
| 2017-04-02 04:02:42 |
+
2)使用sysdate()查看系统日期和时间
mysql> SELECT sysdate();
+
| sysdate() |
+
| 2017-04-02 04:03:21 |
+
3)使用curdate()获得当前的日期,不含时间
mysql> SELECT curdate();
+
| curdate() |
+
| 2017-04-02 |
+
4)使用curtime()获得当前的时间,不含日期
mysql> SELECT curtime();
+
| curtime() |
+
| 04:04:55 |
+
5)分别获取当前日期时间中的年份、月份、日
mysql> SELECT year(now()),month(now()),day(now());
+
| year(now()) | month(now()) | day(now()) |
+
| 2017 | 4 | 2 |
+
6)获取系统日期时间中的月份、日
mysql> SELECT month(sysdate()),day(sysdate());
+
| month(sysdate()) | day(sysdate()) |
+
| 4 | 2 |
+
7)获取系统日期时间中的时刻
mysql> SELECT time(sysdate());
+
| time(sysdate()) |
+
| 04:06:08 |
+
修改表结构
建表
create table testdb.testtable(
id int(4) primary key,
name varchar(4) not null,
age int(2) not null
);
添加一个 address 字段
alert table testdb.testtable add address varchar(48);
alert table testdb.testtable add enum('boy','girl') after age;
修改 gender 字段名为 sex,并添加非空约束
alert table testdb.testtable change gender sex enum('boy','girl') not null;
删除字段,删除 sex 字段
alert table testdb.testtable drop sex;
索引创建与删除
建表时指定 index,name 索引字段
create database home;
use home;
create table tea4(
id char(6) not null,
name varchar(6) not null,
age int(3) not null,
gender enum('boy','girl') default 'boy',
index(id),index(name)
);
删除表中某个 index 索引字段,删除 name 的 index 索引字段
drop index name on tea4;
在已有表中设置一个 index 索引字段,为 age 字段建立索引,名为 nianling
create index nianling no tea4(age);
查看指定表的索引信息
show index from tea4\G;
创建表时指定 unique 索引字段,
- unique 便是唯一,同一个表可以有多个字段具有唯一性
- 创建tea5表,将id,name建立unique索引,age设置index索引
create table tea5(
id char(6),
name varchar(4) not null,
age int(3) not null,
unique(id),unique(name),index(age)
);
删除 unique 索引,在已有表中设置 unique 索引字段
drop index name on tea5;
- 重新为tea5表的name字段建立unique索引
create unique index name on tea5(name);
建表时设置 primary key 主键索引
- 每个表中主键只有一个
- 可以在某个字段的 约束条件中指定主键,也可以在最后指定
create table biao01(
id int(4) primary key,
name vachar(8)
);
create table biao02(
id int(4),
name varchar(8),
primary key(id)
);
- 建表时,某个主键字段为int,可以为其设置 auto_increment 自增属性
create table tea6j(
id int(4) auto_increment,
name varchar(4) not null,
age int(2) not null,
primary key(id)
);
删除现有表的 primary key 主键索引
alert table biao01 drop primary key;
- 若想清除带有自增属性的主键,需要先清除自增属性-修改id列的字段定义
alter table tea6 modify id int(4) not null;
alert table tea6 drop primary key;
为现有表添加 primary key 主键索引
alert table tea6 add primary key(id);
设置外键实现数据同步更行与删除
create table yg(
yg_id int(4) auto_increment,
name char(16) not null,
primary key(yg_id)
);
create table gz(
gz_id int(4) not null,
name char(16) not null,
gz flot(7,2) not null default 0,
index(name),
foreign key(gz_id) peferences yg(yg_id)
on update cascade on delete cascade
);
insert into yg(name)values('zhangsan'),('lisi');
insert into gz(gz_id, name, gz,) values(1,'zhangsan',12000),(2,'lisi',8000);
update yg set yg_id=1234 where name='zhangsan';
slect * from gz where name='zhangshan';
delete from yg where name='zhagnsan';
select * from gz where name='zhangsan';
存储引擎配置
查看存储引擎信息
show engines;
查看默认存储类型
show variables like 'default_storage_engine';
修改默认存储引擎
set default_storage_engine=MyISAM;
修改配置文件,重启使存储引擎生效
vi /etc/my.cnf
[mysqld]
...
default_storage_engine=MyISAM
...
数据的导入导出,
新建userdb库,ueser表,数据导入
create database userdb character set utf8 collate utf8_bin;
CREATE TABLE user(
username varchar(24) NOT NULL,
password varchar(48) DEFAULT 'x',
uid int(5) NOT NULL,
gid int(5) NOT NULL,
fullname varchar(48),
homedir varchar(64) NOT NULL,
shell varchar(24) NOT NULL
);
show variables like '%secure%';
load data infile '/var/lib/mysql-files/passwd'
into table userlist
fileds terminated by ':';
alert table user add sn int(4) auto_increment primary key first;
从数据库中导出查询结果
vi /etc/my.cnf
[mysqld]
secure_file_priv='/myload'
重启数据库
show varable like "secure_file_priv";
select * from userdb.user where uid<100
into outfile '/myload/user2.txt'
fields terminated by ':';
操作表数据
创建表
create table testdb.testtable(
name varchar(12) not null,
gender enum('boy','girl') default 'boy',
age int(3) not null
);
删除表的所有记录
delete from testdb.testtable
插入表数据
insert testdb.testtable values
('zhangshan','boy',24),
('lixiaolu','girl',25),
('wangwu','girl',23);
insert into testdb.testtable(name,age)
values('Mike',22);
更新表数据
update testdb.testtable set age=10;
update testdb.testtable set age=20
where general='boy';
删除表数据
delete from testdb.testtable where age < 18;
delete from testdb.testtable;
查询及匹配条件
表中插入数据
insert into testdb.testtable values
('Jim','girl',24),
('Tom','boy',21),
('Lily','girl',20),
('Jerry','boy',27),
('Mike','boy',21)
;
常用的统计函数
select count(*) from testdb.testtable;
select avg(age),max(age),min(age) from testdb.testtable;
select count(gender) from testdb.testtable where gender='boy';
字段值的数值比较
select * from testdb.testtable where age=21;
select * from testdb.testtable where age>21;
select * from testdb.testtable where age=<21;
select * from testdb.testtable where age between 20 and 24;
多个条件的组合
select * from testdb.testtable where age=<21 and gender='girl';
select * from testdb.testtable where age>21 or gender='girl';
select * from testdb.testtable where name in ('Jim','Tom','Mickey');
使用select做数学计算
select 1234+5678;
select 1234*5678;
select 1234/5678;
select name,age+15 from testdb.testtable;
模糊查询,like
_匹配单个字符,%匹配多个字符
select * from testdb.testtable where name like 'J%';
select * from testdb.testtable where name like 'J__';
使用正则表达式,regexp
select * from testdb.testtable where name regexp '^J.*y$';
select * from testdb.testtable where name like 'J%y';
select * from testdb.testtable where name regexp '^J|y$';
select * from testdb.testtable where name like 'J%' or name like '%y';
指定字段排序,默认升序
select * from testdb.testtable group by age;
select * from testdb.testtable group by age asc;
select * from testdb.testtable group by age desc;
限制输出的条数,limit
select * from testdb.testtable limit 3;
select * from testdb.testtable group by age desc limit 3;
分组查询,group by
select gender,count(gender) from testdb.testtable group by gender;
select gender as '性别',count(gender) as '人数'
from testdb.testtable group by gender;
用户授权及撤销
create user 'syncuser'@'%' identified with mysql_native_password by '123456';
grant all privileges on *.* to 'syncuser'@'%';
grant select on *.* to 'syncuser'@'%';
flush privileges;
show grant for 'syncuser'@'%';
revoke all on *.* from 'syncuser'@'%';
视图
查看某个库的所有视图
show tables status where comment='view';
作用
1、提高了重用性;
2、对数据库重构,却不影响程序的运行;
3、提高了安全性能,可以对不同的用户,设定不同的视图;
4、让数据更加清晰。
select a.name as username,
b.name as goodsname
from user as a,
goods as b, ug as c
where a.id=c.userid and c.goodsid=b.id;
create view other as
select a.name as username,
b.name as goodsname
from user as a, goods as b, ug as c
where a.id=c.userid and c.goodsid=b.id;
select * from other;
create view user as
select a.name,a.age,b.sex
from usera as a, userb as b where a.name=b.name;
create view other as select a.name, a.age from user as a;
视图的基本使用
1、什么是视图:是一种虚拟存在的表
2、内容与真实的表相似,包含一系列带有名称的列和行数据。
3、视图并不在数据库中以存储的数据的形式存在。
4、行和列的数据来自定义视图时查询所引用的基本表,并且在具体引用视图时动态生成。
5、更新视图的数据,就是更新基表的数据
6、更新基表数据,视图的数据也会跟着改变
create table testdb.user(
username char(20),
password char(1),
uidint(2),
gid int(2),
comment char(100),
homedir char(100),
shell char(50));
load data
infile "/var/lib/mysql-files/passwd"
into table testdb.user fields terminated
by ":" lines terminated by "\n";
alert table testdb.user add id int(2) parimary key auto_increment first;
use testdb;
create view userv1 as select * from user;
create view userv2 as shell from user;
insert into userv1(username,uid) values("jarry",9);
update userv1 set uid=9 where username='adm';
delete from userv1 where uid=9;
drop view userv1;
drop view userv2;
创建视图完全格式
create table user2 select username,uid,gid from user limit3;
create table info select username,uid,homedir,shell form user limit 5;
select * from user2 left join info on user2.username=info.name;
create view v4 as
select a.username as ausername,
b.username as busername,
a.uid as auid,
b.uid as buid
from user2 a left join info b on a.username=b.username;
select * from v4;
desc v4;
or replace 选项的使用
create or replace view v4 as
select a.username as ausername,
b.username as busername,
a.uid as auid,
b.uid as buid
from user2 a left join info b on a.username=b.username;
with local check option
create table user1 select username,uid,shell from user where uid>=5 and uid <=40;
create view v1 as select username,uid from user1 where uid<=20;
update v1 set uid=21 where username="sync";
update user1 set uid=41 where username="ftp";
create table a select * from user where uid < 10;
create view v3 as select * from a where uid < 10 with check option;
update v3 set uid=9 where username="adm";
create view v2 as select * from v1 where uid >= 5 with local check option;
update v2 set uid=9 where username="sync";
with cascaded check option
create view v5 as select * from v1 where uid >= 5 with cascaded check option;
存储过程
作用
1、增强SQL语句的功能和灵活性;
2、减少网络流量,从而可降低网络负载;
3、提高数据库的访问效率;
4、提高数据库的安全性和数据的完整性;
5、使数据独立,达到解耦的效果。
创建存储过程
delimiter
create procedure say()
begin
select * from user id<=10;
end
delimiter ;
call say();
查看存储过程
show procedure status ;
select db,name,type from mysql.proc where name="say";
删除存储过程
drop procedure say;
创建一个显示shell是 /bin/bash 的用的 存储过程
delimiter
create procedure pl()
begin
select count(username) from user where shell="/bin/bash";
end
delimiter ;
call pl();
存储过程参数的使用
delimiter
create procedure say2(in username char(10))
begin
select username;
select * from where username=username;
end
delimiter ;
call say2("tom");
delimiter
create procedure p2(out number int)
begin
select count(username) into @number from user where shell!="/bin/bash";
select @number ;
end
delimiter ;
call p2(@number)
使用循环结构
算数运算
set @z=1+2;select @z;
set @x=1;set @y=2;set @z=@x*@y;select @z;
set @x=1;set @y=2;set @z=@x-@y;select @z;
delimiter
create procedure say5(in bash char(20), in nologin char(25), out x int , out y int)
begin
declare z int ;
set z=0;
select count(username) into @x from user where shell=bash;
select count(username) into @y from user where shell=nologin;
set z=@x+@y;
select z;
end
delimiter ;
call say5("/bin/bash","/sbin/nologin",@x,@y);
delimiter
create procedure say6(in x int(1) )
begin
if x <= 10 then
select * from user where id <=x;
end if;
end
delimiter ;
call say6(1);
call say6(2);
delimiter
create procedure p3(in linenum char(10) )
begin
if linenum is null then
set @linenum=1;
select * from user where id=@linenum;
else
select linenum;
select * from user where id=linenum;
end if;
end
delimiter ;
call p3(null);
call p3(3);
|