Professional Documents
Culture Documents
Once you drop a table - IT'S PERMANENT. WON'T COME BACK. THAT'S ITTTTT.
FOREIGN KEY (field) --> variable within table we're calling from elsewhere
REFERENCES table(KEY) --> collects the reference data from the other table
TRUNCATE TABLE table_name; Removes all records from a table but won't destroy the table structure
ALTER TABLE table ADD(key Add new field to a table (e.g., ALTER TABLE customer ADD(email
DATATYPE) Varchar(100));
ALTER TABLE table MODIFY key
datatype
RENAME TABLE table TO rename table
new_name
Bootcamp Page 1
If you had an employee who was in multiple departments then you'd need a:
- department table
- employee table
- department/employee table (aka junction table)
Bootcamp Page 2
Bootcamp Page 3
getdate gets the current datetime
Viewing data:
Bootcamp Page 4
Functions:
Bootcamp Page 5
CaseSensitivity is dependent on the program you're using.
Specifying range:
Bootcamp Page 6
Narrowing down search terms:
Bootcamp Page 7
Counting all unique countries in the database!
SQL REVISION!
SubQueries:
SELECT Name, Population Shows the city and population of the MAX populated
Bootcamp Page 8
SELECT Name, Population Shows the city and population of the MAX populated
FROM Country city in the table.
WHERE Population =
(SELECT max(population) FROM country);
Constraints:
Bootcamp Page 9
^ as above, plus We only want to see the results from employees 10 and 50.
The values we want to see are IN the brackets.
AND emp_no IN (10, 50);
SELECT * This shows only rows with departments containing 'sys' in their names (i.e., systems).
FROM dept This can be capitalised or lowercase, denoted by the enclosing %.
WHERE dept_name LIKE '%SYS%' ;
WHERE field IS NOT NULL; Doesn't show rows where the specified field is empty.
SELECT Coalesce replaces any NULL value in field with the 2nd argument.
COALESCE (field, 'field unknown') as New Field
FROM table;
SELECT order_no, order_value, name Joining a field from one table to another. Table Aliases are used to denote which table
FROM sale S the field belongs in (and aids with readability).
INNER JOIN company C ON S.company_no =
C.company_no
SEE EXAMPLE BELOW!
Sale: Company:
These fields link these two different tables, so we script this with:
It knows we want to use the 'name' field from company because that's what we
specified after SELECT, so this is our output:
Joined table:
order_no order_value name
1 100 4 Supermarket
2 200 6 Retailer
3 300 17 Tech
Outer joins:
--p2) Copy in the 3rd query from p1) and change the word INNER to the word RIGHT
-- This is called an outer JOIN, inserting the word RIGHT means literally
-- "include every row from the table on the RIGHT of the word JOIN",
-- (even if there is no matching row in the table on the LEFT).
-- p4) ‘Can I do a LEFT & a RIGHT JOIN at the same time?’ is a question often asked.
-- Yes, it’s called a FULL JOIN.
Bootcamp Page 10
Manager Surname
-------------------- -------------------------
Adam Apricot Brick
Barbara Banana Custard
Barbara Banana Digger
Paul Peach Ernst
Paul Peach Flipper
Paul Peach Goalie
Diver Dan ** Nobody in this dept **
Xavier Xylophone ** Nobody in this dept **
** Has no manager ** Ell
-- But if you were now to include 'dept_no' in this query, should you choose
-- SP.dept_no or D.dept_no, because one of the rows is surely going to have a NULL dept_no?
-- Well you could include either and COALESCE it to remove the NULL, but there is one problem.
-- Namely, the 'COALESCE' function requires that both parameters are of the same data type,
-- so
INSERT INTO table Add a new line of data and type values in order
VALUES (x, y, z, NULL) of columns.
SELECT * from table Update the values of a column.
UPDATE table
SET column = column + n
DELETE FROM table Say, you want to delete something where the
WHERE column LIKE '% ? %' word contains the specified letter.
UPDATE contact
SET job_title = 'VP of Development', tel = '01242-112233 Ext 444'
WHERE name = 'Ricky Rambo'
Creating tables
CREATE TABLE review NULL / NOT NULL specifies whether the column allows null values
( to exist. If NOT NULL then there must be a given value.
emp_no INT NOT NULL,
review_date DATETIME NOT NULL,
grade CHAR(1) NULL
)
ALTER TABLE review Adding a character column of varying length, not to exceed 40
ADD notes VARCHAR(40) NULL characters.
'Referential integrity' is about a datapoint existing in all necessary tables, so Jack Mason can be found in
'department' AND 'salesperson'. To be missing from one would mean the database is lacking in referential
integrity.
Bootcamp Page 11
----------------------------------------------------------------------------------------------------------------------
-- There is a composite Primary Key constraint defined on the 'emp_no' and 'review_date'
-- columns. The constraint is a separate object in the database which can be 'dropped'
-- independently of the table itself, so it is a good idea to give it a meaningful name
-- like 'pk_review'.
-- Now run this CREATE table syntax and then observe the changed behaviour you get when you
-- either INSERT/UPDATE 'reviews' or DELETE/UPDATE 'salespersons'.
-----------------------------------------------------------------------------------------------------------------------------=
------------------------------------------------------------------------------------------------------------------------
Inserting employee no_90 into the review table didn't work because in the previous CREATE TABLE block we
created a CONSTRAINT. Employee 90 doesn't exist in the salesperson table, so we are prevented from adding
them to review and damaging the referential integrity of the database. This is why we created the
CONSTRAINTs.
---------------------------------------------------------------------------------------------------------------------------
Bootcamp Page 12