您的位置:首页 > 数据库 > MySQL

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;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息