程序猿常用sql语句
2015-04-21 21:21
106 查看
1. 添加一个用户build,并赋予所有权限的命令
[html] view
plaincopyprint?
grant all privileges on *.* to build@'%' identified by 'build' ;
或者
[html] view
plaincopyprint?
grant all privileges on *.* to build@localhost identified by 'build' ;
(只授予本地localhost 的所有权限)
2. 命令行窗口登录的命令
[html] view
plaincopyprint?
mysql -uusername -ppassword [db_name]
[db_name] 如果指定,则进入具体的数据库, 示例:
[html] view
plaincopyprint?
mysql -ubuild -pbuild mysql
3.用gbk字符编码在命令行显示中文
[html] view
plaincopyprint?
set names gbk;
同理,我设置其他编码,如: set names utf8
4. 切换数据库
[html] view
plaincopyprint?
use db_name;
5.运行脚本
[html] view
plaincopyprint?
source sql_file
source命令的注意点:
1). 在windows中文件路径 要用 / 替换 默认的路径符 \ , 如: source F:/project/sql/init.ddl
2). 如果sql_file中有中文内容,则需要保证sql_file的字符编码与数据库的编码一致,并在运行source 命令之前运行 set names 命令.
如数据库编码为utf8, 1), 确保脚本文件(.sql, .ddl)的字符编码是utf8 ; 在运行 source 命令前先执行命令: set names utf8 (也可将此命令放入sql_file中)
6. dump 数据库
在命令行窗口运行命令,如下:
[html] view
plaincopyprint?
mysqldump -u<username> -p<password> db_name > outfile_path
一个例子:
mysqldump -ubuild -pbuild mysql > e:/mysql.sql
默认是dump表结构与数据, 如果只dump表结构,不需要数据, 则命令如下:
[html] view
plaincopyprint?
mysqldump --opt -d <db_name> -u<username> -p<password> > outfile_path
一个例子:
mysqldump --opt -d mysql -ubuild -pbuild > e:/mysql.sql
7.查询限制返回结果集(可实现分页)
使用 limit关键字,举例.
[html] view
plaincopyprint?
select * from user_ order by user_name limit 10,10
[html] view
plaincopyprint?
select * from user_ order by user_name limit 10
limit后面可带两个参数或一个参数,
两个参数: 第一个参数指定开始的位置, 第二个参数指定抓取的条数
一个参数: 从第一条开始, 抓取指定的条数
8. 查看建表的SQL语句
[html] view
plaincopyprint?
show create table table_name
9.创建数据库(若不存在才创建,并指定数据库字符编码为utf8)
[html] view
plaincopyprint?
create database if not exists db_name default character set utf8;
10.删除表数据(保留表结构)
[html] view
plaincopyprint?
truncate table_name
11. 创建表(在创建之前先判断该表是否已经存在,若存在则删除)
[html] view
plaincopyprint?
Drop table if exists cooking_user_group;
CREATE TABLE `cooking_user_group` (
`id` int(11) NOT NULL auto_increment,
`guid` varchar(255) not null unique,
`create_time` datetime ,
`archived` tinyint(1) default '0',
`name_` varchar(255),
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=20 DEFAULT CHARSET=utf8;
cooking_user_group 为表名
id为主键并自增长,并指定从20开始(20之前的为保留id).
engine为InnonDB,支持事务
表的默认字符编码为utf8
[html] view
plaincopyprint?
grant all privileges on *.* to build@'%' identified by 'build' ;
或者
[html] view
plaincopyprint?
grant all privileges on *.* to build@localhost identified by 'build' ;
(只授予本地localhost 的所有权限)
2. 命令行窗口登录的命令
[html] view
plaincopyprint?
mysql -uusername -ppassword [db_name]
[db_name] 如果指定,则进入具体的数据库, 示例:
[html] view
plaincopyprint?
mysql -ubuild -pbuild mysql
3.用gbk字符编码在命令行显示中文
[html] view
plaincopyprint?
set names gbk;
同理,我设置其他编码,如: set names utf8
4. 切换数据库
[html] view
plaincopyprint?
use db_name;
5.运行脚本
[html] view
plaincopyprint?
source sql_file
source命令的注意点:
1). 在windows中文件路径 要用 / 替换 默认的路径符 \ , 如: source F:/project/sql/init.ddl
2). 如果sql_file中有中文内容,则需要保证sql_file的字符编码与数据库的编码一致,并在运行source 命令之前运行 set names 命令.
如数据库编码为utf8, 1), 确保脚本文件(.sql, .ddl)的字符编码是utf8 ; 在运行 source 命令前先执行命令: set names utf8 (也可将此命令放入sql_file中)
6. dump 数据库
在命令行窗口运行命令,如下:
[html] view
plaincopyprint?
mysqldump -u<username> -p<password> db_name > outfile_path
一个例子:
mysqldump -ubuild -pbuild mysql > e:/mysql.sql
默认是dump表结构与数据, 如果只dump表结构,不需要数据, 则命令如下:
[html] view
plaincopyprint?
mysqldump --opt -d <db_name> -u<username> -p<password> > outfile_path
一个例子:
mysqldump --opt -d mysql -ubuild -pbuild > e:/mysql.sql
7.查询限制返回结果集(可实现分页)
使用 limit关键字,举例.
[html] view
plaincopyprint?
select * from user_ order by user_name limit 10,10
[html] view
plaincopyprint?
select * from user_ order by user_name limit 10
limit后面可带两个参数或一个参数,
两个参数: 第一个参数指定开始的位置, 第二个参数指定抓取的条数
一个参数: 从第一条开始, 抓取指定的条数
8. 查看建表的SQL语句
[html] view
plaincopyprint?
show create table table_name
9.创建数据库(若不存在才创建,并指定数据库字符编码为utf8)
[html] view
plaincopyprint?
create database if not exists db_name default character set utf8;
10.删除表数据(保留表结构)
[html] view
plaincopyprint?
truncate table_name
11. 创建表(在创建之前先判断该表是否已经存在,若存在则删除)
[html] view
plaincopyprint?
Drop table if exists cooking_user_group;
CREATE TABLE `cooking_user_group` (
`id` int(11) NOT NULL auto_increment,
`guid` varchar(255) not null unique,
`create_time` datetime ,
`archived` tinyint(1) default '0',
`name_` varchar(255),
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=20 DEFAULT CHARSET=utf8;
cooking_user_group 为表名
id为主键并自增长,并指定从20开始(20之前的为保留id).
engine为InnonDB,支持事务
表的默认字符编码为utf8
相关文章推荐
- 浅谈MySQL中优化sql语句查询常用的30种方法
- SQL 常用的获取时间差的sql语句
- SQL配置文件中常用的几种SQL语句
- oracle常用SQL语句
- SQLServer管理常用SQL语句
- 50个常用sql语句
- SQL一些常用的查询语句
- 常用的sql语句之行列转换
- SqlServer和Oracle中一些常用的sql语句4 局部/全局变量
- 经验分享交流:常用SQL语句技法
- oracle常用sql语句集合
- Oracle中常用SQL语句及函数收集
- 常用经典SQL语句大全
- SqlServer和Oracle中一些常用的sql语句7 游标
- 【63】关系数据库常用的sql语句总结
- 常用SQL语句
- 常用SQL语句(Oracle)(转)
- 常用SQL语句(一)
- SQL Server 创建表 添加主键 添加列常用SQL语句
- ibatis常用16条SQL语句