<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: ishanshre</title>
    <description>The latest articles on DEV Community by ishanshre (@ishanshre).</description>
    <link>https://dev.to/ishanshre</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F912294%2F4f4242bf-3cac-488e-bf06-16fdb381ca63.png</url>
      <title>DEV Community: ishanshre</title>
      <link>https://dev.to/ishanshre</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/ishanshre"/>
    <language>en</language>
    <item>
      <title>MySql CheatSheet</title>
      <dc:creator>ishanshre</dc:creator>
      <pubDate>Sun, 05 Mar 2023 14:29:20 +0000</pubDate>
      <link>https://dev.to/ishanshre/mysql-cheatsheet-ol6</link>
      <guid>https://dev.to/ishanshre/mysql-cheatsheet-ol6</guid>
      <description>&lt;h1&gt;
  
  
  SQL Commands
&lt;/h1&gt;

&lt;h2&gt;
  
  
  Create Database
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Create new database or schema&lt;/li&gt;
&lt;li&gt;Command - &lt;code&gt;CREATE DATABASE &amp;lt;DATABASE NAME&amp;gt;;&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Eg:- &lt;code&gt;CREATE DATABASE myDB;&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Drop Database
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Delete the database&lt;/li&gt;
&lt;li&gt;Command - &lt;code&gt;DROP DATABASE &amp;lt;DATABASE NAME&amp;gt;;&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Eg:- &lt;code&gt;DROP DATABASE myDB;&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Use Database
&lt;/h2&gt;

&lt;p&gt;-Set the database default&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Command- &lt;code&gt;USE &amp;lt;DATABASE NAME&amp;gt;;&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Eg:- &lt;code&gt;USE myDB;&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Alter Database
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;We can change the database to read only and vice versa. When the database is read only we can only view its data. We cannot do any operations in database except reading and altering the read only to false or 0&lt;/li&gt;
&lt;li&gt;Command- &lt;code&gt;ALTER &amp;lt;DATABASE NAME&amp;gt; READ ONLY=1;&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Command- &lt;code&gt;ALTER &amp;lt;DATABASE NAME&amp;gt; READ ONLY=0;&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;h1&gt;
  
  
  Table
&lt;/h1&gt;

&lt;h2&gt;
  
  
  Create Table
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Use CREATE to create new table in the database&lt;/li&gt;
&lt;li&gt;Command:-
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;CREATE TABLE &amp;lt;TABLE NAME&amp;gt; (
column 1 DATATYPE,
column 2 DATATYPE,
column 3 DATATYPE,
… 
);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Eg:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;CREATE TABLE user (
    user_id INT,
    username VARCHAR(50),
    password VARCHAR(50),
    joined_date DATE,
    last_login DATE
);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;CREATE TABLE users (
    id INT PRIMARY KEY AUTO_INCREMENT,
    username VARCHAR(100),
    created DATE DEFAULT (CURRENT_DATE())
);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Rename Table
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Command:- &lt;code&gt;RENAME TABLE &amp;lt;TABLE NAME&amp;gt; TO &amp;lt;NEW TABLE NAME&amp;gt;;&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Eg:- &lt;code&gt;RENAME TABLE user TO account;&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Alter Table
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Alters the table details&lt;/li&gt;
&lt;li&gt;Commands:-&lt;/li&gt;
&lt;li&gt;Add column
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ALTER TABLE account
ADD phone_number VARCHAR(10);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Rename column
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ALTER TABLE account
RENAME phone_number to email;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Change column datatype
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ALTER TABLE account
MODIFY COLUMN email VARCHAR(255);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Change the column order
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ALTER TABLE account
MODIFY email VARCHAR(255)
AFTER password;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Drop Column
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ALTER TABLE account
DROP COLUMN email
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h1&gt;
  
  
  Insert Rows
&lt;/h1&gt;

