Additional SQL Notes
Additional SQL Notes
Auto-increment allows a unique number to be generated when a new record is inserted into a
table.
By default, the starting value for AUTO_INCREMENT is 1, and it will increment by 1 for each
new record. To insert a new record into the created table, you will not have to specify a value for
the primary key column (a unique value will be added automatically.
To let the AUTO_INCREMENT sequence start with another value, use the following SQL
statement.
To insert more than one record at once, we can do this, with each set of field values
separated by a comma:
VALUES
The WHERE clause is used to extract only those records that fulfill a specified criterion.
Operator Description
= Equal
<> Not equal
> Greater than
< Less than
>= Greater than or equal
<= Less than or equal
BETWEEN Between an inclusive range
LIKE Search for a pattern
IN If you know the exact value you want to return for at least one of the columns
In SQL, the AND & OR operators are used to filter records based on more than one condition.
The AND operator displays a record if both the first condition and the second condition is true.
The OR operator displays a record if either the first condition or the second condition is true.
In SQL, The ORDER BY keyword is used to sort the result-set by a specified column.
If you want to sort the records in a descending order, you can use the DESC keyword.
SQL OR Operator
To select only the persons with the first name equal to "Dave" OR the first name equal to "John"
A FOREIGN KEY is a field (or collection of fields) in one table that refers to the PRIMARY
KEY in another table.
The FOREIGN KEY constraint is used to prevent actions that would destroy links between
tables. The table with the foreign key is called the child table, and the table with the primary key
is called the referenced or parent table.
Example:
Student Table
Course table
Explanation:
1. The "StudentID" column in the Course table points to the "studentID" column in the
student table.
Note: - The FOREIGN KEY constraint prevents invalid data from being inserted into the foreign
key column, because it has to be one of the values contained in the parent table.
The following SQL creates a FOREIGN KEY on the "studentID" column when the "course"
table is created:
SQl command:
CREATE TABLE course (courseCode int NOT NULL, courseName text NOT NULL,
studentID int,