日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

MySQL的命令合集

發(fā)布時間:2023/12/9 60 豆豆
生活随笔 收集整理的這篇文章主要介紹了 MySQL的命令合集 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

2019獨角獸企業(yè)重金招聘Python工程師標準>>>

MySQL客戶端操作

  • $db$是數(shù)據(jù)庫名稱
  • $table$是數(shù)據(jù)表名稱
  • $field$是數(shù)據(jù)表里的字段名稱
  • 有些語句最后加上**/G**可以使結(jié)果更美觀
  • 數(shù)據(jù)庫相關(guān)

    創(chuàng)建數(shù)據(jù)庫,修改數(shù)據(jù)庫

    create database $db$; 使用默認設(shè)置create database $db$ default character set "utf8";指定編碼show databases; 查看所有數(shù)據(jù)庫show create database $db$; 查看數(shù)據(jù)庫信息alter database $db$ default character set $code$ collate $cc$;修改數(shù)據(jù)編碼drop database $db$;刪除數(shù)據(jù)庫create database $db$ default character set utf8 collate utf8_general_ci;$完整的建庫語句use $db$;使用數(shù)據(jù)庫數(shù)據(jù)庫權(quán)限grant $privileges$ on $db$.$table$ to $user$@'$host$' identified by "$passwd$" with grant option;1. $privileges$ 指權(quán)限; ALL PRIVILEGES是表示所有權(quán)限,可以使用select,update等權(quán)限.2. ON 用來指定權(quán)限針對哪些庫和表.3. $db$.$table$ 數(shù)據(jù)庫和表;*.*指所有數(shù)據(jù)庫的所有表4. TO 表示將權(quán)限賦予某個用戶.5. $user$@'$host$' 指定用戶和主機;@后面接限制的主機,可以是IP,IP段,域名以及%,%表示任何地方.注意:這里%有的版本不包括本地,以前碰到過給某個用戶設(shè)置了%允許任何地方登錄,但是在本地登錄不了,這個和版本有關(guān)系,遇到這個問題再加一個localhost的用戶就可以了.6. IDENTIFIED BY 指定用戶的登錄密碼.7. WITH GRANT OPTION 這個選項表示該用戶可以將自己擁有的權(quán)限授權(quán)給別人.注意:經(jīng)常有人在創(chuàng)建操作用戶的時候不指定WITH GRANT OPTION選項導致后來該用戶不能使用GRANT命令創(chuàng)建用戶或者給其它用戶授權(quán).刷新權(quán)限flush privileges;查詢權(quán)限show grants;show grants for $user$@'$host$';回收權(quán)限r(nóng)evoke delete on *.* from $user$@'$host$';修改密碼SET PASSWORD FOR $user$@'$host$' = PASSWORD('$passwd$');update user set PASSWORD = PASSWORD('$passwd$') where user = $user$;

    數(shù)據(jù)表相關(guān)

    對數(shù)據(jù)庫里面的表做相關(guān)的操作

  • 創(chuàng)建表

    create table $table$($field1$ 數(shù)據(jù)類型 [完整性約束條件],...$field*$ 數(shù)據(jù)類型,[UNIQUE|FULLTEXT|SPATIAL] INDEX|KEY)ENGINE=[MyISAM|INODB|...];
  • 查看數(shù)據(jù)表

    show tables;查看數(shù)據(jù)庫下面的所有表查看數(shù)據(jù)表信息desc $table$show create table $table$;show columns from $table$;
  • 修改數(shù)據(jù)表

    alter table $table_src$ rename $table_dest$;修改表名$table_src$為原來數(shù)據(jù)表的名稱,而$table_dest$為新的數(shù)據(jù)表名稱.alter table $table$ change $field_src$ $field_dest$;修改字段名alter table $table$ modify $field$ int(20);修改字段的數(shù)據(jù)類型alter table $table$ add $field$ int;增加字段alter table $table$ drop $field$;刪除字段drop table $table$;刪除表truncate table $table$;清空表
  • 索引相關(guān)

    創(chuàng)建索引創(chuàng)建普通索引create index $index_name$ on $table$($field$);alter table $table$ add index $index_name$($field$);創(chuàng)建唯一性索引create unique index $index_name$ on $table$($field$);alter table $table$ add unique $index_name$($field$);刪除索引alter table $table$ drop index $index_name$;drop index $index_name$ on $table$;
  • 數(shù)據(jù)表里面的數(shù)據(jù)的操作

    添加數(shù)據(jù)insert into $table$($field1$,$field2$,...) values($value1$,$value2$,...);insert into $table$ values($value1$,$value2$,...);insert into $table$ set $field1$=$value1$,$field2$=$value2$,...;insert into $table$ values($value1$,$value2$,...),($value1$,$value2$,...),...;查詢數(shù)據(jù)select * from $table$;select * from $table$ limit $count$;select * from $table$ limit $start$,$count$;select * from $table$ where $field$=$value$;select $field1$,$field2$,... from $table$ where $field$=$value$;%:代表0個、1個或者多個任意字符_:代表1個任意字符select * from $table$ where $field$ like "%|_";select * from $table$ group by $field$;分組select $field1$,$field2$,...,group_concat($field$) from $table$ group by $field$;select $field1$,group_concat($field$) as $variable1$,count(*) as $variable2$,max($field$) as $variable3$,sum($field$) as $variable4$from $table$ group by $field$ having count(*) > 2;select * from $src$ order by $field$ desc; $降序select * from $src$ order by $field$ asc; $升序更新數(shù)據(jù)update $table$ set $field$=$value$ where $field$=$value$;刪除數(shù)據(jù)delete from $table$ where $field$=$value$;
  • 備份

    備份數(shù)據(jù)庫到文件mysqldump -u$user$ -p$passwd$ $db$ > $path$/$file$.sql創(chuàng)建一個新的數(shù)據(jù)庫mysql -u$user$ -p$passwd$ -e "create database $db$"導入數(shù)據(jù)到新的數(shù)據(jù)庫mysql -u$user$ -p$passwd$ $db$ < $path$/$file$.sql查看數(shù)據(jù)庫歷史命令cat /root/.mysql_history遠程登陸數(shù)據(jù)庫并查詢mysql -u$user$ -p$passwd$ -h 127.0.0.1 -P 3306 -e "select * from $db$.$table$ limit 2;"查看數(shù)據(jù)表的詳細信息show table status like '$table$';導出數(shù)據(jù)庫的一個數(shù)據(jù)表mysqldump $db$.$table$ > $path$/$file$.sql將數(shù)據(jù)表導入數(shù)據(jù)庫mysql $db$ < $path$/$file$.sql需要注意的是,導入的數(shù)據(jù)表名稱為導出時候,數(shù)據(jù)表在源數(shù)據(jù)庫中的名稱,因此保證在源數(shù)據(jù)庫中的名稱和新數(shù)據(jù)庫中表名稱不會重復,否則會發(fā)生數(shù)據(jù)覆蓋.

    轉(zhuǎn)載于:https://my.oschina.net/jacky0525/blog/1833925

    總結(jié)

    以上是生活随笔為你收集整理的MySQL的命令合集的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。