{"id":197,"date":"2025-05-28T17:44:11","date_gmt":"2025-05-28T17:44:11","guid":{"rendered":"https:\/\/brugbarviden.dk\/?p=197"},"modified":"2025-05-29T13:43:43","modified_gmt":"2025-05-29T13:43:43","slug":"mysql-commands","status":"publish","type":"post","link":"https:\/\/brugbarviden.dk\/?p=197","title":{"rendered":"MySQL Commands"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">MySQL Commands table <\/h2>\n\n\n\n<p>In this example we create a Database Called Movies. So every time you see the word &#8220;Movies&#8221; it&#8217;s representing a Database. <\/p>\n\n\n\n<p>The first steps are to create a database and then  select the database you created. <\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Create a Database: CREATE DATABASE MOVIES; <\/li>\n\n\n\n<li>Chose the database you want to perform operation on: USE MOVIES; <\/li>\n\n\n\n<li>Check that MOVIES is now the selected Database: SELECT DATABASE(); (it should display movies)  <\/li>\n\n\n\n<li>Now you are ready to perform operations on the Database. <\/li>\n<\/ol>\n\n\n\n<figure class=\"wp-block-table alignwide is-style-regular has-large-font-size\" style=\"font-style:normal;font-weight:500;line-height:1.9\"><table class=\"has-fixed-layout\" style=\"border-width:1px\"><thead><tr><th class=\"has-text-align-left\" data-align=\"left\">SUBJECT <\/th><th>MYSQL COMMAND + EXAMPLE <\/th><\/tr><\/thead><tbody><tr><td class=\"has-text-align-left\" data-align=\"left\"><\/td><td><\/td><\/tr><tr><td class=\"has-text-align-left\" data-align=\"left\"><\/td><td><\/td><\/tr><tr><td class=\"has-text-align-left\" data-align=\"left\">Create DB <\/td><td><code>CREATE DATABASE MOVIES;<\/code><\/td><\/tr><tr><td class=\"has-text-align-left\" data-align=\"left\">Drop (delete) DB<\/td><td>D<code>ROP DATABASE MOVIES;<\/code><\/td><\/tr><tr><td class=\"has-text-align-left\" data-align=\"left\">Show all DB<\/td><td><code>SHOW DATABASES;<\/code><\/td><\/tr><tr><td class=\"has-text-align-left\" data-align=\"left\">use specific DB <\/td><td><code>USE MOVIES <\/code><br># Selects a specific database<\/td><\/tr><tr><td class=\"has-text-align-left\" data-align=\"left\">Select  the used database<\/td><td><code>SELECT DATABASE(); <\/code><br>If you typed USE MOVIES, it will return that database. It returns the Database you decided to &#8220;USE&#8221;. When you type commands, they will apply to the database you use. <\/td><\/tr><tr><td class=\"has-text-align-left\" data-align=\"left\">Create a Table <br>&amp; insert Values into the table <\/td><td><code>CREATE TABLE MOVIES (<br>     col 1,<br>     col 2, <br>     col 3<br>     col x  <br>) <\/code><br><br><code>CREATE TABLE MOVIES (<br>\u00a0\u00a0\u00a0\u00a0movie_titles VARCHAR(60), <br>\u00a0\u00a0\u00a0\u00a0imdb_ratings INT<br>    year_release INT, <br>    genre VARCHAR(30), <br>);<br><br>INSERT INTO MOVIES  (<br> id INT PRIMARY KEY AUTO_INCREMENT, <br>movie_titles,<br>imdb_ratings,<br>year_release,<br>genre <br><br>)<br>VALUES ('WALL-E', 6,2008,'Animation'), <br>('Titanic',8, 1997, 'Historical'), <br>(\"Top Gun\". 6, 1987, \"action\")<br><\/code><br><br><br><\/td><\/tr><tr><td class=\"has-text-align-left\" data-align=\"left\">INSERT DATA INTO TABLE<\/td><td>When inserting data into a table you write 2 functions: First you specify which columns (col_name) you want to insert data into. Then below you Specify which Values you want to insert into those columns) <br><br><code>INSERT INTO MOVIES  (<br> id INT PRIMARY KEY AUTO_INCREMENT, <br>movie_titles,<br>imdb_ratings,<br>year_release,<br>genre <br><br>)<br>VALUES ('WALL-E', 6,2008,'Animation'), <br>('Titanic',8, 1997, 'Historical'), <br>(\"Top Gun\". 6, 1987, \"action\")<\/code><br><\/td><\/tr><tr><td class=\"has-text-align-left\" data-align=\"left\">Show Columns from a TABLE_NAME. <\/td><td>Show all columns from movies table<br>   <code>SHOW COLUMNS FROM MOVIES; <\/code><br><br>This does the same as above <br><code>DESC MOVIES; <br>DESCRIBE MOVIES; <\/code><\/td><\/tr><tr><td class=\"has-text-align-left\" data-align=\"left\">Select sth from a table<\/td><td><code>SELECT movie_titles FROM  movies; <br><br>SELECT * FROM movies; <br><br>SELECT movie_titles FROM movies WHERE imdb_ratings &gt; 6;<br><br>SE\u00c6ECT movie_titles, imdb_ratings, year_release FROM movies WHERE imdb_ratings &gt; 6;<\/code><\/td><\/tr><tr><td class=\"has-text-align-left\" data-align=\"left\">SELECT EVERYTHING WHERE <\/td><td><\/td><\/tr><tr><td class=\"has-text-align-left\" data-align=\"left\"><strong>ALIAS:<\/strong> select a column from table and give the column header an alias when you output, <\/td><td><code>SELECT movie_title <strong>AS movie_name<\/strong> FROM MOVIES; <\/code><br>So this selects the column called &#8220;movie_title&#8221; but in the displayed result the column header gets named &#8220;movie_name&#8221; instead of &#8220;movie_title&#8221; <br><br>EXAMPLE 2 <br><code>SELECT id AS movie_id FROM MOVIES;<\/code><br><br>EXAMPLE 3 <br><code>SELECT imdb_ratings AS movie_ratings, title AS movie_name FROM MOVIES WHERE imdb_ratings &lt;6;<\/code><\/td><\/tr><tr><td class=\"has-text-align-left\" data-align=\"left\">DROP TABLE TABLE_NAME<\/td><td>Deletes the table <br>DROP TABLE MOVIES; <\/td><\/tr><tr><td class=\"has-text-align-left\" data-align=\"left\">Update a table <\/td><td>Update the table called Movies <br><br>UPDATE MOVIES<br>SET  imdb_ratings = 8<br>WHERE movie_titles = &#8220;WALL-E&#8221;<br><br>NOTE! Make sure you didn&#8217;t leave an  empty space in the name  when you inserted it into the TABLE. If you did, you have to write &#8220;WALL-E &#8221; instead of &#8220;WALL-E&#8221; <br><br>Example: Update password for users <br>UPDATE users<br>SET password = &#8220;New_Password&#8221;<br>Where ID = 102;<br><br>(ID is better than name, because 2 people could have the same name, but  each user only has only 1 unique ID) <\/td><\/tr><tr><td class=\"has-text-align-left\" data-align=\"left\">Delete <\/td><td>DELETE FROM movies<br>WHERE id = 1<br><br>This deletes the movie whose id = 1. <br><br>DELETE * FROM movies <br>This deletes the entire table (careful!) <br><\/td><\/tr><tr><td class=\"has-text-align-left\" data-align=\"left\">CHAR(10) vs VARCHAR(10) <\/td><td>If you create a table <br>CREATE  TABLE users (<br>     name CHAR(10)<br>    username VARCHAR(10) <br>)<br>and you insert the values into <br><br>INSERT INTO users(name, username)<br>VALUES (&#8220;Thom&#8221;,&#8221;Thom123&#8243;);<br><br>VARCHAR(10) means &#8220;up to 10 characters&#8221; <br>CHAR(10) means &#8220;exactly 10 characters&#8221; so if a user types a username less that 10 characters e.g Thom123 it will be stored with 3 blank spaces &#8220;Thom123   &#8220;<br>So use VARCHAR when possible! <br><\/td><\/tr><tr><td class=\"has-text-align-left\" data-align=\"left\">TIMESTAMPS <\/td><td>If a user makes a comment or updates the comment, you need to set a timestamp for when the comment was created and when it was updated <br>CREATE TABLE user_comments (<br>\u00a0\u00a0id INT AUTO_INCREMENT PRIMARY KEY,<br>\u00a0\u00a0username VARCHAR(25),<br>\u00a0\u00a0comment_content VARCHAR(255),<br>\u00a0\u00a0created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,<br>\u00a0\u00a0updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP<br>);<br><br>So the Default value for the Timestamps is the Current time. <br><\/td><\/tr><tr><td class=\"has-text-align-left\" data-align=\"left\"><\/td><td><\/td><\/tr><tr><td class=\"has-text-align-left\" data-align=\"left\"><\/td><td><\/td><\/tr><tr><td class=\"has-text-align-left\" data-align=\"left\"><\/td><td><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>Table will be updated. <\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><td>REPLACE <\/td><td>SELECT REPLACE (&#8220;Big dog&#8221;, &#8220;Big&#8221;, &#8220;Small&#8221; <br><br>Result <br>&#8220;Small dog&#8221; <br><br>SELECT REPLACE(username, &#8220;Willie&#8221;, &#8220;James&#8221;) FROM USERS;<br><br>This replaces any username that is Willie, with James in the table USERS. <\/td><\/tr><tr><td><\/td><td><\/td><\/tr><tr><td><\/td><td><\/td><\/tr><tr><td><\/td><td><\/td><\/tr><tr><td><\/td><td><\/td><\/tr><\/tbody><\/table><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>MySQL Commands table In this example we create a Database Called Movies. So every time you see the word &#8220;Movies&#8221; it&#8217;s representing a Database. The first steps are to create a database and then select the database you created. SUBJECT MYSQL COMMAND + EXAMPLE Create DB CREATE DATABASE MOVIES; Drop (delete) DB DROP DATABASE MOVIES; [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":201,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[33,6,1],"tags":[32,31],"class_list":["post-197","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-databases","category-it","category-uncategorized","tag-database","tag-mysql"],"jetpack_featured_media_url":"https:\/\/brugbarviden.dk\/wp-content\/uploads\/2025\/05\/mysql-commands.jpg","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/brugbarviden.dk\/index.php?rest_route=\/wp\/v2\/posts\/197","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/brugbarviden.dk\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/brugbarviden.dk\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/brugbarviden.dk\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/brugbarviden.dk\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=197"}],"version-history":[{"count":13,"href":"https:\/\/brugbarviden.dk\/index.php?rest_route=\/wp\/v2\/posts\/197\/revisions"}],"predecessor-version":[{"id":212,"href":"https:\/\/brugbarviden.dk\/index.php?rest_route=\/wp\/v2\/posts\/197\/revisions\/212"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/brugbarviden.dk\/index.php?rest_route=\/wp\/v2\/media\/201"}],"wp:attachment":[{"href":"https:\/\/brugbarviden.dk\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=197"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/brugbarviden.dk\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=197"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/brugbarviden.dk\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=197"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}