fullstack_webdev

Code and notes from Full stack web developer path, LinkedIn Learning.

View on GitHub

3. Fundamental Concepts

03_01. Creating a Table

-- CREATE Table test with column a of int type and column b of text type
CREATE TABLE test(
  a INTEGER,
  b TEXT
);

-- INSERT values into test
INSERT INTO test VALUES(1,'a');
INSERT INTO test VALUES(2,'b');
INSERT INTO test VALUES(3,'c');
-- Display entries in test table
SELECT * FROM test;

03_02 Deleting a table

DROP TABLE IF EXISTS test;
CREATE TABLE test(
  a TEXT,
  b TEXT
);

-- INSERT values into test
INSERT INTO test VALUES('one','a');
INSERT INTO test VALUES('two','b');
INSERT INTO test VALUES('three','c');
-- Display entries in test table
SELECT * FROM test;
DROP TABLE test;

03_03 Inserting Rows

CREATE TABLE test(a INTEGER, b TEXT, c TEXT);

-- INSERT INTO without specifying column names, essentially you want to enter data into each column and supply it in comma separated form
INSERT INTO test VALUES(1,'This','Right Here!');

-- INSERT INTO but insert only in specified b and c columns specific String Literal values
INSERT INTO test (b,c) VALUES('That','Over there!');

-- To insert null, default values in table
INSERT INTO test DEFAULT VALUES;

-- Use the result of SELECT statement to add rows to a table, we use SELECT clause instead of values clause here
INSERT INTO test(a,b,c) SELECT id, name, description from ITEM;

-- Display updated table
SELECT * FROM test;

03_04 Deleting Rows

-- Audition your WHERE clause with SELECT before deletion
SELECT * FROM test WHERE a = 1; -- Returns 2 rows

-- If you want to delete both rows you could do, more on filtering techniques later on
DELETE FROM test WHERE a = 1;

-- DELETE Row based on a particular value in a given column, mostly its the value of primary key column to identify a row uniquely
DELETE FROM test WHERE a = 3;

03_05 The NULL Value

-- Select all rows where a IS NULL
SELECT * FROM test WHERE a IS NULL;

-- Select all rows where a IS NOT NULL
SELECT * FROM test WHERE a IS NOT NULL;

INSERT INTO test VALUES (0, NULL, '');
-- SELECT from b column where value is NULL
SELECT * FROM test WHERE b IS NULL;

-- Create New table with NOT NULL constraint
DROP TABLE IF EXISTS test;

-- CREATE TABLE with NOT NULL
CREATE TABLE test (
 a INTEGER NOT NULL,
 b TEXT NOT NULL,
 c TEXT
);

INSERT INTO test VALUES (1,'this','that');

-- INSERT in b and c but not in a, notice that a is NOT NULL
INSERT INTO test (b,c) VALUES ('one','two'); -- Error while executing query, NOT NULL constraint failed
SELECT * FROM test;
INSERT INTO test (a,b) VALUES (1,'two'); -- No errors as c doesn't have NOT NULL constraint

03_06 Constraining Columns

DROP TABLE IF EXISTS test;

-- Give default value to a column instead of NOT NULL
CREATE TABLE test (a TEXT, b TEXT, c TEXT DEFAULT 'panda');
INSERT INTO test (a,b) VALUES ('one','two');
SELECT * FROM test;

-- Another constraint is that keeping values unique in a column. This is done for columns that hold primary keys in general
-- To create a column with unique values and default value in another column we can do following:
CREATE TABLE test (a TEXT UNIQUE, b TEXT, c TEXT DEFAULT 'panda');

-- Depending on DB, NULL values may be exempted from the unique constraint. Below might work in some systems and not work in others.
INSERT INTO test (a,b) VALUES (NULL,'two');
INSERT INTO test (a,b) VALUES (NULL,'two');
-- The above works on SQLite, on some DBMS NULL is part of unique constraint and on others it is not, depends on DB you are using.

-- Better way is to combine constraints by using UNIQUE and NOT NULL together for primary key column
CREATE TABLE test (a TEXT UNIQUE NOT NULL, b TEXT, c TEXT DEFAULT 'panda');