&lt;h2&gt;
  
  
  Insert single data or single row
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;INSERT INTO account
VALUES (1,"user1","userPassword1","2021-01-23","2022-12-01");
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Insert multiple rows or multiple data
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;INSERT INTO user
VALUES (3, "admin1","admin1pass","2021-01-02","2022-01-09"),
    (3, "admin2","admin2@pass","2021-01-02","2022-01-09"),
    (4, "admin3","admin3@pass","2021-01-02","2022-01-09"),
    (5, "admin4","admin4@pass","2021-01-02","2022-01-09");
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Insert data into selected columns of the row
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;INSERT INTO user (user_id, username, password)
VALUES (6, "admin5","admin6pass");
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Update columns data with where clause
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;UPDATE accounts
SET password = "hello@123"
WHERE user_id = 2;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Delete row data(where clause is important in DELETE command other wise our whole data will be deleted
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;DELETE FROM accounts
WHERE user_id = 2;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Enable/Disable safe mode in MySql
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SET SQL_SAFE_UPDATES = 0

SET SQL_SAFE_UPDATES = 1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Disable autocommit
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SET AUTOCOMMIT = OFF;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Manually create a save point and make changes
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;COMMIT;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Roll Back Changes(only possible if commit changes
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ROLLBACK;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Add UNIQUE Constraints to column when creating a table
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;CREATE TABLE profile (
    profile_id INT,
    name VARCHAR(50) UNIQUE,
    age INT
);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Add UNIQUE to a column of a table
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ALTER TABLE profile
ADD CONSTRAINT
UNIQUE (profile_id);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Set Column NOT NULL when creating a table
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;CREATE TABLE student (
    student_id INT NOT NULL,
    class INT
);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Set column NOT NULL for existing tables
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ALTER TABLE student
MODIFY class INT NOT NULL;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Set a default value new table
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;CREATE TABLE employees (
   id INT,
   name VARCHAR(255),
   salary DECIMAL(10,2) DEFAULT 0
);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Set a default value for existing table
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ALTER TABLE employees 
ALTER salary SET DEFAULT 10;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Set Primary key for new table
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;CREATE TABLE employees (
     id INT PRIMARY KEY,
     name VARCHAR(50)
);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Set Primary key for existing table
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ALTER TABLE employees
ADD CONSTRAINT
PRIMARY KEY(id);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Set Primary key for new table with auto increment
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;CREATE TABLE employee (
    id INT PRIMARY KEY AUTO_INCREMENT,
    name VARCHAR(50)
);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Set auto_increment value starts from
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ALTER TABLE employee 
AUTO_INCREMENT = 1000;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Creating a foriegn key(one to many relationship)
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;CREATE TABLE customers (
    id INT PRIMARY KEY AUTO_INCREMENT,
    name VARCHAR(5),
);

INSERT INTO customers (name)
VALUES ("ishan"),("binod"),("anil");
SELECT * FROM customers;

CREATE TABLE orders (
    order_id INT PRIMARY KEY AUTO_INCREMENT,
    amount DECIMAL(10,2),
    customer_id INT,
    FOREIGN KEY(customer_id) REFERENCES customers(customer_id)
);
SELECT * FROM orders;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Drop the foreign key
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ALTER TABLE orders 
DROP FOREIGN KEY orders_ibfk_1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Add foriegn key to existing table with custom name
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ALTER TABLE orders 
ADD CONSTRAINT fk_customer_id
FOREIGN KEY(customer_id) REFERENCES customers(customer_id);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Inner Join
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Select records from two tables having matching values in both table
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SELECT customers.customer_id, customers.name, orders.amount
FROM customers INNER JOIN orders
ON customers.customer_id = orders.customer_id;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Left join
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Selects all records from left table &lt;/li&gt;
&lt;li&gt;Right side table records are not displayed if record does not matches from the left
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SELECT *
FROM customers LEFT JOIN orders
ON customers.customer_id = orders.customer_id;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Right join
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Selects all the records from the right table&lt;/li&gt;
&lt;li&gt;Selects all the records from the left table that matches with the right table
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SELECT *
FROM customers RIGHT JOIN orders
ON customers.customer_id = orders.customer_id;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  COUNT function
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Counts the records in that column
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SELECT COUNT(amount) AS no_of_amounts
FROM orders;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  MAX Function
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Max returns the highest order of the record in the column
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SELECT MAX(amount) as maximum_amount
FROM orders;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SELECT MAX(name) AS large_name
FROM customers;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  MIN function
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;MIN returns the lowest order of the record in the column
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SELECT MIN(amount) as minimum_amount
FROM orders;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SELECT MIN(name) AS small_name 
FROM customers;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  AVG function
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;AVG returns the average of the records in the columns
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SELECT AVG(amount) AS average_amount
FROM orders;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  SUM function
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;SUM returns the sum of the records in the columns
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SELECT SUM(amount) AS sum_of_amount
FROM orders;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  CONCAT function
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;returns a records concatinating two or more columns
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SELECT CONCAT(first_name, " ", last_name) AS "Full Name"
FROM employees;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  AND, OR, NOT, BETWEEN AND IN
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;They are logical keywords for logical operations
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SELECT *
FROM employees
WHERE hire_date &amp;lt; "2015-01-15 AND job = "Teacher";
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SELECT * 
FROM employees
WHERE job = "Manager" OR job = "Boxer";
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SELECT *
FROM employees
WHERE NOT age = 20;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SELECT * 
FROM employees
WHERE NOT age = 23 AND NOT age = 40;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SELECT *
FROM employees
WHERE hire_date BETWEEN "2022-5-20" AND "2023-1-2";
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SELECT *
FROM employees
WHERE jobs IN ("Cook","Teacher","Doctor","Manager");
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Wildcards (%, _)
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;% represents number of characters&lt;/li&gt;
&lt;li&gt;_ represents only one characters
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SELECT *
FROM employees
WHERE first_name LIKE "I%";
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SELECT *
FROM employees
WHERE last_name LIKE "%t";
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SELECT *
FROM employees
WHERE jobs LIKE "_OO_";
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SELECT *
FROM employees
WHERE jobs LIKE "_e_ach_r";
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SELECT *
FROM employees
WHERE hire_date LIKE "____-01-__";
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SELECT *
FROM employees
WHERE last_name LIKE "_a%";
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  ORDER BY
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Default order : Ascending (ASC keyword)&lt;/li&gt;
&lt;li&gt;To order in reverse, add DESC keyword
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SELECT * FROM employees
ORDER BY last_name;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SELECT * FROM employees
ORDER BY first_name DESC;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SELECT * FROM employees
ORDER BY hire_date DESC;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  LIMIT and OFFSET
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;LIMIT is used for returning limited records, especially usefull for large datasets&lt;/li&gt;
&lt;li&gt;OFFSET is used to display next records to limit. Especially used in pagination
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;-- Displays next 10 records i.e. record from 11 to 20
SELECT * FROM employees
LIMIT 10
OFFSET 1;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  UNION and UNION ALL
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Combines the result of two or more tables&lt;/li&gt;
&lt;li&gt;Must have same number of columns in the tables&lt;/li&gt;
&lt;li&gt;UNION remove the duplicates&lt;/li&gt;
&lt;li&gt;UNION ALL allows duplicates
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SELECT first_name,  last_name FROM employees
UNION
SELECT first_name, last_name FROM customers
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SELECT * FROM incomes
UNION
SELECT * FROM expences
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SELECT first_name, last_name FROM employees
UNION ALL
SELECT first_name, last_name FROM customers
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Self Join
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Join a table to itself&lt;/li&gt;
&lt;li&gt;Comparing rows with other rows in same table
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SELECT a.first_name, a.last_name, 
       CONCAT(b.first_name,b.last_name) as "Referred by"
FROM customers as a
INNER JOIN customers as b
ON a.referred_id = b.customer_id;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SELECT a.first_name, a.last_name
       CONCAT(b.first_name, b.last_name) as "Supervised by"
FROM employees as a
LEFT JOIN employees as b
ON a.supervisor_id = b.employee_id;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  VIEW
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;VIEW is a virtual table of a result of a query&lt;/li&gt;
&lt;li&gt;Can perform operations of table in view as well&lt;/li&gt;
&lt;li&gt;VIEW get updated as corresponding table updates
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;CREATE VIEW employeee_attendances AS
SELECT first_name, last_name
FROM employees;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;CREATE VIEW email_employees_list AS
SELECT email 
FROM employees;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  INDEX
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;INDEX is applied to the column&lt;/li&gt;
&lt;li&gt;INDEX is a BTree data structure&lt;/li&gt;
&lt;li&gt;It is used for increasing the speed of searching&lt;/li&gt;
&lt;li&gt;Disadvantage of Index is that it slows down the update&lt;/li&gt;
&lt;li&gt;Longer the column, longer the operations takes
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SHOW INDEX FROM customers;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;CREATE INDEX last_name_idx
ON customers(last_name);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;CREATE INDEX last_name_first_name_idx
ON customers(last_name, first_name);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ALTER TABLE customers
DROP INDEX last_name_idx;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  SUBQUERIES
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;A query within a query&lt;/li&gt;
&lt;li&gt;Syntax:- query(subquery)&lt;/li&gt;
&lt;li&gt;Result of the subquery is used in the outer query.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SELECT * FROM employees
WHERE hourly_pay &amp;gt; (SELECT AVG(hourly_pay) FROM employees);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SELECT first_name, last_name
FROM customers
WHERE customer_id IN
(SELECT DISTINCT customer_id
FROM orders
WHERE customer_id IS NOT NULL);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  GROUP BY clause
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Allows to group rows based upon one or more column&lt;/li&gt;
&lt;li&gt;Most often used with aggregate function such as max, avg, etc.&lt;/li&gt;
&lt;li&gt;Use HAVING instead of WHERE keyword when using GROUP BY&lt;/li&gt;
&lt;li&gt;Using WHERE results an error with GROUP BY
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SELECT COUNT(amount), ordered_date
FROM orders
GROUP BY ordered_date;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SELECT SUM(amount), customer_id
FROM orders
GROUP BY customer_id;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SELECT MAX(amount), customer_id,
FROM orders
GROUP BY customer_id;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SELECT COUNT(amount) AS total_orders, customer_id
FORM orders
GROUP BY customer_id
HAVING total_orders &amp;gt; 1;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  ROLLUP clause
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Extension of GROUP BY clause&lt;/li&gt;
&lt;li&gt;Super aggregate value or grand total in new row
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SELECT SUM(amount), ordered_date
FROM orders
GROUP BY ordered_date WITH ROLLUP;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SELECT COUNT(order_id), ordered_date
FROM orders
GROUP BY ordered_date WITH ROLLUP;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SELECT SUM(hourly_pay) AS "Hourly Pay", employee_id
FROM employees
GROUP_BY employee_id WITH ROLLUP;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  ON DELETE
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Important for tables with relations
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;-- ON DELETE SET NULL = SET NULL when FK is deleted
-- ON DELETE CASCADE = DELETE record when FK is deleted
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;CREATE TABLE users (
    id INT PRIMARY KEY AUTO_INCREMENT,
    username VARCHAR(100),
    created DATE DEFAULT (CURRENT_DATE())
);

CREATE TABLE posts (
    id INT PRIMARY KEY AUTO_INCREMENT,
    title VARCHAR(100),
    body VARCHAR(1000),
    user_id INT,
    FOREIGN KEY (user_id) REFERENCES users(id)
    ON DELETE CASCADE
    );

CREATE TABLE comments (
    id INT PRIMARY KEY AUTO_INCREMENT,
    body VARCHAR(100),
    post_id INT,
    user_id INT,
    FOREIGN KEY (post_id) REFERENCES posts(id) ON DELETE CASCADE,
    FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE SET NULL
);

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;-- adding FK and ON DELETE CASCADE to existing table
ALTER TABLE orders
ADD CONSTRAINT fk_customer_id
FOREIGN KEY(customer_id) REFERENCES customers(customer_id)
ON DELETE CASCADE
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;-- adding FK and ON DELETE SET NULL to exisiting table
ALTER TABLE orders
ADD CONSTRAINT fk_customer_id
FOREIGN KEY(customer_id) REFERENCES customers(customer_id)
ON DELETE SET NULL
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Stored Procedure
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;It is same as function or methods in programming.&lt;/li&gt;
&lt;li&gt;It is a prepared SQL code that can be used again and again.&lt;/li&gt;
&lt;li&gt;Some advantages are reduces network traffic, increase performance, secure and admin can grant permission to use.&lt;/li&gt;
&lt;li&gt;The only disadvantage is it increases the memory usuage of every connection.&lt;/li&gt;
&lt;li&gt;We also change the delimiter from ; to $$ for the procedure.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;DELIMITER $$
CREATE PROCEDURE get_users()
BEGIN
     SELECT * FROM users;
END $$
DELIMITER ;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;To call a procedure
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;CALL get_users();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;To drop a procedure
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;DROP PROCEDURE get_users();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Passing a argument
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;DELIMITER $$
CREATE PROCEDURE get_user_by_id(IN id INT)
BEGIN
     SELECT * FROM user
     WHERE user_id = id;
END $$
DELIMITER ;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;CALL get_user_by_id(1);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Trigger
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;A special type stored procedure that automatically runs when an event occurs.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;CREATE TRIGGER before_hourly_pay_update
BEFORE UPDATE ON employees
FOR EACH ROW
SET NEW.salary = (NEW.hourly_pay * 2080);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;CREATE TRIGGER before_hourly_pay_insert
BEFORE INSERT ON employees
FOR EACH ROW
SET NEW.salary = (NEW.hourly_pay * 2080);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
    </item>
    <item>
      <title>Dockerizing Django and MySql</title>
      <dc:creator>ishanshre</dc:creator>
      <pubDate>Wed, 21 Dec 2022 08:55:32 +0000</pubDate>
      <link>https://dev.to/ishanshre/dockerizing-django-and-mysql-9np</link>
      <guid>https://dev.to/ishanshre/dockerizing-django-and-mysql-9np</guid>
      <description>&lt;h2&gt;
  
  
  Dockerizing Django and MySql(Linux)
&lt;/h2&gt;

&lt;p&gt;In this guide, I will show you how to dockerize your django project in one container and MySql Server in another container in Linux Operating System. We will create, manage and run these container using docker-compose. For this I assume you have installed python, docker and docker-compose in your machine. If not, refer to &lt;a href="https://www.python.org/downloads/" rel="noopener noreferrer"&gt;"install python"&lt;/a&gt; and &lt;a href="https://docs.docker.com/get-docker/" rel="noopener noreferrer"&gt;"install docker"&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  First we setup our django project using terminal:
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ mkdir myProject
$ cd myProject
$ python -m venv venv
$ source venv/bin/activate
(venv) $ pip install Django==4.1.4 python-dotenv
(venv) $ django-admin startproject core .
(venv) $ pip freeze &amp;gt; requirements.txt
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Lets go line by line:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;mkdir myProject&lt;/code&gt; create a new directory in your current working directory&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;cd myProject&lt;/code&gt; change directory to myProject&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;python -m venv venv&lt;/code&gt; create a python virtual environment&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;source venv/bin/activate&lt;/code&gt; activate the virtual environment.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;pip install Django==4.1.4 python-dotenv&lt;/code&gt; install django and python-dotenv package. Python-dotenv package is used for securing our secret keys&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;django-admin startproject core .&lt;/code&gt; create django project in current directory. The dot after core represents the current directory&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;pip freeze &amp;gt; requirements.txt&lt;/code&gt; create a requirements.txt file in current directory with all the package installed in this virtual environment&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  This is my basic project directory tree:
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2a1ufrhdukz1e5rk87vy.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2a1ufrhdukz1e5rk87vy.png" alt="Basic Project Folder Tree" width="278" height="256"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Configuring Our Project:
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Create a .env file in our project directory with touch .env&lt;/li&gt;
&lt;li&gt;Setup our python-dotenv in settings.py of myProject/core/settings.py
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import os
from dotenv import load_dotenv
load_dotenv(os.path.join(BASE_DIR, ".env"))
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Frvev1zrph0uaeo18u179.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Frvev1zrph0uaeo18u179.png" alt="settings" width="644" height="237"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Create a new secret key for our project
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;(venv) $ python manage.py shell
&amp;gt;&amp;gt;&amp;gt; from django.core.management.utils import get_random_secret_key
&amp;gt;&amp;gt;&amp;gt; print(get_random_secret_key())
&amp;gt;&amp;gt;&amp;gt; exit()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Copy the secret key and paste it to .env file&lt;/li&gt;
&lt;li&gt;Replace the secret key in settings.pyby pointing it to the .env file
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SECRET_KEY = str(os.getenv("DJANGO_SECRET_KEY"))
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Configure Django Database in settings.py
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': str(os.getenv("DATABASE_NAME")),
        'USER': str(os.getenv("DATABASE_USER")),
        'PASSWORD': str(os.getenv("DATABASE_PASSWORD")),
        'HOST': str(os.getenv("DATABASE_HOST")),
        'PORT': 3306,
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Configure .env for database
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;DATABASE_NAME = "mysql_new_db"
DATABASE_USER = "myProject"
DATABASE_PASSWORD = "myProject"
DATABASE_ROOT_PASSWORD = "myProject"
DATABASE_HOST = "db"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Setup files for dockerizing our project and MySql:
We need to create three files to dockerizing our project and MySql. They are .dockerignore, Dockerfile and docker-compose.yml. A Dockerfile contains all the commands a user could call on the command line to assemble an image. .dockerignore is similar to .gitignore. It allows to specify the list of files and folders to ignore when building a image. And docker-compose.yml a YAML file to define the services and we can also spin everything up or tear it all down.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Setup Dockerfile
&lt;/h2&gt;

&lt;p&gt;Create a Dockerfile in project directory (venv) $ touch Dockerfile&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;FROM python:3
LABEL maintainer="ishanshrestha"
ENV PYTHONDONTWRITEBYTECODE=1
ENV PYTHONUNBUFFERED 1 
WORKDIR /app
COPY requirements.txt /app/
RUN pip install --upgrade pip
RUN pip install -r requirements.txt
COPY . /app/
EXPOSE 8000
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;FROM python:3&lt;/code&gt; is the python docker image. It will be your base image your Docker image will be built upon.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;LABEL maintainer="ishanshrestha"&lt;/code&gt; specifies the maintainer of your docker image. A maintainer can be your name or the website you own. It is a best practice to define a maintainer.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;ENV PYTHONDONTWRITEBYTECODE=1&lt;/code&gt; prevents Python from writing out files with extension .pyc .&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;ENV PYTHONUNBUFFERED&lt;/code&gt; 1 tells Python that you don't want to buffer the stdin/stdout. Prints output directly to the console, which prevents any delay of message on the screen.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;WORKDIR /app&lt;/code&gt; is a default directory in your docker image where the all docker commands runs from.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;COPY requirements.txt /app/&lt;/code&gt; copies earlier created requirements.txt to the docker image working directory.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;RUN pip3 install  -- upgrade pip&lt;/code&gt; upgrade the pip in the docker image.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;RUN pip install -r requirements.txt&lt;/code&gt; installs all the packages defined in the requirements.txt file.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;COPY . /app/&lt;/code&gt; copies our project to working directory of your docker image.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;EXPOSE 8000&lt;/code&gt; exposes the container to your local machine&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Setup docker-compose.yml
&lt;/h2&gt;

&lt;p&gt;Create a docker-compose file in project directory (venv) $ touch docker-compose.yml&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;version: "3.9"

services:
  db:
    container_name: mysql_new_db
    image: mysql:latest
    restart: always
    ports:
      - 3306:3306
    environment:
      - MYSQL_DATABASE=${DATABASE_NAME}
      - MYSQL_USER=${DATABASE_USER}
      - MYSQL_PASSWORD=${DATABASE_PASSWORD}
      - MYSQL_ROOT_PASSWORD=${DATABASE_ROOT_PASSWORD}
      - MYSQL_HOST=${DATABASE_HOST}
    volumes:
      - .:/app
      - mysql_new_data:/var/lib/mysql

  app:
    container_name: app
    build: .
    command: python manage.py runserver 0.0.0.0:8000
    volumes:
      - .:/app
    ports:
      - 8000:8000
    depends_on:
      - db
    restart: always

volumes:
  mysql_new_data:
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;We define two services int the docker-compose file. These services db and app are the two containers we are going to create. "db" refers to our MySql container and "app" refers to our project container.
### Lets break our "db" section:&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;container_name: mysql_new_db&lt;/code&gt; is the name of the container&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;image: mysql:latest&lt;/code&gt; is the MySql docker image that we pull from docker hub&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;restart: always&lt;/code&gt; sets the restart policy of Docker container to&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;port: 3306:3306&lt;/code&gt; defines the port for our MySql Server&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;environment:&lt;/code&gt; defines the environment variables for MySql Server&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;volumes:&lt;/code&gt; defines volume mapping of database storage&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Now, lets break our "app" section:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;build: .&lt;/code&gt; points to project directory when building the image&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;command:&lt;/code&gt; runs our django projects&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;ports:&lt;/code&gt; exposes our project image to the local machine&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;depends_on:&lt;/code&gt; sets the order of image for start/stop. In our case, MySql container/image must start before our app image/container.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Setup dokcerignore
&lt;/h2&gt;

&lt;p&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;git
.gitignore

.docker
code/__pycache__/
code/*/__pycache__/
code/*/*/__pycache__/
code/*/*/*/__pycache__/
.env/
.venv/
venv
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Final myProject directory tree:
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjvreop9uo0d1z34gji8c.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjvreop9uo0d1z34gji8c.png" alt="Final Project Tree" width="282" height="383"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Final project directory treeAppend mysqlclient==2.1.1 to requirements.txt
&lt;/h2&gt;

&lt;h2&gt;
  
  
  Build and run the docker container
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;To build and run the docker continer:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;(venv) $ docker-compose up -d --build
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;It will take some time to build the image for first time. It uses caching system to speed up the image building for second time.&lt;/li&gt;
&lt;li&gt;In command -d tag tells docker to run in the background. To stop the conatiner, run the command
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;docker-compose down
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;View the container running
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ docker ps
CONTAINER ID   IMAGE           COMMAND                  CREATED         STATUS         PORTS                                                  NAMES
2303bef6e116   myproject-app   "python manage.py ru…"   5 minutes ago   Up 5 minutes   0.0.0.0:8000-&amp;gt;8000/tcp, :::8000-&amp;gt;8000/tcp              app
b2ea4a7ba174   mysql:latest    "docker-entrypoint.s…"   5 minutes ago   Up 5 minutes   0.0.0.0:3306-&amp;gt;3306/tcp, :::3306-&amp;gt;3306/tcp, 33060/tcp   mysql_new_db
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Running manage.py commands
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ docker ps
(venv) $ docker-compose run exec app manage.py makemigrations
(venv) $ docker-compose run exec app manage.py migrate
(venv) $ docker-compose run exec app manage.py startapp
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://github.com/ishanshre/Dockrizing-Django-and-MySql-Database" rel="noopener noreferrer"&gt;Project Link&lt;/a&gt;&lt;/p&gt;

</description>
      <category>gratitude</category>
    </item>
  </channel>
</rss>
