Documente Academic
Documente Profesional
Documente Cultură
What is SQL?
Why SQL?
Allows users to define the data in database and manipulate that data.
SQL Commands
Standard SQL commands are CREATE, SELECT, INSERT, UPDATE, DELETE and DROP.
These commands can be classified into groups based on their nature:
DDL - Data Definition Language
DML - Data Manipulation Language
DCL - Data Control Language
DQL - Data Query Language
Description
CREATE
ALTER
DROP
Description
INSERT
Creates a record
UPDATE
Modifies records
DELETE
Deletes records
REVOKE
Table
The table is a collection of related data entries and it consists of columns and rows.
A table is the most common and simplest form of data storage in a relational database.
Field
The fields in the CUSTOMERS table consist of ID, NAME, AGE, ADDRESS and SALARY.
Record or Row
A record, also called a row of data, is each individual entry that exists in a table.
Column
A column is a vertical entity in a table that contains all information associated with a specific
field in a table.
NULL value
A NULL value is different than a zero value or a field that contains spaces.
SQL Constraints:
This ensures the accuracy and reliability of the data in the database.
Types of Constraints:
PRIMARY KEY
FOREIGN KEY
DEFAULT Constraint
CHECK Constraint
UNIQUE Constraint
NULL :-
Here Null is not the same as no data, rather, it represents unknown data.
PRIMARY Key:
A primary key is a field in a table which uniquely identifies each row/record in a database
table.
When multiple fields are used as a primary key, they are called a composite key.
Create a PRIMARY KEY constraint on the "ID" column when CUSTOMERS table already exists,
Create a PRIMARY KEY constraint on the "ID" and "NAMES" columns when CUSTOMERS table
already exists.
FOREIGN Key:
A foreign key is a key used to link two tables together. This is sometimes called a referencing
key.
The relationship between 2 tables matches the Primary Key in one of the tables with a Foreign
Key in the second table.
Example:
CUSTOMERS table:
ORDERS table:
If ORDERS table has already been created, and the foreign key has not yet been set.
DEFAULT Constraint:
The DEFAULT constraint provides a default value to a column when the INSERT INTO statement
does not provide a specific value.
Example
CUSTOMERS table has already been created, then to add a DFAULT constraint to SALARY column.
CHECK Constraint:
The CHECK Constraint enables a condition to check the value being entered into a record. If
the condition evaluates to false, the CHECK constraint doesnt allows the value entered/record
into the table/column.
CUSTOMERS table has already been created, then to add a CHECK constraint to AGE column.
UNIQUE Constraint:
The UNIQUE Constraint prevents two records from having identical values in a particular
column.
CUSTOMERS table has already been created, then to add a UNIQUE constraint to AGE column.
Explanation of Relationships
One to One : In theOne-To-One relationeach row in the first table may match only one row in
the second and the other way around.
One to Many : InOne-To-Many relationa row in one of the tables can have many matching
rows in the second table, but a row the second table can match only one row in the first table.
Many to Many :In theMany-To-Many relation. many rows from the first table can match many
rows in the second and the other way around. To define this type of relation you need a third
table whose primary key is composed of the 2 foreign keys from the other 2 table.