03_07 Changing a schema

DROP TABLE IF EXISTS test;

CREATE TABLE test (a TEXT, b TEXT, c TEXT);
INSERT INTO test VALUES ('one','two','three');
INSERT INTO test VALUES ('two','three','four');
INSERT INTO test VALUES ('three','four','five');
SELECT * FROM test;
-- Gives us 3 rows with entries, now if we want to alter the data we use the ALTER statement as follows:
ALTER TABLE test ADD d TEXT DEFAULT 'added later';

03_08 ID Columns

DROP TABLE IF EXISTS test;

-- INTEGER PRIMARY KEY constraint is followed for id column, the system will automatically generate unique, sequential number, and will enforce unique value constraint
CREATE TABLE test (id INTEGER PRIMARY KEY, a INTEGER, b TEXT);
INSERT INTO test (a,b) VALUES (10,'a')
INSERT INTO test (a,b) VALUES (11,'b')
INSERT INTO test (a,b) VALUES (12,'c')

SELECT * FROM test;

03_09 Filtering Data

-- Order by population in descending order the countries with population less than 100k
SELECT Name, Continent, Population From Country
  WHERE Population < 100000 ORDER BY Population DESC;
-- Order by population in descending order the countries with population less than 100k or if population is null
SELECT Name, Continent, Population From Country
  WHERE Population < 100000 OR Population IS NULL
  ORDER BY Population DESC;
-- Order by population in descending order the countries with population less than 100k AND Continent is Oceania
SELECT Name, Continent, Population From Country
  WHERE Population < 100000 OR Continent = 'Oceania'
  ORDER BY Population DESC;
-- %island% wildcard means anything that has letters island somewhere in the string will be selected and we order the results by Name alphabetically
SELECT Name, Continent, Population From Country
  WHERE Name LIKE '%island%' ORDER BY Name;
SELECT Name, Continent, Population From Country
  WHERE Name LIKE '_nd%' ORDER BY Name;
SELECT Name, Continent, Population From Country
  WHERE Continent IN ('Europe','Asia') ORDER BY Name;

03_10 Removing Duplicates

-- Gives us only unique entries from a column
SELECT DISTINCT Continent FROM Country;

-- test.db
DROP TABLE IF EXISTS test;
CREATE TABLE test ( a int, b int );
INSERT INTO test VALUES ( 1, 1 );
INSERT INTO test VALUES ( 2, 1 );
INSERT INTO test VALUES ( 3, 1 );
INSERT INTO test VALUES ( 4, 1 );
INSERT INTO test VALUES ( 5, 1 );
INSERT INTO test VALUES ( 1, 2 );
INSERT INTO test VALUES ( 1, 2 );
INSERT INTO test VALUES ( 1, 2 );
INSERT INTO test VALUES ( 1, 2 );
INSERT INTO test VALUES ( 1, 2 );
SELECT * FROM test;
SELECT DISTINCT a FROM test;
SELECT DISTINCT b FROM test;
SELECT DISTINCT a, b FROM test;

DROP TABLE IF EXISTS test;

03_11 Ordering Output

SELECT Name FROM Country;
SELECT Name FROM Country ORDER BY Name;
-- Sort in Descending Order
SELECT Name FROM Country ORDER BY Name DESC;
SELECT Name FROM Country ORDER BY Name ASC;
-- Sort by multiple columns, here we order by Continent and Name Nations of Africa in A - Z order then Nations of Asia in A-Z order
SELECT Name, Continent FROM Country ORDER BY Continent, Name;
-- Sort by continent in descending order and then Region and Name in Ascending Order.
SELECT Name, Continent, Region FROM Country ORDER BY Continent DESC, Region, Name;

03_12 Conditional Expressions

DROP TABLE IF EXISTS booltest;
CREATE TABLE booltest (a INTEGER, b INTEGER);
INSERT INTO booltest VALUES (1,0);
SELECT * FROM booltest;

-- Conditional expressions
SELECT
    CASE WHEN a THEN 'true' ELSE 'false' END as boolA,
    CASE WHEN b THEN 'true' ELSE 'false' END as boolb,