设为首页 收藏本站
查看: 701|回复: 0

[经验分享] mysql常用操作语句

[复制链接]

尚未签到

发表于 2018-10-4 08:42:26 | 显示全部楼层 |阅读模式
  mysql常用操作语句 | moon's blog http://www.92csz.com/56/992.html
  1.进入数据库:

  • mysql -u root -p
  • mysql -h localhost -u root -p database_name
  2.列出数据库:

  • show databases;
  3.选择数据库:

  • use databases_name;
  4.列出数据表:

  • show tables;
  5.显示表格列的属性:

  • show columns from table_name;
  • describe table_name;
  6.导出整个数据库:

  • mysqldump -u user_name -p database_name > /tmp/file_name
例如:mysqldump -u root -p test_db > d:/test_db.sql
  7.导出一个表:

  • mysqldump -u user_name -p database_name table_name > /tmp/file_name
  例如:mysqldump -u root -p test_db table1 > d:/table1.sql
  8.导出一个数据库结构:

  • mysqldump -u user_name -p -d --add-drop-table database_name > file_name
例如:mysqldump -u root -p -d --add-drop-table test_db > test_db.sql
  9.导入数据库:

  • source file_name;

  • mysql -u user_name -p database_name < file_name
  例如:
  source /tmp/bbs.sql;
  source d:/bbs.sql;
  mysql -u root -p bbs < &quot;d:/bbs.sql&quot;
  mysql -u root -p bbs < &quot;/tmp/bbs.sql&quot;
  10.将文本文件导入数据表中(excel与之相同)

  • load data infile &quot;tables.txt&quot; into table table_name;
  例如:
  load data infile &quot;/tmp/bbs.txt&quot; into table bbs;
  load data infile &quot;/tmp/bbs.xls&quot; into table bbs;
  load data infile &quot;d:/bbs.txt&quot; into table bbs;
  load data infile &quot;d:/bbs.xls&quot; into table bbs;
  11.将数据表导出为文本文件(excel与之相同)

  • select * into outfile &quot;path_file_name&quot; from table_name;
  例如:
select * into outfile &quot;/tmp/bbs.txt&quot; from bbs;

select * into outfile &quot;/tmp/bbs.xls&quot; from bbs where>  select * into outfile &quot;d:/bbs.txt&quot; from bbs;

  select * into outfile &quot;d:/bbs.xls&quot; from bbs where>  12.创建数据库时先判断数据库是否存在:

  • create database if not exists database_name;
  例如:create database if not exists bbs
  13.创建数据库:

  • create database database_name;
  例如:create database bbs;
  14.删除数据库:

  • drop database database_name;
  例如:drop database bbs;
  15.创建数据表:

  • mysql> create table  (   ,  , ...);
  例如:create table (id int not null auto_increment primary key,name char(16) not null default &quot;jack&quot;,date_year date not null);
  16.删除数据表中数据:

  • delete from table_name;
  例如:
  delete from bbs;

  delete from bbs where>  17.删除数据库中的数据表:

  • drop table table_name;
  例如:
  drop table test_db;
rm -f database_name/table_name.* (linux下)
  例如:
  rm -rf bbs/accp.*
  18.向数据库中添加数据:

  • insert into table_name set column_name1=value1,column_name2=value2;
例如:insert into bbs set name=&quot;jack&quot;,date_year=&quot;1993-10-01&quot;;

  • insert into table_name values (column1,column2,...);
  例如:insert into bbs (&quot;2&quot;,&quot;jack&quot;,&quot;1993-10-02&quot;)

  • insert into table_name (column_name1,column_name2,...) values (value1,value2);
  例如:insert into bbs (name,data_year) values (&quot;jack&quot;,&quot;1993-10-01&quot;);
  19.查询数据表中的数据:

  • select * from table_name;

  例如:select * from bbs where>  20.修改数据表中的数据:

  • update table_name set col_name=new_value where id=1;
  例如:update bbs set name=&quot;tom&quot; where name=&quot;jack&quot;;
  21.增加一个字段:

  • alter table table_name add column field_name datatype not null default &quot;1&quot;;
  例如:alter table bbs add column tel char(16) not null;
  22.增加多个字段:(column可省略不写)

  • alter table table_name add column filed_name1 datatype,add column filed_name2 datatype;
  例如:alter table bbs add column tel char(16) not null,add column address text;
  23.删除一个字段:

  • alter table table_name drop field_name;
  例如:alter table bbs drop tel;
  24.修改字段的数据类型:

  • alter table table_name modify id int unsigned;//修改列id的类型为int unsigned
  • alter table table_name change id sid int unsigned;//修改列id的名字为sid,而且把属性修改为int unsigned
  25.修改一个字段的默认值:

  • alter table table_name modify column_name datatype not null default &quot;&quot;;
  例如:alter table test_db modify name char(16) default not null &quot;yourname&quot;;
  26.对表重新命名:

  • alter table table_name rename as new_table_name;
  例如:alter table bbs rename as bbs_table;

  • rename table old_table_name to new_table_name;
  例如:rename table test_db to accp;
  27.从已经有的表中复制表的结构:

  • create table table2 select * from table1 where 11;
  例如:create table test_db select * from accp where 11;
  28.查询时间:

  • select now();
  29.查询当前用户:

  • select user();
  30.查询数据库版本:

  • select version();
  31.创建索引:

  • alter table table1 add index ind_id(id);
  • create index ind_id on table1(id);
  • create unique index ind_id on table1(id);//建立唯一性索引
  32.删除索引:

  • drop index idx_id on table1;
  • alter table table1 drop index ind_id;
  33.联合字符或者多个列(将id与&quot;:&quot;和列name和&quot;=&quot;连接)

  • select concat(id,':',name,'=') from table;
  34.limit(选出10到20条)

  • select * from bbs order by id limit 9,10;
  (从查询结果中列出第几到几条的记录)
  35.增加一个管理员账号:

  • grant all on *.* to user@localhost identified by &quot;password&quot;;
  36.创建表是先判断表是否存在

  • create table if not exists students(……);
  37.复制表:

  • create table table2 select * from table1;
  例如:create table test_db select * from accp;
  38.授于用户远程访问mysql的权限

  • grant all privileges on *.* to &quot;root&quot;@&quot;%&quot; identified by &quot;password&quot; with grant option;
  或者是修改mysql数据库中的user表中的host字段

  • use mysql;
  • select user,host from user;
  • update user set host=&quot;%&quot; where user=&quot;user_name&quot;;
  39.查看当前状态

  • show status;
  40.查看当前连接的用户

  • show processlist;
  (如果是root用户,则查看全部的线程,得到的用户连接数同show status;里的 Threads_connected值是相同的)


运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-611673-1-1.html 上篇帖子: Mysql 备份 与 恢复 下篇帖子: mysql proxy 乱码问题
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表