MySQL Flashcards
My SQL Comments
–this is a comment #this is a non-standard SQL comment /* multi line comment */
How can you show all columns of a table named MyTable?
SELECT * FROM MyTable
SELECT
Select the following columns for display in the query
AS
Specify aliases/names for the columns SELECT Name AS Country
ORDER BY
Sort based on a column. ORDER BY Continent To sort in ascending/descending order ORDER BY Continent ASC ORDER BY Continent DESC To sort on one criteria then another (two levels): ORDER BY Continent, Name To sort in different sort orders ORDER BY Continent DESC, Region, Name
How can you do multi-level sorting? (First by one column, then another?)
To sort on one criteria then another (two levels): ORDER BY Continent, Name To sort in different sort orders ORDER BY Continent DESC, Region, Name
WHERE
Filters results. Takes a Boolean expression and returns the row only if the expression is true. WHERE Population)
How can you return rows in a query that matches a condition?
WHERE Population<1000
How can you return rows in a column that have a value that matches a pattern?
WHERE Name LIKE ‘%island%’ WHERE Name REGEXP ‘^.[a-e].*$’ %=wildcard - = any character
How can you return rows in a column that match values in a list only?
WHERE CONTINENT IN (‘Europe’, ‘Asia’)
INSERT
Insert new rows into table INSERT INTO test VALUES (1,2,3) To only insert into specified columns INSERT INTO test(b,c) VALUES (2,3) To insert values from a query INSERT INTO test(a,b,c) SELECT id, name, description FROM item
How can you insert a new row into a table?
INSERT INTO test VALUES (1,2,3)
How can you insert values from a query into a tabe?
To insert values from a query INSERT INTO test(a,b,c) SELECT id, name, description FROM item
How can you only insert into specified columns of a table?
INSERT INTO test(b,c) VALUES (2,3)
UPDATE
Change data in particular columns/rows as specified by WHERE UPDATE test SET c=NULL WHERE a=2
How can you change data in particular columns/rows of a table?
UPDATE test SET c=NULL WHERE a=2
DELETE
Deletes/removes rows from a table DELETE FROM test WHERE a=2;
How can you delete all rows in a table?
DELETE FROM test
DROP TABLE
Deletes table from database
How can you delete a table from a databse?
DROP TABLE
How can you concatenate strings in MYSQL?
Strings (in single quotes/double quotes) are separated by spaces for concatenation. ‘hello’ ‘world’
NULL
Represents the lack of a value. Cannot be tested for by using the equal sign since it has no value. a=NULL –invalid To test for equality: a IS NULL -or- a IS NOT NULL
CREATE DATABASE
Creates a new database
How can you create a new database?
CREATE DATABASE newdatabase