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;