Structured Query Language Flashcards
What are the four key characteristics of SQL?
- Set-oriented and declarative;
- Free-form language;
- Case insensitive;
- Can be used both interactively from a command prompt or executed by a program.
Code for check existing databases:
SHOW DATABASES;
Code for creating a database:
CREATE DATABASE [IF NOT EXISTS] database_name
[CHARACTER SET charset_name]
[COLLATE collation_name]
Code for use a database:
USE database_name;
Code for drop a database:
DROP DATABASE [IF EXISTS] database_name
Code for see the tables present in the database:
SHOW TABLES;
(after Use command)
Code for creating a table:
CREATE TABLE [IF NOT EXISTS] table_name(
column_name data_type(length) [NOT NULL] [DEFAULT value]
[AUTO_INCREMENT]
col2 data_type(length) [NOT NULL] [DEFAULT value] [AUTO_INCREMENT]
col3 data_type(length) [NOT NULL] [DEFAULT value] [AUTO_INCREMENT]
) ENGINE = storage_engine;
In the following code what does NOT NULL does?
CREATE TABLE [IF NOT EXISTS] table_name(
column_name data_type(length) [NOT NULL] [DEFAULT value]
[AUTO_INCREMENT]
col2 data_type(length) [NOT NULL] [DEFAULT value] [AUTO_INCREMENT]
col3 data_type(length) [NOT NULL] [DEFAULT value] [AUTO_INCREMENT]
) ENGINE = storage_engine;
It indicates that the column does not allow NULL
In the following code what does DEFAULT does?
CREATE TABLE [IF NOT EXISTS] table_name(
column_name data_type(length) [NOT NULL] [DEFAULT value]
[AUTO_INCREMENT]
col2 data_type(length) [NOT NULL] [DEFAULT value] [AUTO_INCREMENT]
col3 data_type(length) [NOT NULL] [DEFAULT value] [AUTO_INCREMENT]
) ENGINE = storage_engine;
The default value is used to specify the default value of the column.
In the following code what does AUTO INCREMENT does?
CREATE TABLE [IF NOT EXISTS] table_name(
column_name data_type(length) [NOT NULL] [DEFAULT value]
[AUTO_INCREMENT]
col2 data_type(length) [NOT NULL] [DEFAULT value] [AUTO_INCREMENT]
col3 data_type(length) [NOT NULL] [DEFAULT value] [AUTO_INCREMENT]
) ENGINE = storage_engine;
The auto increment indicates that the value of the column is incremented by one automatically whenever a new row is inserted into the table. Each table has one and only one AUTO_INCREMENT column.
In the following code what does ENGINE does?
CREATE TABLE [IF NOT EXISTS] table_name(
column_name data_type(length) [NOT NULL] [DEFAULT value]
[AUTO_INCREMENT]
col2 data_type(length) [NOT NULL] [DEFAULT value] [AUTO_INCREMENT]
col3 data_type(length) [NOT NULL] [DEFAULT value] [AUTO_INCREMENT]
) ENGINE = storage_engine;
We can optionally specify the storage engine for the table in the ENGINE clause. We can use any storage engine such as InnoDB and MyISAM. If we don’t explicitly declare the storage engine, MySQL will use InnoDB by default.
What are examples of datatypes?
- CHAR (size)
- VARCHAR (size)
- TEXT
- INT (size)
- FLOAT (size, d): small number with a floating decimal point
- DOUBLE (size, d): large number with a floating decimal point
- DATE()
- YEAR()
Code to create primary keys:
CREATE TABLE [IF NOT EXISTS] table_name(
col1 data_type(length)[PRIMARY KEY]
col2 data_type(length)[PRIMARY KEY]
col3 data_type(length)[PRIMARY KEY]
);
Or
CREATE TABLE [IF NOT EXISTS] table_name(
col1 data_type(length)
col2 data_type(length)
col3 data_type(length)
PRIMARY KEY(col1, col2, …)
);
Code to create foreign keys:
CREATE TABLE [IF NOT EXISTS] table_name(
col1 data_type(length)
PRIMARY KEY(col1, col2, …)
CONSTRAINT constraint_name
FOREIGN KEY (columns)
REFERENCES parent_table (columns)
ON DELETE action
ON UPDATE action
);
What does the Constraint clause do in the following code:
CREATE TABLE [IF NOT EXISTS] table_name(
col1 data_type(length)
PRIMARY KEY(col1, col2, …)
CONSTRAINT constraint_name
FOREIGN KEY (columns)
REFERENCES parent_table (columns)
ON DELETE action
ON UPDATE action
);
Allows us to define the constraint name for the foreign key constraint. If we omit it, MySQL will generate a name automatically.
What does the Foreign key clause do in the following code:
CREATE TABLE [IF NOT EXISTS] table_name(
col1 data_type(length)
PRIMARY KEY(col1, col2, …)
CONSTRAINT constraint_name
FOREIGN KEY (columns)
REFERENCES parent_table (columns)
ON DELETE action
ON UPDATE action
);
Specifies the columns in the child table that refer to primary key columns in the parent table.
What does the References clause do in the following code:
CREATE TABLE [IF NOT EXISTS] table_name(
col1 data_type(length)
PRIMARY KEY(col1, col2, …)
CONSTRAINT constraint_name
FOREIGN KEY (columns)
REFERENCES parent_table (columns)
ON DELETE action
ON UPDATE action
);
Specifies the parent table and its columns to which the columns in the child table refer. The number of columns in the child table and parent table specified in the foreign key and references must be the same.
What does the On delete clause do in the following code:
CREATE TABLE [IF NOT EXISTS] table_name(
col1 data_type(length)
PRIMARY KEY(col1, col2, …)
CONSTRAINT constraint_name
FOREIGN KEY (columns)
REFERENCES parent_table (columns)
ON DELETE action
ON UPDATE action
);
Allows us to define what happens to the records in the child table when the records in the parent table are deleted. If we omit the on delete clause and delete a record in the parent table that has records in the child table refer to, MySQL will reject the deletion. Possible actions: CASCADE, SET NULL, NO ACTION.
What does the On update clause do in the following code:
CREATE TABLE [IF NOT EXISTS] table_name(
col1 data_type(length)
PRIMARY KEY(col1, col2, …)
CONSTRAINT constraint_name
FOREIGN KEY (columns)
REFERENCES parent_table (columns)
ON DELETE action
ON UPDATE action
);
Enables us to define what happens to the rows in the child table when the records in the parent table are updated. We can omit the on update clause to let MySQL reject any updates to the rows in the child table when the rows in the parent table are updated. Possible actions: CASCADE, SET NULL, NO ACTION.
Code to drop a table:
DROP TABLE [IF EXISTS] table_name[, table_name] …
What does the if exists addition do in the following code?
DROP TABLE [IF EXISTS] table_name[, table_name] …
It helps to prevent from the attempt of removing non-existent tables. When we use this addition, MySQL generates a NOTE, which can be retrieved by using the SHOW WARNING statement. It is important to note that this statement removes all existing tables and issues an error message or a note when we have a non-existent table in the list.
What are the CRUD operations?
- Create (insert)
- Read
- Update
- Delete
Code for insert data into table:
INSERT INTO table_name(c1,c2,…)
VALUES (v1, v2, …)
Code for read table:
SELECT
column_1, column_2, …
FROM
table_1
[INNER | LEFT | RIGHT] JOIN table_2 ON conditions
WHERE
conditions
GROUP BY column_1
HAVING group_conditions
ORDER BY column_1
LIMIT offset, length;
Code for update a table:
UPDATE [LOW_PRIORITY][IGNORE] table_name
SET
column_name1 = expr1,
column_name2 = expr2,
[WHERE
condition];
Code for delete data from a table:
DELETE FROM table_name
WHERE condition;
Describe the limit clause and how to use it.
The limit clause is used in the SELECT statement to constrain the number of rows to return.
Code:
SELECT
select_list
FROM
table_name
LIMIT [offset,] row_count;
What does the offset do in the following code?
SELECT
select_list
FROM
table_name
LIMIT [offset,] row_count;
The offset specifies the offset of the first row to return. The offset of the first row is 0 and not 1.
What does the row_count do in the following code?
SELECT
select_list
FROM
table_name
LIMIT [offset,] row_count;
The row_count specifies the maximum number of rows to return.
What does the distinct clause do?
Is to remove duplicates and it is used with select.
Example:
SELECT DISTINCT e.first_name FROM employees AS e LIMIT 10;
What does the between operator do?
Is a logical operator that allows us to specify whether a value is within a range or not.
Example:
SELECT e.emp_no, e.first_name, e.last_name, e.hire_date
FROM employees AS e
WHERE e.hire_date BETWEEN ‘1999-01-01’ AND ‘199-12-31’
What is the like operator?
Is a logical operator that tests whether a string contains a specified pattern or not.
Example: retrieve the employees whose last names begin with ‘T’:
SELECT e.first_name, e.last_name
FROM employees AS e
WHERE e.last_name LIKE ‘T%’
In the following code what is ‘%’
SELECT e.first_name, e.last_name
FROM employees AS e
WHERE e.last_name LIKE ‘T%’
Is a wildcard. Represents zero or more characters
Example: T% finds Tires, Teodoro, Timóteo
In the following code what is ‘_’
SELECT e.first_name, e.last_name
FROM employees AS e
WHERE e.last_name LIKE ‘T%’
Is a wildcard. Represents a single character.
Example: h_t finds hot, hat, and hit
Is there a not like operator?
Yes.
Example: retrieve the employees where the last name does not begin with ‘Tr’:
SELECT e.first_name, e.last_name
FROM employees AS e
WHERE e.last_name NOT LIKE ‘Tr%’;
What is the code for MySQL alias for columns?
SELECT
[column_1 | expression] AS descriptive_name
FROM table_name;
What is the code for MySQL alias for tables?
table_name AS table_alias