{"id":102,"date":"2024-08-18T15:57:11","date_gmt":"2024-08-18T15:57:11","guid":{"rendered":"https:\/\/www.yehger.com\/?p=102"},"modified":"2024-09-25T06:28:47","modified_gmt":"2024-09-25T06:28:47","slug":"types-of-sql-commands","status":"publish","type":"post","link":"https:\/\/www.yehger.com\/index.php\/2024\/08\/18\/types-of-sql-commands\/","title":{"rendered":"2. DDL \u2013 Data Definition Language"},"content":{"rendered":"<h2>1. Database operation<\/h2>\n<pre><code class=\"language-sql\">-- The &quot;[xxx]&quot; are optional.\n\n-- Show all databases\nSHOW DATABASES;\n-- show current database you used.\nSELECT DATABASE();\n-- create a new database\uff08utf8mb4 utf8mb4_general_ci\uff09\nCREATE DATABASE [IF NOT EXISTS] databasename [DEFAULT CHARSET charset] [COLLATE collate];\n-- delete database\nDROP DATABASE [IF EXISTS] databasename;\n-- select a database for operation\nUSE databasename;<\/code><\/pre>\n<h2>2. Table Operation - Create &amp; Query<\/h2>\n<pre><code class=\"language-sql\">-- Create table\nCREATE TABLE user(\n    id int,\n    uname varchar(20) comment &#039;username&#039;,\n    pwd varchar(24) comment &#039;password&#039;\n) comment &#039;user table&#039;;\n\n-- show all tables\nshow tables;\n\n-- show description of table\ndesc user;\n\n-- show the creation SQL of table\nshow create table user;<\/code><\/pre>\n<h2>3. Table Operation - Alter<\/h2>\n<pre><code class=\"language-sql\">\n-- Add a new column\nAlter TABLE tableName ADD columnName datatype(size) [comment &quot;remark&quot;];\n\n-- Alter column name\nAlter TABLE tableName RENAME TO newTableName\n-- Alter datatype\nAlter TABLE tableName MODIFY columnName datatype(size);\n\n-- Alter the column name and datatype\nAlter TABLE tableName CHANGE oldColumnName newColumnName datatype(size) [comment &quot;remark&quot;];\n\n-- Delete column\nAlter Table tableName DROP columnName\n\n-- Delete table\nDROP TABLE [IF EXISTS] tableName\n\n-- Rebuild table\nTRUNCATE TABLE tableName\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>1. Database operation &#8212; The &quot;[xxx]&quot; are optional. &#8212; Show all databases SHOW DATABASES; &#8212; show current database you used&#8230;. <a class=\"read-more\" href=\"https:\/\/www.yehger.com\/index.php\/2024\/08\/18\/types-of-sql-commands\/\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9],"tags":[],"class_list":["post-102","post","type-post","status-publish","format-standard","hentry","category-mysql"],"_links":{"self":[{"href":"https:\/\/www.yehger.com\/index.php\/wp-json\/wp\/v2\/posts\/102","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.yehger.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.yehger.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.yehger.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.yehger.com\/index.php\/wp-json\/wp\/v2\/comments?post=102"}],"version-history":[{"count":15,"href":"https:\/\/www.yehger.com\/index.php\/wp-json\/wp\/v2\/posts\/102\/revisions"}],"predecessor-version":[{"id":140,"href":"https:\/\/www.yehger.com\/index.php\/wp-json\/wp\/v2\/posts\/102\/revisions\/140"}],"wp:attachment":[{"href":"https:\/\/www.yehger.com\/index.php\/wp-json\/wp\/v2\/media?parent=102"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.yehger.com\/index.php\/wp-json\/wp\/v2\/categories?post=102"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.yehger.com\/index.php\/wp-json\/wp\/v2\/tags?post=102"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}