Overview of Database Languages
Overview of Database Languages
SQL COMMANDS
SQL commands are instructions, they are used to communicate with the database, they are
also used to perform specific tasks, functions and queries of data.
SQL can perform various tasks like create a table, add data to tables, drop the table, to
modify the table, set permissions for users etc.
Types of SQL Commands
There are five main types of SQL commands: Data Definition Language (DDL), Data Query
Language (DQL), Data Manipulation Language (DML), Data Control Language (DCL), and
Transaction Control Language (TCL).
DATATYPES
In the context of database, a datatype defines the kind of data that can be stored in a
particular column of a database table. It specifies the type of value (e.g. number, text, data)
and determines how much memory the data will occupy as well as the operations that can
be performed on it.
Common Database Datatypes:
1. Numeric Datatypes:
These store numeric values.
• INT: Integer values (whole numbers)
Examples: 122, -45 etc.
• DECIMAL (p, s): Fixed point numbers with Precision (p) and Scale (s).
Example: Decimal (5, 2). This stores up to five (5) digits, with two (2) after the
decimal point, (e.g. 123.56)
2. Character Datatypes:
They are used to store text or strings.
• CHAR (n): Fixed length string with exactly n characters.
Example: CHAR (5) stores “Hello” or “He ” (padded with spaces).
4. Binary Datatypes:
They are used for binary data like files or images.
• BLOB: Binary Large Object for storing binary files (images, videos etc.)
5. Boolean Datatype:
They store True or False values.
• BOOL: Stores TRUE or FALSE.
Some databases use SMALLINT where 1 = TRUE and 0 = FALSE
Choosing the right datatype
2. Creating a Table
•Used to define a new table in the database.
•Syntax:
CREATE TABLE table_name
(
column1 datatype constraints,
column2 datatype constraints,
...
);
Example:
CREATE TABLE students (
id INT PRIMARY KEY,
name VARCHAR(50) NOT NULL,
age INT,
enrollment_date DATE
);
4. Updating Records
•Modifies existing records in a table.
•Syntax:
UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition;
Example:
UPDATE students
SET age = 21
WHERE id = 1;
5. Deleting Records
•Removes records from a table.
•Syntax:
DELETE FROM table_name
WHERE condition;
Example:
DELETE FROM students
WHERE id = 1;
6. Altering Tables
•Modifies an existing table structure.
•Add a Column:
ALTER TABLE table_name
ADD column_name datatype;
•Drop a Column:
ALTER TABLE table_name
DROP COLUMN column_name;
Example:
ALTER TABLE students
ADD email VARCHAR(100);
7. Dropping Tables
• Deletes a table from the database.
• Syntax:
DROP TABLE table_name;
Example:
DROP TABLE students;
8. Creating Indexes
•Improves the speed of data retrieval.
•Syntax:
CREATE INDEX index_name
ON table_name (column_name);
Example:
CREATE INDEX idx_name
ON students (name);
Etc…
CONSTRAINTS
In a database, constraints are rules applied to table columns to ensure the integrity,
accuracy, and reliability of the data. They help enforce certain conditions on data before it is
inserted, updated, or deleted.
Here are the common types of constraints in a database:
1. NOT NULL
•Ensures that a column cannot have NULL values.
•Syntax:
CREATE TABLE table_name (
column_name datatype NOT NULL
);
Example:
CREATE TABLE students (
id INT NOT NULL,
name VARCHAR(50) NOT NULL
);
2. UNIQUE
•Ensures that all values in a column are distinct.
•Syntax:
CREATE TABLE table_name (
column_name datatype UNIQUE
);
Example:
CREATE TABLE students
(
id INT UNIQUE,
email VARCHAR(100) UNIQUE
);
3. PRIMARY KEY
•Uniquely identifies each row in a table. Combines NOT NULL and UNIQUE.
•A table can have only one primary key.
•Syntax:
CREATE TABLE table_name (
column_name datatype PRIMARY KEY
);
Example:
CREATE TABLE students
(
id INT PRIMARY KEY,
name VARCHAR(50) NOT NULL
);
•For composite keys:
CREATE TABLE enrollments (
student_id INT,
course_id INT,
PRIMARY KEY (student_id, course_id)
);
4. FOREIGN KEY
•Ensures referential integrity by linking a column to the primary key of another table.
•Syntax:
CREATE TABLE table_name (
column_name datatype,
FOREIGN KEY (column_name) REFERENCES
parent_table (primary_key_column)
);
Example:
CREATE TABLE enrollments (
student_id INT,
course_id INT,
FOREIGN KEY (student_id) REFERENCES students (id)
);
5. CHECK
•Ensures that column values meet specific conditions.
•Syntax:
CREATE TABLE table_name
(
column_name datatype CHECK (condition)
);
Example:
CREATE TABLE students (
id INT PRIMARY KEY,
age INT CHECK (age >= 18)
);
6. DEFAULT
•Assigns a default value to a column when no value is provided.
•Syntax:
CREATE TABLE table_name (
column_name datatype DEFAULT default_value
);
Example:
CREATE TABLE students (
id INT PRIMARY KEY,
name VARCHAR(50),
enrollment_date DATE DEFAULT CURRENT_DATE
);
7. AUTO_INCREMENT (MySQL-specific)
•Automatically generates a unique number for a column, usually for primary keys.
•Syntax:
CREATE TABLE table_name (
column_name INT AUTO_INCREMENT PRIMARY KEY
);
Example:
CREATE TABLE students
(
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(50)
);
Constraint Description
NOT NULL Ensures column cannot contain NULL values.
Here’s a complete workflow using SQL commands to manage a database for a student
management system.
-- Create a database:
CREATE DATABASE StudentManagement;
The (*) sign represents ALL. So (SELECT * FROM Students;) means select all from students.
-- Start a transaction:
START TRANSACTION;
UPDATE Students
SET Major = 'Chemistry'
WHERE ID = 4;
This workflow gives you an end-to-end example of using SQL commands to manage a
database. Let me know if you’d like help with setting up an environment to test these
commands, like MySQL, PostgreSQL, or SQLite!.
• ‘AND’ operator allows you to add additional criteria WHERE statement. It is known as
CONJUNCTION operation, the AND statement is true only when both criteria
connected by AND is true.
Example:
Here the students’ lastname and firstname will be listed only if their age is between 16 and
25 years and their department is Computer Science.
• ‘OR’ DISJUNCTION operator can also be used with WHERE statement, unlike AND both
conditions must not be true to appear in result but any of the conditions must be satisfied.
• ‘NOT’ NEGATION can also be used with WHERE statement to display values in which the
specified condition is not true.
For example, to pull other students except Computer Science students from the database,
it can be queried thus:
Example:
SELECT lastname, firstname
FROM student, dept
WHERE NOT department_name = “Computer Science”
• LIMIT function
This can be used to limit the number of results that can be gotten especially when sampling
is needed from a voluminous database.
• VIEWS
Views are virtual tables created by selecting fields from one or more tables present in the
database. They also have rows and columns. They can have specific rows based on certain
conditions or all the rows of a table.
Creating a view from a single table, for example let’s create a view named Student_Detail
from a table named student.
CREATE VIEW Student_Detail AS
SELECT Matno, Name, gender
WHERE age<=20;
To delete a view:
Example:
DROP Student_score;