Heim >Datenbank >MySQL-Tutorial >程序员实用的MySQL sql语句_MySQL

程序员实用的MySQL sql语句_MySQL

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOriginal
2016-06-01 13:30:15967Durchsuche

bitsCN.com

程序员实用的MySQL sql语句

 

这儿只讲究实用,  程序员编程时常用到的 MySQL的 sql语句(不包括基本的 select, update, delete 等语句).

 

1. 添加一个用户build,并赋予所有权限的命令

[html] grant all privileges on *.* to 'build'@'%' identified by 'build' ;  

 

 

2. 命令行窗口登录的命令

[html] mysql -uusername -ppassword [db_name]  [db_name] 如果指定,则进入具体的数据库, 示例:[html] mysql -ubuild -pbuild mysql  

 

 

3.用gbk字符编码在命令行显示中文

[html] set names gbk;  

 

同理,我设置其他编码,如: set names utf8

 

4. 切换数据库

 

[html] use db_name;  

 

 

5.运行脚本

 

[html] 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] mysqldump -u<username> -p<password> db_name > outfile_path  一个例子:mysqldump -ubuild -pbuild mysql > e:/mysql.sql默认是dump表结构与数据, 如果只dump表结构,不需要数据, 则命令如下:[html] mysqldump --opt -d <db_name> -u<username> -p<password> > outfile_path  一个例子:mysqldump --opt -d mysql -ubuild -pbuild > e:/mysql.sql

 

 

7.查询限制返回结果集(可实现分页)

 

使用 limit关键字,举例.

 

[html] select * from user_ order by user_name limit 10,10  [html] select * from user_ order by user_name limit 10  

 

 

limit后面可带两个参数或一个参数, 

两个参数:  第一个参数指定开始的位置, 第二个参数指定抓取的条数

 

一个参数: 从第一条开始, 抓取指定的条数

 

8. 查看建表的SQL语句

 

[html] show create table table_name  

 

 

9.创建数据库(若不存在才创建,并指定数据库字符编码为utf8)

 

[html] create database if not exists db_name default character set utf8;  

 

 

10.删除表数据(保留表结构)

 

[html] truncate table_name  

 

 

11. 创建表(在创建之前先判断该表是否已经存在,若存在则删除)

 

[html] 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 &#39;0&#39;,    `name_` varchar(255),    PRIMARY KEY  (`id`)  ) ENGINE=InnoDB AUTO_INCREMENT=20 DEFAULT CHARSET=utf8;  cooking_user_group 为表名

 

id为主键并自增长,并指定从20开始(20之前的为保留id).

 

engine为InnonDB,支持事务

 

表的默认字符编码为utf8

 

bitsCN.com
Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn