diff --git a/database-commands/mysql_commands.rst b/database-commands/mysql_commands.rst index 3a4fd33..aeff2f0 100644 --- a/database-commands/mysql_commands.rst +++ b/database-commands/mysql_commands.rst @@ -68,7 +68,7 @@ Create Alias (alternate names) a column .. code-block:: sql SELECT Name AS 'country-name', Population as 'country-population' - FROM country; + FROM Country; Order the rows by Name column in ascending order @@ -76,7 +76,7 @@ Order the rows by Name column in ascending order .. code-block:: sql SELECT * - FROM country + FROM Country ORDER BY Name ASC; @@ -85,7 +85,7 @@ Order the rows by Name column in descending order .. code-block:: sql SELECT * - FROM country + FROM Country ORDER BY Name DESC; @@ -94,7 +94,7 @@ First order by Continent and then Name within each Continent .. code-block:: sql SELECT Name, Continent - FROM country + FROM Country ORDER BY Continent, Name DESC; @@ -103,7 +103,7 @@ Order by content name in descending order and then name in ascending order .. code-block:: sql SELECT Name, Continent - FROM country + FROM Country ORDER BY Continent DESC, Name; @@ -119,93 +119,101 @@ Offset .. code-block:: sql - SELECT * from Country ORDER BY Name LIMIT 5 OFFSET 5; + SELECT * + FROM Country + ORDER BY Name + LIMIT 5 OFFSET 5; Where clause .. code-block:: sql - SELECT Name,Code,population from Country WHERE population > 100000000 ORDER BY population; + SELECT Name, Code, Population + FROM Country + WHERE Population > 100000000 + ORDER BY Population; Null clause .. code-block:: sql - SELECT Name,Code,population + SELECT Name, Code, Population FROM Country - WHERE population > 100000000 OR population IS NULL - ORDER BY population; + WHERE Population > 100000000 OR Population IS NULL + ORDER BY Population; OR clause .. code-block:: sql - SELECT Name,Code,population + SELECT Name, Code, Population FROM Country - WHERE population > 100000000 OR population IS NULL - ORDER BY population; + WHERE Population > 100000000 OR Population IS NULL + ORDER BY Population; NOT Null clause .. code-block:: sql - SELECT Name,Code,population + SELECT Name, Code, Population FROM Country - WHERE population > 100000000 OR population IS NOT NULL - ORDER BY population; + WHERE Population > 100000000 OR Population IS NOT NULL + ORDER BY Population; -Null clause +AND clause .. code-block:: sql - SELECT Name,Code,population + SELECT Name, Code, Population FROM Country - WHERE population > 100000000 AND continent = 'Asia' - ORDER BY population; + WHERE Population > 100000000 AND Continent = 'Asia' + ORDER BY Population; Where clause with string match .. code-block:: sql - SELECT Name,Code from Country WHERE Code = 'USA’; + SELECT Name, Code + FROM Country + WHERE Code = 'USA’; Like clause .. code-block:: sql - SELECT Name, continent, population + SELECT Name, Continent, Population FROM Country - WHERE name like 'ind%’; + WHERE Name LIKE 'ind%’; Like clause with single character .. code-block:: sql - SELECT Name, continent, population + SELECT Name, Continent, Population FROM Country - WHERE name like ‘_a%’; + WHERE Name LIKE ‘_a%’; IN clause .. code-block:: sql - SELECT Name, continent, population + SELECT Name, Continent, Population FROM Country - WHERE continent IN ('Asia', 'Europe') - ORDER BY continent; + WHERE Continent IN ('Asia', 'Europe') + ORDER BY Continent; Regex .. code-block:: sql - SELECT Name, continent, population + SELECT Name, Continent, Population FROM Country WHERE Name REGEXP '^.[a-d].*’; @@ -221,7 +229,7 @@ Insert row .. code-block:: sql INSERT INTO test - VALUES (1, 'First Value’, ’Second Value') + VALUES (1, 'First Value’, ’Second Value'); Insert row specifying the columns @@ -244,7 +252,8 @@ Updating a row .. code-block:: sql - UPDATE test SET c = ’Something else’ + UPDATE test + SET c = ’Something else’ WHERE a = 1; @@ -254,7 +263,7 @@ Delete a row with only its first occurrance DELETE FROM test - WHERE a=1 + WHERE a = 1 LIMIT 1; SELECT * FROM test; @@ -263,7 +272,7 @@ Delete a table .. code-block:: sql - DROP TABLE test + DROP TABLE test; Describe table <— MySQL specific @@ -284,7 +293,7 @@ Index while creating table .. code-block:: sql - INDEX(a) + INDEX(a); Show index @@ -298,13 +307,15 @@ Modify the table at a later stage .. code-block:: sql - ALTER TABLE test ADD d VARCHAR(10); + ALTER TABLE test + ADD d VARCHAR(10); Remove a column .. code-block:: sql - ALTER TABLE test DROP d; + ALTER TABLE test + DROP d; Add a column with more options @@ -312,7 +323,8 @@ Add a column with more options ALTER TABLE test ADD d VARCHAR(10) - AFTER a DEFAULT ’something’; + AFTER a + DEFAULT ’something’; Timezone @@ -328,7 +340,10 @@ String functions - Length of a value <— counts the bytes .. code-block:: sql - SELECT Name, LocalName, Length(LocalName) AS len FROM Country where Continent = 'Europe' ORDER BY len; + SELECT Name, LocalName, Length(LocalName) AS len + FROM Country + WHERE Continent = 'Europe' + ORDER BY len; CHAR_LENGTH counts characters @@ -392,7 +407,10 @@ GROUP BY .. code-block:: sql - SELECT Continent, COUNT(*) as count from Country GROUP BY Continent ORDER BY count DESC; + SELECT Continent, COUNT(*) as Count + FROM Country + GROUP BY Continent + ORDER BY Count DESC; Scan the table and get the count of distinct values