我总结的常用SQL语句

发表于:2017-5-24 09:53

字体: | 上一篇 | 下一篇 | 我要投稿

 作者:妞妞女神小吃货    来源:博客园精华区

分享:
  索引:
  主键索引:create table ‘表名’(‘gradeid’ int(11) auto_increment primary key,或primary key(‘gradeid’))
  唯一索引:create table ‘grade’(‘gradeid’ int(11) auto_increment primary key,’gradename’ varchar(32) not null unique 或unique key ‘gradeid’(‘gradeid’))
  注:Unique key 后面的为给索引取的名字,括号里的是列名
  常规索引(index):create table ‘result’(…index/key ‘ind’(‘studentNo’,’subjectNo’))   创建时添加
  Alter table ‘result’ add index ‘ind’ (‘studentNo’,’subjectNo’);     创建后追加
  例:create table test(
  A int primary key,B char(5) unique,C int,D int,
  Key ‘index_cd’ (‘c’,’d’),
  或  Index ‘index_c’(‘c’)
  #primary key(‘a’)   #unique key ‘index_b’ (‘b’)
  )注:联合索引前后要求:哪个条件筛选后留下的数据量最少哪个在前
  全文索引:create table ‘student’(…fulltext(‘studentName’)engine=myisam);
  Alter table employee add fulltext(‘first_name’)
  注:用于快速定位特定数据,还能用于myisam类型的数据表、只能用于char、varchar、text数据列类型、适合大型数据集
  Select * from test where match(‘studentNmae’) against(‘key’);   查询test表,使用全文索引对studenname进行索引,针对key这个关键字检索
  删除索引:
  Drop index 索引名 on 表名
  Alter table 表名 drop index 索引名
  Alter table 表名 drop primary key
  查看索引:
  Show index(或keys) from 表名
  备份:
  Msqldump客户端:在bin目录下找,进入后输入:
  Mysqldump -h 主机名 -u 用户名 -p [options] 数据库名 [table 1 table2 table3] >path/filename.sql
  例如:备份bdqn数据库:cmd进入dos窗口,输入:
  mysqldump -u root -p bdqn bdqn category users > d:/sqlbk.sql
  Mysqldump常用选项:
  Mysqldump -help    查看基本参数
  --add-drop-table   导出sql脚本会加上drop table if exists语句,默认是打开的,可以用
  --skip-add-drop-table取消
  例:Mysqldump -uroot -pbdqn –-skip-add-drop-table bdqn category users >d:sqlbk.sql
  --add-locks   会在insert语句中捆绑一个lock table和unlock table语句
  好处:防止记录被再次导入时,其他用户对表进行的操作,默认是打开的
  -t或--no-create-info  纯数据备份,没有创建表格语句
  例:Mysqldump -uroot -pbdqn –-no-create-info bdqn category users >d:sqlbk.sql
  -c或--complete-insert  在每个inseert语句的列上加上字段名,在数据库导入另一个数据库时有用
  例:Mysqldump -uroot -pbdqn –c bdqn category users >d:sqlbk.sql
  -d或—no-data  只转存表结构,不存数据
  --where ’where-condition’或-w’ where-condition’   只转储给定的where条件选择的记录
  例:Mysqldump -uroot -pbdqn –-where ‘studentNo=1’ bdqn student>d:sqlbk.sql
  恢复数据库:
  方法一:用source方法 source /path/db_name.sql
  注:/path/是一个绝对路径,并且必须是mysql运行用户有权限读取的文件,source在mysql命令行里执行
  例:mysql -uroot -pbdqn;
  Use test;
  source d:sqlbk.sql
  方法二:用mysql客户端  mysql-u root -p dbname< /path/db_name.sql;
  例:mysql -uroot -pbdqn test <d:sqlbk.sql
22/2<12
精选软件测试好文,快来阅读吧~

关注51Testing

联系我们

快捷面板 站点地图 联系我们 广告服务 关于我们 站长统计 发展历程

法律顾问:上海兰迪律师事务所 项棋律师
版权所有 上海博为峰软件技术股份有限公司 Copyright©51testing.com 2003-2024
投诉及意见反馈:webmaster@51testing.com; 业务联系:service@51testing.com 021-64471599-8017

沪ICP备05003035号

沪公网安备 31010102002173号