Revision Notes - SQL
Revision Notes - SQL
(mysql- rdbms)
SQL, or Structured Query Language, is a domain-specific language used in programming
and managing relational databases. It provides a standardized way of interacting with
databases, allowing users to perform various operations such as querying, updating,
inserting, and deleting data.
Table
What is a Table?
● A table is like a spreadsheet. It has rows and columns where we can store
information.
Rows and Columns:
● Rows are like horizontal lines in the table. Each row holds a single record
or piece of information.
● Columns are like vertical lines in the table. Each column represents a
specific type of information, such as names, ages, or dates.
Database
A database is an organized collection of structured information or
data, typically stored in tables, that can be easily accessed, managed,
and updated.. A database is usually controlled by a database
management system (DBMS).
Relational Database:
The software used to store, manage, query, and retrieve data stored in
a relational database is called a relational database management
system (RDBMS)
RDBMS EX:
1.
2. PostgreSQL
3. Oracle Database
4. Microsoft SQL Server
5. SQLite
6. IBM Db2
7. MariaDB
MySQL:
MySQL Workbench:
Revision
TOPIC 2:
Database keys
● Primary Key
● Super Key
● Alternate Key
● Foreign Key
● Composite Key
Primary Key:
● A primary key is a unique identifier for each record in a table.
● It cannot contain NULL values and must be unique for each record.
● Only one primary key can exist per table.
Candidate Key:
● A candidate key is a column or a set of columns in a table that uniquely identifies
each row in the table.
● There can be multiple candidate keys in a table.
● From the set of candidate keys, one is chosen as the primary key.
Alternate Key:
● An alternate key, also known as a secondary key, is any candidate key that is not
selected as the primary key.
● While it uniquely identifies rows, it is not chosen as the primary means of
identification.
Super Key:
● A super key is a set of one or more columns that can uniquely identify each row
in a table.
● It may contain more columns than necessary for uniqueness.
● Any subset of a super key is also a super key.
Foreign Key:
● A foreign key column in a table points to a column with unique values
in another table (often the primary key column) to create a way of
cross-referencing the two tables.
● It establishes a relationship between two tables.
● It ensures referential integrity by enforcing a link between the data in two tables.
Composite Key:
● A composite key is a key that consists of two or more columns in a table.
● Together, these columns uniquely identify a record in the table.
● Unlike primary keys, composite keys can contain NULL values, but their
combination must be unique.
TOPIC 3:
SQL Commands
In SQL (Structured Query Language), there are five main types of commands:
● DDL : Data Definition Language
● DML : Data Manipulation Language
● DQL : Data Query Language
● DCL : Data Control Language
● TCL : Transaction Control Language
● Data Definition Language consists of the SQL commands that can be used to define
the Database schema.
● It simply deals with descriptions of the database schema and is used to create and
modify the
structure of database objects in the database.
● CREATE, ALTER, TRUNCATE and DROP are DDL commands.
1. create
Example:
Example 2
Rules for naming tables and columns
● The name may contain letters (A-Z, a-z), digits (0-9), under score (_) and dollar ($)
symbol.
● The name must contain at least one character. (Names with only digits are invalid).
● The name must not contain white spaces, special symbols.
● The name must not be an SQL keyword.
● The name should not duplicate with the names of other tables in the same database
and with
other columns in the same table.
2. Drop
DROP is a DDL command used to delete/remove the database objects from the SQL
database. We can easily remove the entire table, view, or index from the database using
this DDL command.
ALTER is a DDL command which changes or modifies the existing structure of the
database, and it also changes the schema of database objects.
We can also add and drop constraints of the table using the ALTER command.
FIRST and AFTER clauses are used in the ALTER TABLE statement to specify the
position where
a new column should be added within a table. These clauses are used when you
want to specify
whether the new column should be the first column or be positioned before an
existing column.
Add a new column at the beginning
To delete a column in a table, use the following syntax (notice that some
database systems don't allow deleting a column):
The following SQL deletes the "Email" column from the "Customers" table:
(column_name);
4. TRUNCATE
The TRUNCATE TABLE statement is used to delete all rows from a table quickly and
efficiently, without logging individual row deletions. Unlike the DELETE statement, which
removes rows one by one and generates a log entry for each deleted row, TRUNCATE
TABLE removes all rows in a single operation, making it faster, especially for large
tables.
Constraints
Constraints in SQL are rules defined on columns or tables to enforce data integrity and
ensure that data values meet certain conditions. Here are some common constraints in
SQL:
1. NOT NULL: Ensures that a column cannot contain NULL values.
2. UNIQUE: Ensures that all values in a column are unique.
3. PRIMARY KEY: Combines the NOT NULL and UNIQUE constraints. It uniquely
identifies each record in a table.
4. FOREIGN KEY: Ensures referential integrity by enforcing a link between data in
two tables.
5. CHECK: Ensures that all values in a column meet specified conditions.
6. DEFAULT: Provides a default value for a column when no value is specified.
DepartmentID INT);
);
In this example:
Constraints play a vital role in maintaining data integrity and ensuring that databases
remain consistent and reliable.
ddl
Create: db, table
Alter: change
Note: you can perform Data Manipulation Language (DML) operations using the primary
key (PK) in SQL. The primary key uniquely identifies each row in a table, so it can be
used to target specific rows for INSERT, UPDATE, DELETE, or SELECT operations. Here's
how you can use the primary key in different DML operations:
1. INSERT: When inserting new rows into a table, you typically don't include the
primary key column in the VALUES list because it's often an auto-incremented or
2. UPDATE: When updating existing rows, you can use the primary key in the
3. DELETE: When deleting rows from a table, you can use the primary key in the
4. SELECT: When retrieving data from a table, you can use the primary key to fetch
example1:I
2, DQL SELECT
This query retrieves the "gender" and "lunch" columns for rows where the
"gender" column has the value "female".
4. Select Distinct Values:
SELECT DISTINCT gender, lunch FROM college.sqldataset;
This query retrieves unique combinations of values for the "gender" and "lunch"
columns from the "sqldataset" table.
This query retrieves the "gender" and "lunch" columns for rows where the "lunch"
column contains the substring "sta".
3. DML Update
1. Update a Single Row (reg_no = 1):
UPDATE college.sqldataset
SET gender = 'Vegetarian'
WHERE reg_no = 1;
● This query updates the "gender" column to 'Vegetarian' for the row where
the "reg_no" column equals 1.
● The WHERE clause ensures that only the row with "reg_no" equal to 1 is
updated.
UPDATE college.sqldataset
SET gender = 'Vegetarian'
WHERE reg_no IN (1,2,3,4);
● This query updates the "gender" column to 'Vegetarian' for the rows where
the "reg_no" column matches any value in the list (1, 2, 3, or 4).
● The IN operator allows us to specify multiple values to match against.
Notes:
● The UPDATE statement modifies existing data in a table by changing the values
of specified columns.
Answer:
● "DROP" is used to permanently remove objects like tables, views, or indexes from
the database. It's a DDL command and results in the complete elimination of the
object and its definition.Example: DROP TABLE TableName;
● "DELETE" is used to remove specific rows from a table based on specified
criteria. It's a DML command and leaves the table structure intact while removing
selected data.Example: DELETE FROM TableName WHERE condition;
● "TRUNCATE" is used to remove all rows from a table in a single operation,
effectively resetting the table to its initial state. It's faster than DELETE as it
doesn't generate individual row-wise transactions for deletion, but it also doesn't
allow specifying conditions for which rows to delete. It's also a DDL
command.Example: TRUNCATE TABLE TableName;
Codes:
use students;
select * from 10a;
VALUES (11,"hello","female",100);
UPDATE 10a
UPDATE 10a
CONSTRAINS:
);