{"id":27,"date":"2012-05-24T22:11:24","date_gmt":"2012-05-24T22:11:24","guid":{"rendered":"http:\/\/eltonoverip.com\/blog\/?p=27"},"modified":"2015-02-16T18:26:03","modified_gmt":"2015-02-16T18:26:03","slug":"mysql-examples","status":"publish","type":"post","link":"https:\/\/eltonoverip.com\/blog\/2012\/05\/mysql-examples\/","title":{"rendered":"MySQL examples"},"content":{"rendered":"<p>I&#8217;ve always wanted to create my own cheat sheet that lists simple and common examples of MySQL commands that I collected.<\/p>\n<p><strong>Create a new database<\/strong><br \/>\n<code>create database <em>unfortunate_database<\/em>;<\/code><\/p>\n<p><strong>Delete a database<\/strong><br \/>\n<code>drop database <em>unfortunate_database<\/em>;<\/code><\/p>\n<p><strong>Create a new user with specific privileges on the database<\/strong><br \/>\n<code>grant select,update,insert,delete,create on <em>unfortunate_database<\/em>.* to '<em>jack<\/em>'@'localhost' identified by '<em>H0lyFreak1ng@!<\/em>';<\/code><\/p>\n<p>The example above allows the username jack, password H0lyFreak1ng@! access to unfortunate_database but does not have permissions to drop a table or database.  Granting specific privileges is particularly useful if you want to provide read only access.  Ideally, you would have a user with all privileges and a second user with read-only privileges (grant select on unfortunate_database.*&#8230;)<\/p>\n<p><strong>To Backup a MySQL database<\/strong><br \/>\n<code>mysqldump -u root -p<em>root_password<\/em> unfortunate_database &gt; <em>unfortunate_database.sql<\/em><\/code><br \/>\nor<br \/>\n<code>mysqldump -u root -p <em>unfortunate_database<\/em> &gt; <em>unfortunate_database.sql<\/em><\/code><\/p>\n<p>The <em>mysqldump<\/em> command is done outside of the MySQL server console. \u00a0You don&#8217;t necessarily have to be root in order to issue the command nor specify as the user <em>root<\/em> to perform a backup of the database. As long as the user has privileges in the shell and the specified MySQL user has permissions (on the database), you should be fine. Also, the new MySQL dump file will be stored in the present working directory you are in.  Which means, if you are on \/home\/jack on some Linux server, unfortunate_database.sql will in that directory. \u00a0The dump file, unfortunate_users.sql is just a text file that includes commands that will restore the data to the database. \u00a0With that, the file doesn&#8217;t really need to end in <em>.sql<\/em> extension but is considered good practice.<\/p>\n<p><strong>To create a table<\/strong><\/p>\n<pre>\r\n<code>CREATE TABLE `unfortunate_users` (\r\n`id` int(8) NOT NULL auto_increment,\r\n`username` varchar(64) NOT NULL,\r\n`password` varchar(64) NOT NULL,\r\n`active` tinyint(1) NOT NULL default '0', \r\nPRIMARY KEY (`id`));<\/code>\r\n<\/pre>\n<p><strong>To empty out data from a table without wiping out or altering the table structure<\/strong><br \/>\n<code>truncate table <em>unfortunate_users<\/em><\/code><\/p>\n<p>In case of you run into a scenario where someone sends you a CSV file and you need to load it unto an empty or truncated table<\/p>\n<pre>\r\n<code>load data local infile '<em>more_unfortunate_bastards.csv<\/em>' into table <em>unfortunate_<\/em> fields terminated by ';'\r\nenclosed by ''\r\nlines terminated by '\\n'\r\n(column1, column2, column3, column4);<\/code>\r\n<\/pre>\n<p>What good is a backup if you can&#8217;t restore it?<br \/>\n<strong>To restore a MySQL backup&#8230;<\/strong><br \/>\n<code>mysql -u root -p <em>database_name<\/em> &lt; <em>dumpfile.sql<\/em><\/code><\/p>\n<p><strong>Add a column to an existing MySQL database table<\/strong><br \/>\n<code>ALTER TABLE table_name ADD sometext tinyint(1);<\/code><\/p>\n<p>I made an error with the above example, I did not intend <em>sometext<\/em> to be a storage for a boolean character.  <\/p>\n<p><strong>To delete a MySQL database column<\/strong><br \/>\n<code>ALTER TABLE table_name DROP sometext;<\/code><\/p>\n<p>Not enough commands? \u00a0This guy has more. \u00a0<a href=\"http:\/\/www.nparikh.org\/notes\/mysql.php\">http:\/\/www.nparikh.org\/notes\/mysql.php<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;ve always wanted to create my own cheat sheet that lists simple and common examples of MySQL commands that I collected. Create a new database create database unfortunate_database; Delete a database drop database unfortunate_database; Create a new user with specific privileges on the database grant select,update,insert,delete,create on unfortunate_database.* to &#8216;jack&#8217;@&#8217;localhost&#8217; identified by &#8216;H0lyFreak1ng@!&#8217;; The example [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8],"tags":[],"class_list":["post-27","post","type-post","status-publish","format-standard","hentry","category-web-development"],"_links":{"self":[{"href":"https:\/\/eltonoverip.com\/blog\/wp-json\/wp\/v2\/posts\/27","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/eltonoverip.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/eltonoverip.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/eltonoverip.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/eltonoverip.com\/blog\/wp-json\/wp\/v2\/comments?post=27"}],"version-history":[{"count":9,"href":"https:\/\/eltonoverip.com\/blog\/wp-json\/wp\/v2\/posts\/27\/revisions"}],"predecessor-version":[{"id":584,"href":"https:\/\/eltonoverip.com\/blog\/wp-json\/wp\/v2\/posts\/27\/revisions\/584"}],"wp:attachment":[{"href":"https:\/\/eltonoverip.com\/blog\/wp-json\/wp\/v2\/media?parent=27"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/eltonoverip.com\/blog\/wp-json\/wp\/v2\/categories?post=27"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/eltonoverip.com\/blog\/wp-json\/wp\/v2\/tags?post=27"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}