mysql基本数据库操作
2017-03-01 14:13
459 查看
1,将表中数据导出到文件中
eg:select * from tb_contact into outfile '/tmp/contact.txt' fields terminated by ',' optionally enclosed by '"' lines terminated by '\n';
2,将文件数据导入到数据库表中(local关键字指定文件路径为客户机,默认为服务器路径)
eg:load data local infile '/tmp/contact.txt' into table tb_contact /*(contactId,id,groupName)*/ fields terminated by ',' lines terminated '\r\n';
3,数据库脚本整体备份
eg:mysqldump -uroot -p123 tb_contact > contact.dump;
4,mysqlimport导入数据
eg:mysqlimport -uroot -123 --local --columns=a,b,c db_test F:/contact.txt
5,防止SQL注入
a,表单的双向验证
b,对SQL错误进行自定义处理
c,对输入数据进行加密处理
d,使用存储过程加上权限控制来进行所有的查询
e,通过专业的漏洞检查工具来查找漏洞
6,对重复数据的忽略插入
eg:insert ignore into tb_contact values(4,2,"好友");
7,对重复数据的覆写插入
eg:replace into tb values(4,2,"好友");
8,统计某列的重复数据
eg:select count(*) as repet from tb_contact group by groupName having repet>1;
9,过滤重复的数据
eg:select distinct groupName/*指定不重复的列*/ from tb_contact oreder by groupName;
10,删除重复数据
eg:create tablt tmp select * from tb_contact group by group groupName;
drop table tb_contact;
alter table tmp rename to tb_contact;
/*通过动态添加主键的方式消除重复的值*/
alter table tb_contact add primary key (contactId);
11,删除主键
eg:alter table tb_contact drop primary key;
12,创建序列化表
eg:create table tb_contact(
id int not null auto_increment=100,
contactId int not null default 10000,
groupName varchar(50) not null,
primary key(id)
unique (contactId)
);
13,获取元数据
eg:select version();
select user();
select database();
show status;
show varlabes;
14,查询表结构
eg:show create table tb_contact;
15,创建临时表
eg:create termpoary table tmp(
id int not null primary key,
contactId int not null
);
16,增加/删除/修改字段
eg:alter table tb_contact drop id;
alter table tb_contact add i int /*first第一列,after contactId 在某一列之后*/;
alter table tb_contact modify i varchar(10) not null default 100;
alter table tb_contact change i j varchar(10);
alter table tb_contact change i i varchar(10);
alter table tb_contact alter i set default 1000;
alter table tb_contact alter i drop default;
alter table tb_contact rename to tb_temp;
17,正则表达式的使用
eg:select * from tb_contact where groupName regexp 'ok$';
18,空置的使用
eg:select * from tb_contact where groupName is null;
19,内/左/右连接
eg:select * from tb_contact as a inner join tb_user as b on a.id = b.id;/*左右表互相匹配,两者同时存在的才显示,这与普通的where一样*/
select * from tb_contact as a left join tb_user as b on a.id = b.id;/*左表全显,右表匹配左表,有则显示,无则显空*/
select * from tb_contact as a right join tb_user as b on a.id = b.id;/*右表全显,左表匹配右表,有则显示,无则显空*/
20,数据分组
eg:select count(*) as rept from tb_contact group by groupName having rept>1;
eg:select * from tb_contact into outfile '/tmp/contact.txt' fields terminated by ',' optionally enclosed by '"' lines terminated by '\n';
2,将文件数据导入到数据库表中(local关键字指定文件路径为客户机,默认为服务器路径)
eg:load data local infile '/tmp/contact.txt' into table tb_contact /*(contactId,id,groupName)*/ fields terminated by ',' lines terminated '\r\n';
3,数据库脚本整体备份
eg:mysqldump -uroot -p123 tb_contact > contact.dump;
4,mysqlimport导入数据
eg:mysqlimport -uroot -123 --local --columns=a,b,c db_test F:/contact.txt
5,防止SQL注入
a,表单的双向验证
b,对SQL错误进行自定义处理
c,对输入数据进行加密处理
d,使用存储过程加上权限控制来进行所有的查询
e,通过专业的漏洞检查工具来查找漏洞
6,对重复数据的忽略插入
eg:insert ignore into tb_contact values(4,2,"好友");
7,对重复数据的覆写插入
eg:replace into tb values(4,2,"好友");
8,统计某列的重复数据
eg:select count(*) as repet from tb_contact group by groupName having repet>1;
9,过滤重复的数据
eg:select distinct groupName/*指定不重复的列*/ from tb_contact oreder by groupName;
10,删除重复数据
eg:create tablt tmp select * from tb_contact group by group groupName;
drop table tb_contact;
alter table tmp rename to tb_contact;
/*通过动态添加主键的方式消除重复的值*/
alter table tb_contact add primary key (contactId);
11,删除主键
eg:alter table tb_contact drop primary key;
12,创建序列化表
eg:create table tb_contact(
id int not null auto_increment=100,
contactId int not null default 10000,
groupName varchar(50) not null,
primary key(id)
unique (contactId)
);
13,获取元数据
eg:select version();
select user();
select database();
show status;
show varlabes;
14,查询表结构
eg:show create table tb_contact;
15,创建临时表
eg:create termpoary table tmp(
id int not null primary key,
contactId int not null
);
16,增加/删除/修改字段
eg:alter table tb_contact drop id;
alter table tb_contact add i int /*first第一列,after contactId 在某一列之后*/;
alter table tb_contact modify i varchar(10) not null default 100;
alter table tb_contact change i j varchar(10);
alter table tb_contact change i i varchar(10);
alter table tb_contact alter i set default 1000;
alter table tb_contact alter i drop default;
alter table tb_contact rename to tb_temp;
17,正则表达式的使用
eg:select * from tb_contact where groupName regexp 'ok$';
18,空置的使用
eg:select * from tb_contact where groupName is null;
19,内/左/右连接
eg:select * from tb_contact as a inner join tb_user as b on a.id = b.id;/*左右表互相匹配,两者同时存在的才显示,这与普通的where一样*/
select * from tb_contact as a left join tb_user as b on a.id = b.id;/*左表全显,右表匹配左表,有则显示,无则显空*/
select * from tb_contact as a right join tb_user as b on a.id = b.id;/*右表全显,左表匹配右表,有则显示,无则显空*/
20,数据分组
eg:select count(*) as rept from tb_contact group by groupName having rept>1;
相关文章推荐
- 数据库基本操作小摘(代码由mysql-essential-5.1.50-win32测试通过)
- MySQL学习笔记2:数据库的基本操作(创建删除查看)
- Mysql 数据库基本操作总结
- 数据库基本操作Mysql与Oracle
- Linux 下的MySQL安装 以及数据库的基本操作
- mysql 数据库基本操作
- 数据库MySQL的基本操作命令
- mysql 基本操作----数据库的操作
- MySQL 5.6学习笔记(数据库基本操作,查看和修改表的存储引擎)
- MySQL基础-数据库基本操作
- oracle mysql sqlserver 查看当前所有数据库及数据库基本操作命令
- MySql的基本用法(2)_常见的数据库操作
- 数据库基本操作与mysql错误号
- oracle mysql sqlserver 查看当前所有数据库及数据库基本操作命令
- 数据库基本操作小摘(代码由mysql-essential-5.1.50-win32测试通过)
- MySQL学习笔记2:数据库的基本操作
- mysql笔记二:基本数据库、表查询操作
- mysql笔记三:基本数据库、表创建更新操作
- DF学Mysql(一)——数据库基本操作
- MYSQL入门学习之八:数据库及表的基本操作