{"id":4503,"date":"2015-03-27T11:21:59","date_gmt":"2015-03-27T03:21:59","guid":{"rendered":"http:\/\/rmohan.com\/?p=4503"},"modified":"2015-03-27T12:16:14","modified_gmt":"2015-03-27T04:16:14","slug":"mysql-terminal-commands","status":"publish","type":"post","link":"https:\/\/mohan.sg\/?p=4503","title":{"rendered":"MySQL Terminal Commands"},"content":{"rendered":"<p>MySQL has a lot of visual management tools, such as &#8220;mysql-workbench&#8221; and &#8220;sequel-pro-&#8220;. Now I&#8217;m writing MySQL terminal command operation is to strengthen the understanding of yourself for MySQL, always be better than using a graphical thorough understanding, because I already prefer to write code. Meanwhile write these articles, you want to be a reference, and hopefully be helpful to everyone, has improved, which is why I write articles of MySQL terminal operation.<\/p>\n<p>Note: MySQL database commands are not case sensitive. But in the MAC of the terminal, if you want to use the tab to automatically complete the command, then you must use uppercase, so MAC terminal will help you complete the command, otherwise you will not be according to N times the response tab.<\/p>\n<p>1, the database (database) management<\/p>\n<p>1.1 create the database is created<\/p>\n<p>1.2 show View all databases<\/p>\n<p>1.3 alter modify the database<\/p>\n<p>1.4 use to use the database<\/p>\n<p>1.5 View database currently in use<\/p>\n<p>1.6 drop delete database<\/p>\n<p>2, the data table (table) management<\/p>\n<p>2.1 create create table<\/p>\n<p>2.2 show display table<\/p>\n<p>2.3 desc view the table structure<\/p>\n<p>2.4 alter modify table structure (add, delete, change)<\/p>\n<p>2.4.1 insert in the table to add columns (fields)<\/p>\n<p>2.4.2 alter modify tables (column) field<\/p>\n<p>2.4.3 delete delete a table (column) field<\/p>\n<p>2.4.4 rename Rename table<\/p>\n<p>2.5 create use of existing data to create a new table<\/p>\n<p>3, operation and management data<\/p>\n<p>3.1 increases the data (by)<\/p>\n<p>3.2 Delete Data (deleted)<\/p>\n<p>3.3 modify the data (change)<\/p>\n<p>3.4 query data (check)<\/p>\n<p>1, the database (database) management<\/p>\n<p>1.1 create the database is created<br \/>\ncreate the database is created<\/p>\n<p>create database mohandb;<br \/>\n[root@cluster1 ~]# mysql -u root -p<br \/>\nEnter password:<br \/>\nWelcome to the MySQL monitor. Commands end with ; or \\g.<br \/>\nYour MySQL connection id is 2<br \/>\nServer version: 5.1.73 Source distribution<\/p>\n<p>Copyright (c) 2000, 2013, Oracle and\/or its affiliates. All rights reserved.<\/p>\n<p>Oracle is a registered trademark of Oracle Corporation and\/or its<br \/>\naffiliates. Other names may be trademarks of their respective<br \/>\nowners.<\/p>\n<p>Type &#8216;help;&#8217; or &#8216;\\h&#8217; for help. Type &#8216;\\c&#8217; to clear the current input statement.<\/p>\n<p>mysql&gt; create database mohandb;<br \/>\nQuery OK, 1 row affected (0.00 sec)<br \/>\n1.2 show View all databases<\/p>\n<p>mysql &gt; show databases;<\/p>\n<p>mysql&gt; show databases;<br \/>\n+&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;+<br \/>\n| Database |<br \/>\n+&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;+<br \/>\n| information_schema |<br \/>\n| mohandb |<br \/>\n| mysql |<br \/>\n| test |<br \/>\n+&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;+<br \/>\n4 rows in set (0.00 sec)<\/p>\n<p>mysql&gt;<br \/>\n1.3 alter modify the database<\/p>\n<p>alter database command to change the code:<br \/>\nHow to convert an entire MySQL database characterset and collation to UTF-8<\/p>\n<p>se the ALTER DATABASE and ALTER TABLE commands.<\/p>\n<p>ALTER DATABASE mohandb CHARACTER SET utf8 COLLATE utf8_unicode_ci;<br \/>\nALTER TABLE tablename CONVERT TO CHARACTER SET utf8 COLLATE utf8_unicode_ci;<\/p>\n<p>mysql&gt; ALTER DATABASE mohandb CHARACTER SET utf8 COLLATE utf8_unicode_ci;<br \/>\nQuery OK, 1 row affected (0.00 sec)<\/p>\n<p>mysql&gt;<\/p>\n<p>1.4 use to use the database<br \/>\nuse mohandb;<br \/>\nmysql&gt; use mohandb;<br \/>\nDatabase changed<br \/>\nmysql&gt;<br \/>\n1.5 View database currently in use<br \/>\nmysql&gt; select database ();<br \/>\n+&#8212;&#8212;&#8212;&#8212;-+<br \/>\n| database () |<br \/>\n+&#8212;&#8212;&#8212;&#8212;-+<br \/>\n| mohandb |<br \/>\n+&#8212;&#8212;&#8212;&#8212;-+<br \/>\n1 row in set (0.00 sec)<\/p>\n<p>mysql&gt;<br \/>\nmysql&gt;<br \/>\n1.6 drop delete database<\/p>\n<p>drop database mohandb;<\/p>\n<p>mysql&gt; drop database mohandb;<br \/>\nQuery OK, 0 rows affected (0.01 sec)<\/p>\n<p>The data table (table) management<\/p>\n<p>We first create a database, provided that we use in the future:<br \/>\nmysql&gt; create database testDB;<br \/>\nQuery OK, 1 row affected (0.00 sec)<\/p>\n<p>mysql&gt; use testDB;<br \/>\nDatabase changed<\/p>\n<p>create create table<\/p>\n<p>mysql&gt; create table PEOPLE (<br \/>\n-&gt; ID int AUTO_INCREMENT PRIMARY KEY,<br \/>\n-&gt; NAME varchar(20) not null,<br \/>\n-&gt; AGE int not null,<br \/>\n-&gt; BIRTHDAY datetime);<br \/>\nQuery OK, 0 rows affected (0.01 sec)<\/p>\n<p>Show Table 2.2 show<\/p>\n<p>Displays the current database of all the data tables<br \/>\nmysql&gt; show tables;<br \/>\n+&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;+<br \/>\n| Tables_in_testDB |<br \/>\n+&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;+<br \/>\n| PEOPLE |<br \/>\n+&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;+<br \/>\n1 row in set (0.00 sec)<\/p>\n<p>mysql&gt;<\/p>\n<p>desc view the table structure<\/p>\n<p>desc PEOPLE;<\/p>\n<p>mysql&gt; desc PEOPLE;<br \/>\n+&#8212;&#8212;&#8212;-+&#8212;&#8212;&#8212;&#8212;-+&#8212;&#8212;+&#8212;&#8211;+&#8212;&#8212;&#8212;+&#8212;&#8212;&#8212;&#8212;&#8212;-+<br \/>\n| Field | Type | Null | Key | Default | Extra |<br \/>\n+&#8212;&#8212;&#8212;-+&#8212;&#8212;&#8212;&#8212;-+&#8212;&#8212;+&#8212;&#8211;+&#8212;&#8212;&#8212;+&#8212;&#8212;&#8212;&#8212;&#8212;-+<br \/>\n| ID | int(11) | NO | PRI | NULL | auto_increment |<br \/>\n| NAME | varchar(20) | NO | | NULL | |<br \/>\n| AGE | int(11) | NO | | NULL | |<br \/>\n| BIRTHDAY | datetime | YES | | NULL | |<br \/>\n+&#8212;&#8212;&#8212;-+&#8212;&#8212;&#8212;&#8212;-+&#8212;&#8212;+&#8212;&#8211;+&#8212;&#8212;&#8212;+&#8212;&#8212;&#8212;&#8212;&#8212;-+<br \/>\n4 rows in set (0.00 sec)<br \/>\nalter modify table structure (add, delete, change)<br \/>\nALTER TABLE PEOPLE CONVERT TO CHARACTER SET utf8 COLLATE utf8_unicode_ci;<\/p>\n<p>mysql&gt; ALTER TABLE PEOPLE CONVERT TO CHARACTER SET utf8 COLLATE utf8_unicode_ci;<br \/>\nQuery OK, 0 rows affected (0.00 sec)<br \/>\nRecords: 0 Duplicates: 0 Warnings: 0<\/p>\n<p>insert in the table to add columns (fields)<\/p>\n<p>alter table PEOPLE add star BOOL;<\/p>\n<p>mysql&gt; alter table PEOPLE add star BOOL;<br \/>\nQuery OK, 0 rows affected (0.00 sec)<br \/>\nRecords: 0 Duplicates: 0 Warnings: 0<\/p>\n<p>mysql&gt; alter table PEOPLE add star BOOL;<br \/>\nQuery OK, 0 rows affected (0.00 sec)<br \/>\nRecords: 0 Duplicates: 0 Warnings: 0<\/p>\n<p>mysql&gt; desc PEOPLE;<br \/>\n+&#8212;&#8212;&#8212;-+&#8212;&#8212;&#8212;&#8212;-+&#8212;&#8212;+&#8212;&#8211;+&#8212;&#8212;&#8212;+&#8212;&#8212;&#8212;&#8212;&#8212;-+<br \/>\n| Field | Type | Null | Key | Default | Extra |<br \/>\n+&#8212;&#8212;&#8212;-+&#8212;&#8212;&#8212;&#8212;-+&#8212;&#8212;+&#8212;&#8211;+&#8212;&#8212;&#8212;+&#8212;&#8212;&#8212;&#8212;&#8212;-+<br \/>\n| ID | int(11) | NO | PRI | NULL | auto_increment |<br \/>\n| NAME | varchar(20) | NO | | NULL | |<br \/>\n| AGE | int(11) | NO | | NULL | |<br \/>\n| BIRTHDAY | datetime | YES | | NULL | |<br \/>\n| star | tinyint(1) | YES | | NULL | |<br \/>\n+&#8212;&#8212;&#8212;-+&#8212;&#8212;&#8212;&#8212;-+&#8212;&#8212;+&#8212;&#8211;+&#8212;&#8212;&#8212;+&#8212;&#8212;&#8212;&#8212;&#8212;-+<br \/>\n5 rows in set (0.00 sec)<\/p>\n<p>mysql&gt;<br \/>\nalter modify tables (column) field<\/p>\n<p>mysql&gt; alter table PEOPLE MODIFY star int ;<br \/>\nQuery OK, 0 rows affected (0.00 sec)<br \/>\nRecords: 0 Duplicates: 0 Warnings: 0<\/p>\n<p>mysql&gt; desc PEOPLE;<br \/>\n+&#8212;&#8212;&#8212;-+&#8212;&#8212;&#8212;&#8212;-+&#8212;&#8212;+&#8212;&#8211;+&#8212;&#8212;&#8212;+&#8212;&#8212;&#8212;&#8212;&#8212;-+<br \/>\n| Field | Type | Null | Key | Default | Extra |<br \/>\n+&#8212;&#8212;&#8212;-+&#8212;&#8212;&#8212;&#8212;-+&#8212;&#8212;+&#8212;&#8211;+&#8212;&#8212;&#8212;+&#8212;&#8212;&#8212;&#8212;&#8212;-+<br \/>\n| ID | int(11) | NO | PRI | NULL | auto_increment |<br \/>\n| NAME | varchar(20) | NO | | NULL | |<br \/>\n| AGE | int(11) | NO | | NULL | |<br \/>\n| BIRTHDAY | datetime | YES | | NULL | |<br \/>\n| star | int(11) | YES | | NULL | |<br \/>\n+&#8212;&#8212;&#8212;-+&#8212;&#8212;&#8212;&#8212;-+&#8212;&#8212;+&#8212;&#8211;+&#8212;&#8212;&#8212;+&#8212;&#8212;&#8212;&#8212;&#8212;-+<br \/>\n5 rows in set (0.00 sec)<\/p>\n<p>mysql&gt;<\/p>\n<p>delete delete a table (column) field<br \/>\nmysql&gt; alter table PEOPLE DROP column star;<br \/>\nQuery OK, 0 rows affected (0.00 sec)<br \/>\nRecords: 0 Duplicates: 0 Warnings: 0<\/p>\n<p>mysql&gt; desc PEOPLE;<br \/>\n+&#8212;&#8212;&#8212;-+&#8212;&#8212;&#8212;&#8212;-+&#8212;&#8212;+&#8212;&#8211;+&#8212;&#8212;&#8212;+&#8212;&#8212;&#8212;&#8212;&#8212;-+<br \/>\n| Field | Type | Null | Key | Default | Extra |<br \/>\n+&#8212;&#8212;&#8212;-+&#8212;&#8212;&#8212;&#8212;-+&#8212;&#8212;+&#8212;&#8211;+&#8212;&#8212;&#8212;+&#8212;&#8212;&#8212;&#8212;&#8212;-+<br \/>\n| ID | int(11) | NO | PRI | NULL | auto_increment |<br \/>\n| NAME | varchar(20) | NO | | NULL | |<br \/>\n| AGE | int(11) | NO | | NULL | |<br \/>\n| BIRTHDAY | datetime | YES | | NULL | |<br \/>\n+&#8212;&#8212;&#8212;-+&#8212;&#8212;&#8212;&#8212;-+&#8212;&#8212;+&#8212;&#8211;+&#8212;&#8212;&#8212;+&#8212;&#8212;&#8212;&#8212;&#8212;-+<br \/>\n4 rows in set (0.00 sec)<\/p>\n<p>mysql&gt;<br \/>\nrename Rename table<\/p>\n<p>mysql&gt; RENAME TABLE PEOPLE TO NEW_PEOPLE;<br \/>\nQuery OK, 0 rows affected (0.00 sec)<\/p>\n<p>2.5 create use of existing data to create a new table<br \/>\nmysql&gt; create table newTable select * from NEW_PEOPLE;<br \/>\nQuery OK, 0 rows affected (0.00 sec)<br \/>\nRecords: 0 Duplicates: 0 Warnings: 0<\/p>\n<p>2.5 create use of existing data to create a new table<\/p>\n<p>mysql&gt; create table newTable select * from NEW_PEOPLE;<br \/>\nQuery OK, 0 rows affected (0.00 sec)<br \/>\nRecords: 0 Duplicates: 0 Warnings: 0<\/p>\n<p>mysql&gt; show tables<br \/>\n-&gt; ;<br \/>\n+&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;+<br \/>\n| Tables_in_testDB |<br \/>\n+&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;+<br \/>\n| NEW_PEOPLE |<br \/>\n| newTable |<br \/>\n+&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;+<br \/>\n2 rows in set (0.00 sec)<\/p>\n<p>operation and management data<br \/>\nThe basic operation of data tables, including add, delete, change, check data.<\/p>\n<p>The following commands are operating in the PEOPLE table.<\/p>\n<p>3.1 increases the data (by)<\/p>\n<p>PEOPLE table is currently no data, it is empty of data tables, we first add some data.<\/p>\n<p>insert into command to add data:<br \/>\nmysql&gt; insert into NEW_PEOPLE VALUES (null, &#8216;mohan&#8217;, 22, &#8216;1993-03-23&#8217;);<br \/>\nQuery OK, 1 row affected (0.00 sec)<br \/>\nData show that there is one in the data.<br \/>\nWe add a few more data, such as:<br \/>\ninsert into NEW_PEOPLE VALUES (null, &#8216;mohan&#8217;, 22, &#8216;1993-03-23&#8217;);<br \/>\ninsert into NEW_PEOPLE VALUES (null, &#8216;raj&#8217;, 22, &#8216;1991-03-23&#8217;);<br \/>\ninsert into NEW_PEOPLE VALUES (null, &#8216;ravi&#8217;, 22, &#8216;1992-03-23&#8217;);<br \/>\ninsert into NEW_PEOPLE VALUES (null, &#8216;jack&#8217;, 22, &#8216;1913-03-23&#8217;);<\/p>\n<p>mysql&gt; insert into NEW_PEOPLE VALUES (null, &#8216;mohan&#8217;, 22, &#8216;1993-03-23&#8217;);<br \/>\nQuery OK, 1 row affected (0.00 sec)<\/p>\n<p>mysql&gt; insert into NEW_PEOPLE VALUES (null, &#8216;raj&#8217;, 22, &#8216;1991-03-23&#8217;);<br \/>\nQuery OK, 1 row affected (0.00 sec)<\/p>\n<p>mysql&gt; insert into NEW_PEOPLE VALUES (null, &#8216;ravi&#8217;, 22, &#8216;1992-03-23&#8217;);<br \/>\nQuery OK, 1 row affected (0.00 sec)<\/p>\n<p>mysql&gt; insert into NEW_PEOPLE VALUES (null, &#8216;jack&#8217;, 22, &#8216;1913-03-23&#8242;);<br \/>\nQuery OK, 1 row affected (0.00 sec)<\/p>\n<p>mysql &gt; select * from NEW_PEOPLE;<\/p>\n<p>mysql&gt; select * from NEW_PEOPLE;<br \/>\n+&#8212;-+&#8212;&#8212;-+&#8212;&#8211;+&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;+<br \/>\n| ID | NAME | AGE | BIRTHDAY |<br \/>\n+&#8212;-+&#8212;&#8212;-+&#8212;&#8211;+&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;+<br \/>\n| 1 | Anny | 22 | 1992-05-22 00:00:00 |<br \/>\n| 2 | mohan | 22 | 1993-03-23 00:00:00 |<br \/>\n| 3 | raj | 22 | 1991-03-23 00:00:00 |<br \/>\n| 4 | ravi | 22 | 1992-03-23 00:00:00 |<br \/>\n| 5 | jack | 22 | 1913-03-23 00:00:00 |<br \/>\n+&#8212;-+&#8212;&#8212;-+&#8212;&#8211;+&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;+<br \/>\n5 rows in set (0.00 sec)<br \/>\nmodify the data (change)<\/p>\n<p>update command to modify the data:<br \/>\nupdate NEW_PEOPLE set name=&#8217;Anny&#8217; where name = &#8216;rcs&#8217;;<br \/>\nmysql&gt; update NEW_PEOPLE set name=&#8217;Anny&#8217; where name = &#8216;rcs&#8217;;<br \/>\nQuery OK, 0 rows affected (0.00 sec)<br \/>\nRows matched: 0 Changed: 0 Warnings: 0<br \/>\nselect * from NEW_PEOPLE;<\/p>\n<p>mysql&gt; select * from NEW_PEOPLE;<br \/>\n+&#8212;-+&#8212;&#8212;-+&#8212;&#8211;+&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;+<br \/>\n| ID | NAME | AGE | BIRTHDAY |<br \/>\n+&#8212;-+&#8212;&#8212;-+&#8212;&#8211;+&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;+<br \/>\n| 1 | Anny | 22 | 1992-05-22 00:00:00 |<br \/>\n| 2 | mohan | 22 | 1993-03-23 00:00:00 |<br \/>\n| 3 | raj | 22 | 1991-03-23 00:00:00 |<br \/>\n| 4 | ravi | 22 | 1992-03-23 00:00:00 |<br \/>\n| 5 | jack | 22 | 1913-03-23 00:00:00 |<br \/>\n+&#8212;-+&#8212;&#8212;-+&#8212;&#8211;+&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;+<br \/>\n5 rows in set (0.00 sec)<br \/>\nselect NAME, AGE, BIRTHDAY from NEW_PEOPLE;<br \/>\nmysql&gt; select NAME, AGE, BIRTHDAY from NEW_PEOPLE;<br \/>\n+&#8212;&#8212;-+&#8212;&#8211;+&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;+<br \/>\n| NAME | AGE | BIRTHDAY |<br \/>\n+&#8212;&#8212;-+&#8212;&#8211;+&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;+<br \/>\n| Anny | 22 | 1992-05-22 00:00:00 |<br \/>\n| mohan | 22 | 1993-03-23 00:00:00 |<br \/>\n| raj | 22 | 1991-03-23 00:00:00 |<br \/>\n| ravi | 22 | 1992-03-23 00:00:00 |<br \/>\n| jack | 22 | 1913-03-23 00:00:00 |<br \/>\n+&#8212;&#8212;-+&#8212;&#8211;+&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;+<br \/>\n5 rows in set (0.00 sec)<\/p>\n","protected":false},"excerpt":{"rendered":"<p>MySQL has a lot of visual management tools, such as &#8220;mysql-workbench&#8221; and &#8220;sequel-pro-&#8220;. Now I&#8217;m writing MySQL terminal command operation is to strengthen the understanding of yourself for MySQL, always be better than using a graphical thorough understanding, because I already prefer to write code. Meanwhile write these articles, you want to be a reference, [&#8230;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"_links":{"self":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/4503"}],"collection":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=4503"}],"version-history":[{"count":1,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/4503\/revisions"}],"predecessor-version":[{"id":4504,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/4503\/revisions\/4504"}],"wp:attachment":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4503"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4503"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4503"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}