MySQL Functionsa3
MySQL Functionsa3
Functions
A function is a predefined command set that performs some operation and returns the single
value.
Indexing in Databases
Indexing improves database performance by minimizing the number of disc visits required to
fulfill a query. It is a data structure technique used to locate and quickly access data in
databases. The main key or candidate key of the table is duplicated in the first column, which is
the Search key. To speed up data retrieval, the values are also kept in sorted order. The second
column is the Data Reference or Pointer which contains a set of pointers holding the address of
the disk block where that particular key value can be found.
MySQL UNION
Summary: in this tutorial, you will learn how to use MySQL UNION operator to combine two or
more result sets from multiple SELECT statements into a single result set.
MySQL UNION operator
MySQL UNION operator allows you to combine two or more result sets of queries into a single
result set. The following illustrates the syntax of the UNION operator:
SELECT column_list
UNION [DISTINCT | ALL]
SELECT column_list
UNION [DISTINCT | ALL]
SELECT column_list
...Code language: SQL (Structured Query Language) (sql)
To combine result set of two or more queries using the UNION operator, these are the basic
rules that you must follow:
First, the number and the orders of columns that appear in all SELECT statements must
be the same.
Second, the data types of columns must be the same or compatible.
By default, the UNION operator removes duplicate rows even if you don’t specify the DISTINCT
operator explicitly.
UNION vs. JOIN
A JOIN combines result sets horizontally, a UNION appends result set vertically. The following
picture illustrates the difference between UNION and JOIN:
JOIN
Joins are used in relational databases to combine data from multiple tables based on a common
column between them. A foreign key may be used to reference a row in another table and join
can be done based on those columns. Two or more tables may have some related data, and to
combine all the data from multiple tables joins are used.
There are different types of joins in MySQL.
1. INNER JOIN
2. LEFT JOIN
3. RIGHT JOIN
4. CROSS JOIN
INNER JOIN
The INNER JOIN produces the output by combining those rows which have matching column
values.
Syntax:
SELECT column_names
FROM table1
INNER JOIN table2 ON table1.common_column=table2.common_column
INNER JOIN table3 ON table1.common_column=table3.common_column
...;
LEFT JOIN
The LEFT JOIN returns all the rows from the left table ‘A’ and the matching rows from the right
table ‘B’ in the join. The rows from the left table, which have no matching values in the right
table will be returned with a NULL value in the link column.
Syntax:
SELECT column_names
FROM table1
LEFT JOIN table2 ON table1.common_column=table2.common_column;
RIGHT JOIN
The RIGHT JOIN returns all the rows from the right table ‘B’ and the matching rows from the left
table ‘A’ in the join. The rows from the right table, which have no matching values in the left
table will be returned with a NULL value in the link column.
Syntax:
SELECT Column_names
FROM table1
RIGHT JOIN table2 ON table1.common_column=table2.common_column;
CROSS JOIN
CROSS JOIN returns the cartesian product of rows from the tables in the join. It combines each
row of the first table with each row of the second table. If there are X rows in the first table and
Y rows in the second table then the number of rows in the joined table will be X*Y.
Syntax:
SELECT column_names
FROM table1
CROSS JOIN table2;
GROUP BY
The GROUP BY clause is used to arrange the rows in a group using a particular column value. If
there are multiple rows with the same value for a column then all those rows will be grouped
with that column value.
Syntax:
SELECT column1,column2,…
FROM table_name
WHERE condition
GROUP BY column1,column2, …
Order BY column1, column2, ….
The GROUP BY clause is generally used with aggregate functions like SUM, AVG, COUNT, MAX,
MIN. The aggregate functions provide information about each group.
HAVING
The HAVING clause is used with the GROUP BY clause in a query to specify come conditions and
filter some groups or aggregates that fulfill those conditions. The difference between WHERE
and HAVING is, WHERE is used to filter rows, and HAVING is used to filter groups by applying
some conditions.
Syntax:
SELECT column1, column2, …
FROM table_name
WHERE conditions
GROUP BY column1, column2, …..
HAVING conditions
Primary Key
In the relational model, a table shall not contain duplicate rows, because that would create
ambiguity in retrieval. To ensure uniqueness, each table should have a column (or a set of
columns), called primary key, that uniquely identifies every record of the table. For example, an
unique number customerID can be used as the primary key for the customers table; productCode for
products table; isbn for books table. A primary key is called a simple key if it is a single column; it is
called a composite key if it is made up of several columns. Most RDBMSs build an index on the
primary key to facilitate fast search. The primary key is often used to relate to other tables.
Foreign Key
A foreign key of a child table is used to reference the parent table. Foreign key constraint can be
imposed to ensure so-called referential integrity - values in the child table must be valid values
in the parent table.
Sub-Query
Results of one query can be used in another SQL statement. Subquery is useful if more than one
tables are involved.
SELECT with Subquery
In the previous many-to-many product sales example, how to find the suppliers that do not
supply any product? You can query for the suppliers that supply at least one product in the
products_suppliers table, and then query the suppliers table for those that are not in the previous
result set.
mysql> SELECT suppliers.name from suppliers
WHERE suppliers.supplierID
NOT IN (SELECT DISTINCT supplierID from products_suppliers);
View
Views: A view is a virtual table whose contents are taking from other tables depending upon a
condition.
Table: Student
Roll. No. Name Marks
101 Anu 85
102 Riya 70
103 Ankit 78
Definition of the VIEW:
CREATE VIEW toppers AS SELECT * FROM Student WHERE Marks > 75;
Here name of the view is toppers
Base table is students
toppers(A virtual table based on Student table)
Roll. No. Name Marks
101 Anu 85
102 Ankit 78
mysql> DROP VIEW IF EXISTS patient_view;
1. Domain Constraints:
Domain constraints specify that the values of an attribute must be from a specific domain. For
example, an age attribute might be constrained to integer values between 0 and 120.
Enforcement:
Data Type Check: The DBMS enforces domain constraints by checking that the data entered
matches the specified data type (e.g., integer, string, date).
Range Check: Values are checked to ensure they fall within the specified range or set (e.g., age
between 0 and 120).
Pattern Check: For string data, patterns (e.g., regular expressions) can enforce formats like email
addresses or phone numbers.
2. Entity Integrity
Entity integrity is a type of Enforcing integrity constraints, it ensures that each entity (or row) in
a database is uniquely identifiable by a primary key. The primary key must be unique and not
null.
Enforcement:
Primary Key Constraint: The DBMS enforces uniqueness by disallowing duplicate values in the
primary key column(s).
NOT NULL Constraint: Ensures that no null values are allowed in the primary key column(s).
3. Referential Integrity
Referential integrity ensures that a foreign key value always points to an existing, valid record in
another table. This maintains the logical consistency of the data.
Enforcement:
Foreign Key Constraint: The DBMS checks that the foreign key value in one table corresponds to
a primary key value in the referenced table.
Cascading Actions: The DBMS can enforce referential integrity through cascading actions such as
ON DELETE CASCADE or ON UPDATE CASCADE, which automatically propagate changes to
foreign keys when the referenced primary key changes.
4. Unique Constraints
Unique constraints ensure that all values in a column or a group of columns are unique across
the table.
Enforcement:
Unique Index: The DBMS creates a unique index on the column(s) specified, disallowing duplicate
values.
5. Check Constraints
Check constraints enforce a condition that each row in the table must satisfy. These are custom
rules defined by the user.
Enforcement:
Check Clause: The DBMS evaluates the condition specified in the CHECK clause whenever data is
inserted or updated. If the condition is violated, the operation is rejected.
6. NULL Constraints
NULL constraints specify whether a column can accept NULL values or not.
Enforcement:
NOT NULL Constraint: The DBMS ensures that columns specified with NOT NULL cannot store
NULL values.
For example, in the code below, we’re selecting a column called name from a table
called customers.
SELECT name
FROM customers;
SELECT *
SELECT used with an asterisk (*) will return all of the columns in the table we're querying.
The code below would return only rows with a unique name from the customers table.
The code below would return the top 50 results from the customers table:
SELECT name
FROM customers
WHERE name = ‘Bob’;
AND
AND combines two or more conditions in a single query. All of the conditions must be met for
the result to be returned.
SELECT name
FROM customers
WHERE name = ‘Bob’ AND age = 55;
OR
OR combines two or more conditions in a single query. Only one of the conditions must be met
for a result to be returned.
SELECT name
FROM customers
WHERE name = ‘Bob’ OR age = 55;
BETWEEN
BETWEEN filters your query to return only results that fit a specified range.
SELECT name
FROM customers
WHERE age BETWEEN 45 AND 55;
LIKE
LIKE searches for a specified pattern in a column. In the example code below, any row with a
name that included the characters Bob would be returned.
SELECT name
FROM customers
WHERE name LIKE ‘%Bob%’;
Other operators for LIKE:
IN
IN allows us to specify multiple values we want to select for when using the WHERE command.
SELECT name
FROM customers
WHERE name IN (‘Bob’, ‘Fred’, ‘Harry’);
IS NULL
IS NULL will return only rows with a NULL value.
SELECT name
FROM customers
WHERE name IS NULL;
IS NOT NULL
IS NOT NULL does the opposite — it will return only rows without a NULL value.
SELECT name
FROM customers
WHERE name IS NOT NULL;
CREATE
CREATE can be used to set up a database, table, index or view.
CREATE DATABASE
CREATE DATABASE creates a new database, assuming the user running the command has the
correct admin rights.
It goes without saying that the DROP command should only be used where absolutely
necessary.
DROP DATABASE
DROP DATABASE deletes the entire database including all of its tables, indexes etc as well as all
the data within it.
UPDATE customers
SET age = 56
WHERE name = ‘Bob’;
DELETE
DELETE can remove all rows from a table (using ), or can be used as part of a WHERE clause to
delete rows that meet a specific condition.
COUNT
COUNT returns the number of rows that match the specified criteria. In the code below, we’re
using *, so the total row count for customers would be returned.
SELECT COUNT(*)
FROM customers;
SUM
SUM returns the total sum of a numeric column.
SELECT SUM(age)
FROM customers;
AVG
AVG returns the average value of a numeric column.
SELECT AVG(age)
FROM customers;
MIN
MIN returns the minimum value of a numeric column.
SELECT MIN(age)
FROM customers;
MAX
MAX returns the maximum value of a numeric column.
SELECT MAX(age)
FROM customers;
GROUP BY
The GROUP BY statement groups rows with the same values into summary rows. The statement
is often used with aggregate functions. For example, the code below will display the average
age for each name that appears in our customers table.
The below example would return the number of rows for each name, but only for names with
more than 2 records.
SELECT name
FROM customers
ORDER BY age;
DESC
DESC will return the results in descending order.
SELECT name
FROM customers
ORDER BY age DESC;
JOINS (INNER, LEFT, RIGHT, FULL)
A JOIN clause is used to combine rows from two or more tables. The four types of JOIN are
INNER, LEFT, RIGHT and FULL.
INNER JOIN
INNER JOIN selects records that have matching values in both tables.
SELECT name
FROM customers
INNER JOIN orders
ON customers.customer_id = orders.customer_id;
LEFT JOIN
LEFT JOIN selects records from the left table that match records in the right table. In the below
example the left table is customers.
SELECT name
FROM customers
LEFT JOIN orders
ON customers.customer_id = orders.customer_id;
RIGHT JOIN
RIGHT JOIN selects records from the right table that match records in the left table. In the
below example the right table is orders.
SELECT name
FROM customers
RIGHT JOIN orders
ON customers.customer_id = orders.customer_id;
FULL JOIN
FULL JOIN selects records that have a match in the left or right table. Think of it as the “OR”
JOIN compared with the “AND” JOIN (INNER JOIN).
SELECT name
FROM customers
FULL OUTER JOIN orders
ON customers.customer_id = orders.customer_id;
GRANT
GRANT gives a particular user access to database objects such as tables, views or the database
itself. The below example would give SELECT and UPDATE access on the customers table to a
user named “usr_bob”.
SAVEPOINT SAVEPOINT_NAME;
COMMIT
COMMIT is for saving every transaction to the database. A COMMIT statement will release any
existing savepoints that may be in use and once the statement is issued, you cannot roll back
the transaction.
ROLLBACK TO SAVEPOINT_NAME;
UNION
UNION combines multiple result-sets using two or more SELECT statements and eliminates
duplicate rows.
UNION ALL
UNION ALL combines multiple result-sets using two or more SELECT statements and keeps
duplicate rows.
1. Data Definition Language (DDL): DDL is used to define and manage the structure of a
database.
DDL Commands:
2. Data Manipulation Language (DML): DML is used for manipulating data within the database.
DML Commands:
3. Data Query Language (DQL): DQL is a subset of DML focused on retrieving data.
DQL Commands:
4. Data Control Language (DCL): DCL is used to control access to data within the database.
Commands like GRANT and REVOKE are used to assign and revoke permissions, determining
who can access or modify certain data.
DCL Commands:
5. Transaction Control Language (TCL): TCL is used to manage transactions within a database.
TCL Commands:
1. SELECT Statement
The SELECT statement in SQL is used to retrieve data from one or more tables in a database. It is
a fundamental and powerful command that allows you to query and display information stored
in a relational database.
Example:
1. SELECT * FROM employees; (* gives you the complete data of the table).
2. SELECT first_name, last_name FROM employees; (gives you all the data of the first name and
last name from the table employees).
2. SELECT DISTINCT
The SELECT DISTINCT statement is used to retrieve unique values from a specific column in a table.
It eliminates duplicate rows and returns only distinct values.
Syntax: SELECT DISTINCT column1, column2, ... FROM table_name WHERE condition;
Example:
1. SELECT DISTINCT first_name, last_name FROM students; (gives you the unique values of the
first name and last name from the table students).
3. WHERE Clause
The WHERE clause in SQL is used to filter the rows returned by a SELECT statement based on a
specified condition. It allows you to retrieve only the rows that meet certain criteria.
Example:
1. SELECT * FROM students WHERE age > 20; (gives you the data of students whose age is
above 20).
4. ORDER BY Clause
The ORDER BY clause in SQL is used to sort the result set of a SELECT statement based on one or
more columns. It allows you to specify the order in which the rows should be displayed, either
in ascending (default) or descending order.
Syntax: SELECT column1, column2, ... FROM table_name ORDER BY column1 [ASC | DESC],
column2 [ASC | DESC], ...;
Example:
1. SELECT * FROM students ORDER BY age; (organizes the data of the table by age in
ascending/descending order).
5. LIMIT and OFFSET
The LIMIT and OFFSET clauses in SQL are used to control the number of rows returned by
a SELECT statement, allowing you to retrieve a specific subset of the result set. These clauses are
often used together to implement pagination or to fetch a limited number of rows starting from
a certain position.
Example:
1. SELECT * FROM students LIMIT 2 OFFSET 2; (the LIMIT 2 restricts the result set to two rows,
and the OFFSET 2 skips the first two rows.)
6. INSERT Statement
The INSERT statement in SQL is used to insert new records (rows) into a table. It adds data to an
existing table by specifying the values to be inserted into each column.
Syntax: INSERT INTO table_name (column1, column2, …) VALUES (value1, value2, …);
Example:
1. INSERT INTO students (student_id, first_name, last_name, age, grade) VALUES (7, ‘Alice’,
‘Johnson’, 21, ‘B’); (adds a new student to the students’ table with the required details).
7. UPDATE Statement
The UPDATE statement in SQL is used to modify existing records (rows) in a table. It allows you to
change the values of one or more columns in the specified rows based on a specified condition.
Syntax: UPDATE table_name SET column1 = value1, column2 = value2, … WHERE condition;
Example:
1. UPDATE students SET age = 20, grade = ‘B’ WHERE student_id = 3; (update the students’
table with the student’s age as 20, grade as B where id is 3).
8. DELETE Statement
The DELETE statement in SQL is used to remove records (rows) from a table based on a specified
condition. It permanently removes data from the table.
Example:
1. DELETE FROM students WHERE student_id = 4; (deletes the entire data of student whose id is
4 from the table students).
9. COUNT() Function
The COUNT function in SQL is used to count the number of rows in a table or the number of rows
that satisfy a particular condition in a specified column.
Example:
1. SELECT COUNT(*) FROM students; (displays the count of total number of students from the table
students).
The SUM function in SQL is used to calculate the sum of values in a numeric column, typically
within a specified group or for all rows in a table.
Example:
1. SELECT SUM(amount) FROM sales; (displays the total amount from the sales table).
The GROUP BY clause in SQL is used to group rows that have the same values in specified
columns into summary rows, like “total sales per product” or “average score per student.” It’s
often used with aggregate functions like SUM, COUNT, AVG, etc.
1. SELECT product, SUM(amount) as total_sales FROM sales GROUP BY product; (groups the
data by the “product” column and displays the result.)
The HAVING clause in SQL is used in conjunction with the GROUP BY clause to filter the results of a
grouped query. While the WHERE clause filters rows before grouping, the HAVING clause filters
groups after the grouping has taken place. It is commonly used with aggregate functions
like SUM, COUNT, AVG, etc.
Example:
In SQL, the JOIN statement is used to combine rows from two or more tables based on a related
column between them.
There are different types of joins, including INNER JOIN, LEFT JOIN (or LEFT OUTER JOIN), RIGHT
JOIN (or RIGHT OUTER JOIN), and FULL JOIN (or FULL OUTER JOIN).
Example:
14. Subqueries
A subquery, also known as a nested query or inner query, is a query embedded within another
query. It can be used to retrieve data that will be used in the main query as a condition to
further restrict the data to be retrieved. Subqueries can be used in various parts of a SQL
statement, including the SELECT, FROM, WHERE, and HAVING clauses.
Example:
1. SELECT first_name FROM students WHERE student_id IN (SELECT student_id FROM grades
WHERE grade = ‘A’); (it finds the student_id values from the “grades” table where the grade is ‘A’.
The main query then selects the first_name of students whose student_id is in the result set of the
subquery.)
15. Views
A view is a virtual table based on the result of a SELECT query. It allows you to simplify complex
queries, encapsulate logic, and present a subset or transformation of the data to users without
exposing the underlying table structure.
Views are often used to enhance data security by restricting direct access to certain tables and
providing a controlled way to access specific data.
Syntax: CREATE VIEW view_name AS SELECT column1, column2, … FROM table_name WHERE
condition;
Example:
“high_performers” view is created, which includes the columns student_id, first_name, last_name,
and grade from the “students” table. Only the rows where the grade is ‘A’ are included in the
view.
SQL Commands
Codecademy Team
Share
Glossary of commonly used SQL commands.
Background
SQL, Structured Query Language, is a programming language designed to manage data stored in
relational databases. SQL operates through simple, declarative statements. This keeps data
accurate and secure, and it helps maintain the integrity of databases, regardless of size.
Commands
ALTER TABLE
AND
SELECT column_name(s)
FROM table_name
WHERE column_1 = value_1
AND column_2 = value_2;
AND is an operator that combines two conditions. Both conditions must be true for the row to
be included in the result set.
AS
AS is a keyword in SQL that allows you to rename a column or table using an alias.
AVG()
SELECT AVG(column_name)
FROM table_name;
AVG() is an aggregate function that returns the average value for a numeric column.
BETWEEN
SELECT column_name(s)
FROM table_name
WHERE column_name BETWEEN value_1 AND value_2;
The BETWEEN operator is used to filter the result set within a certain range. The values can be
numbers, text or dates.
CASE
SELECT column_name,
CASE
WHEN condition THEN 'Result_1'
WHEN condition THEN 'Result_2'
ELSE 'Result_3'
END
FROM table_name;
CASE statements are used to create different outputs (usually in the SELECT statement). It is SQL’s
way of handling if-then logic.
COUNT()
SELECT COUNT(column_name)
FROM table_name;
COUNT() is a function that takes the name of a column as an argument and counts the number of
rows where the column is not NULL.
CREATE TABLE
CREATE TABLE creates a new table in the database. It allows you to specify the name of the table
and the name of each column in the table.
DELETE
GROUP BY
GROUP BY is a clause in SQL that is only used with aggregate functions. It is used in collaboration
with the SELECT statement to arrange identical data into groups.
HAVING
HAVING was added to SQL because the WHERE keyword could not be used with aggregate
functions.
INNER JOIN
SELECT column_name(s)
FROM table_1
JOIN table_2
ON table_1.column_name = table_2.column_name;
An inner join will combine rows from different tables if the join condition is true.
INSERT
SELECT column_name(s)
FROM table_name
WHERE column_name IS NULL;
IS NULL and IS NOT NULL are operators used with the WHERE clause to test for empty values.
LIKE
SELECT column_name(s)
FROM table_name
WHERE column_name LIKE pattern;
LIKE is a special operator used with the WHERE clause to search for a specific pattern in a column.
LIMIT
SELECT column_name(s)
FROM table_name
LIMIT number;
LIMIT is a clause that lets you specify the maximum number of rows the result set will have.
MAX()
SELECT MAX(column_name)
FROM table_name;
MAX() is a function that takes the name of a column as an argument and returns the largest
value in that column.
MIN()
SELECT MIN(column_name)
FROM table_name;
MIN() is a function that takes the name of a column as an argument and returns the smallest
value in that column.
OR
SELECT column_name
FROM table_name
WHERE column_name = value_1
OR column_name = value_2;
OR is an operator that filters the result set to only include rows where either condition is true.
ORDER BY
SELECT column_name
FROM table_name
ORDER BY column_name ASC | DESC;
ORDER BY is a clause that indicates you want to sort the result set by a particular column either
alphabetically or numerically.
OUTER JOIN
SELECT column_name(s)
FROM table_1
LEFT JOIN table_2
ON table_1.column_name = table_2.column_name;
An outer join will combine rows from different tables even if the join condition is not met. Every
row in the left table is returned in the result set, and if the join condition is not met,
then NULL values are used to fill in the columns from the right table.
ROUND()
ROUND() is a function that takes a column name and an integer as arguments. It rounds the
values in the column to the number of decimal places specified by the integer.
SELECT
SELECT column_name
FROM table_name;
SELECT statements are used to fetch data from a database. Every query will begin with SELECT.
SELECT DISTINCT
SELECT DISTINCT specifies that the statement is going to be a query that returns unique values in
the specified column(s).
SUM
SELECT SUM(column_name)
FROM table_name;
SUM() is a function that takes the name of a column as an argument and returns the sum of all
the values in that column.
UPDATE
UPDATE table_name
SET some_column = some_value
WHERE some_column = some_value;
WHERE
SELECT column_name(s)
FROM table_name
WHERE column_name operator value;
WHERE is a clause that indicates you want to filter the result set to include only rows where the
following condition is true.
WITH
WITH temporary_name AS (
SELECT *
FROM table_name)
SELECT *
FROM temporary_name
WHERE column_name operator value;
WITH clause lets you store the result of a query in a temporary table using an alias. You can also
define multiple temporary tables using a comma and with one instance of the WITH keyword.
The WITH clause is also known as common table expression (CTE) and subquery factoring.
In the era where data is being generated in humongous amounts, there is a constant need
to handle data in databases. Relational databases are one of the most popular databases, and
SQL is the basis of relational databases. Therefore SQL skills are indispensable in most of the
job roles. In this article on SQL Commands, I will discuss the top commands and statements that
you need to understand in SQL.
Comments in SQL
There are two ways in which you can comment in SQL, i.e. either the Single-Line Comments or
the Multi-Line Comments.
Single-Line Comments
The single line comment starts with two hyphens (–). So, any text mentioned after (–), till the
end of a single line will be ignored by the compiler.
Example:
1 --Select all:
Multi-Line Comments
The Multi-line comments start with /* and end with */. So, any text mentioned between /* and
*/ will be ignored by the compiler.
Example:
This section of the article will give you an insight into the commands through which you can
define your database. The commands are as follows:
CREATE
DROP
TRUNCATE
ALTER
BACKUP DATABASE
CREATE
This statement is used to create a table or a database.
Syntax
CREATE DATABASE DatabaseName;
Example
1 CREATE DATABASE Employee;
Syntax
CREATE TABLE TableName (
Column1 datatype,
Column2 datatype,
Column3 datatype,
....
ColumnN datatype
);
Example
1 CREATE TABLE Employee_Info
2 (
3 EmployeeID int,
4 EmployeeName varchar(255),
6 PhoneNumber int,
7 Address varchar(255),
8 City varchar(255),
9 Country varchar(255)
);
10
You can also create a table using another table. Refer the below sytax and example:
Syntax
CREATE TABLE NewTableName AS
SELECT Column1, column2,..., ColumnN
FROM ExistingTableName
WHERE ....;
Example
1 CREATE TABLE ExampleTable AS
3 FROM Employee_Info;
DROP
This statement is used to drop an existing database. When you use this statement, complete
information present in the database will be lost.
Syntax
DROP DATABASE DatabaseName;
Example
1 DROP DATABASE Employee;
This statement is used to drop an existing table. When you use this statement, complete
information present in the table will be lost.
Syntax
DROP TABLE TableName;
Example
1 DROP Table Employee_Info;
TRUNCATE
This command is used to delete the information present in the table but does not delete the
table. So, once you use this command, your information will be lost, but not the table.
Syntax
TRUNCATE TABLE TableName;
Example
1 TRUNCATE Table Employee_Info;
ALTER
This command is used to delete, modify or add constraints or columns in an existing table.
You can use the ALTER TABLE statement with ADD/DROP Column command according to your
need. If you wish to add a column, then you will use the ADD command, and if you wish to
delete a column, then you will use the DROP COLUMN command.
Syntax
ALTER TABLE TableName
ADD ColumnName Datatype;
7
8 ALTER TABLE Employee_Info
Discover the secrets of efficient data management through our SQL Online Course.