0% found this document useful (0 votes)
2 views

DATABASE-COMMANDS-2

SQL, or Structured Query Language, is used for managing databases, allowing users to create, insert, update, and retrieve data. SQL commands are categorized into five types: DDL (Data Definition Language), DML (Data Manipulation Language), DQL (Data Query Language), DCL (Data Control Language), and TCL (Transaction Control Language). The document also covers SQL data types, the syntax for INSERT, UPDATE, and DELETE statements, and the use of the WHERE clause for filtering records.

Uploaded by

sangaara260
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

DATABASE-COMMANDS-2

SQL, or Structured Query Language, is used for managing databases, allowing users to create, insert, update, and retrieve data. SQL commands are categorized into five types: DDL (Data Definition Language), DML (Data Manipulation Language), DQL (Data Query Language), DCL (Data Control Language), and TCL (Transaction Control Language). The document also covers SQL data types, the syntax for INSERT, UPDATE, and DELETE statements, and the use of the WHERE clause for filtering records.

Uploaded by

sangaara260
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

What is SQL?

SQL stands for Structured Query Language. It is developed to manage database. SQL enable us to create
database, insert data, and update data as per requirement and retrieve data.

What are the Categories of SQL Commands?


All SQL commands are categorized in five types:
• Data Definition Language (DDL)
• Data Manipulation language (DML)
• Data Query Language (DQL)
• Data Control Language (DCL)
• Transaction Control Language (TCL)

Data Definition Language (DDL)


 DDL is used to define structure and constraints of data
 It also defines key constraints, relationships between tables, and other data validation constraints.
 Example of DDL commands are:
 CREATE: This command is used to create the database or its objects (like table, index, function, views,
store procedure, and triggers).
 DROP: This command is used to delete objects from the database.
 ALTER: This is used to alter the structure of the database.
 TRUNCATE: This is used to remove all records from a table, including all spaces allocated for the records
are removed.
 COMMENT: This is used to add comments to the data dictionary.
 RENAME: This is used to rename an object existing in the database.

Data Manipulation language (DML)


 DML is used to insert, modify and delete data in a database.
 Example of DML commands are:
 INSERT: It is used to insert data into a table.
 UPDATE: It is used to update existing data within a table.
 DELETE: It is used to delete records from a database table.

Data Query language (DQL)


 DQL is used to retrieve or extract data from a database.
 Example of DQL commands are:
 SELECT: It is used to retrieve data from the database.

Data Control Language (DCL)


 DCL mainly deal with the rights, permissions, and other controls of the database system.
 Example of DCL commands are:
 GRANT: This command gives users access privileges to the database.
 REVOKE: This command withdraws the user’s access privileges given by using the GRANT command.

Transaction Control Language (TCL)


 TCL commands are used to control the execution of a transaction.
 Example of TCL commands are:
 COMMIT: Commits a Transaction.
 ROLLBACK: Rollbacks a transaction in case of any error occurs.
 SAVEPOINT: Sets a save point within a transaction. So, it can Rollback to this position when a transaction
fails at later stage
What are the Datatypes used in MySQL?
Given table shows data types commonly used in SQL.
Data type Use Example

Fixed length character string. n denotes max no of characters.


Char(n) It will always occupy fixed amount of space in database as per Char(5) : Arman, Rajni
the specified value of n.

Variable length character string. n denotes max no of


Varchar(n) Varchar(10): ram, arjun
characters.

Date Date as YYYY-MM-DD ‘2022-07-23’

Integer(n) Specify a medium integer value. Signed range is from


Or -2147483648 to 2147483647. The n parameter specifies the 45, 4539
Int(n) maximum display width (which is 255)

Specify a large integer value. Signed range is from 98552325655648562,


Bigint(n)
-9223372036854775808 to 9223372036854775807. 9855223252

An exact fixed-point number. M denotes no of digits stored for


Decimal(3,2): 34.89
values and d denotes no of decimal points. Suppose for
Decimal(M,d) Decimal(4): 2582
datatype decimal(5,2), it allowed maximum (5-2) = 3 digits
Decimal(5,3): 25621.250
before decimal and 2 digits after decimal.

A floating point number. The FLOAT data type stores


approximate values. MySQL uses the p value to determine
whether to use FLOAT or DOUBLE for the resulting data type.
Float(p) 456.23
If p is from 0 to 24, the data type becomes FLOAT(). If p is from
25 to 53, the data type becomes DOUBLE(). Default is Zero (0).
In MySQL, the default precision is 6 digits for float.

SQL INSERT STATEMENT


 INSERT statement is used to insert new records in a table.
 It is possible to write the INSERT statement in two ways.
By specify both the column INSERT INTO <table_name> (<column1>, <column2>, <column3>, ...)
names and the values to be VALUES (<value1>, <value2>, <value3>, ...);
inserted:
If you are adding values for INSERT INTO <table_name>
all the columns of the table VALUES (<value1>, <value2>, <value3>, ...);

INSERT DATA ONLY IN SPECIFIED COLUMNS ONLY


SYNTAX INSERT INTO <table_name> (<column_x>, <column_y>, <column_z>, ...)
VALUES (<value_for_col_x>, < value_for_col_y>, < value_for_col_z>, ...);

(NOTE: here column_x, column_y and column_z are those columns/fields name
where data/values are required to enter.)

EXAMPLE INSERT INTO Customers (C_ID, Name, City, Country)


VALUES (123, 'Sagarika', 'Asansol', 'India');
INSERT MULTIPLE ROWS AT ONCE
SYNTAX INSERT INTO <table_name> (<column1>, <column2>, <column3>, ...)
VALUES
(For N numbers of Rows) (<Row1_value1>, < Row1_value2>, < Row1_value3>, ...),
(<Row1_value1>, < Row1_value2>, < Row1_value3>, ...),
(<Row2_value1>, < Row2_value2>, < Row2_value3>, ...),
….
(<RowN_value1>, < RowN_value2>, < RowN_value3>, ...);
EXAMPLE INSERT INTO Customers (C_ID, Name, Address, City, PIN, Country, Contact)
VALUES
(123, 'Sagarika', 'Sristinagar', 'Asansol', 713305, 'India', 9832198321);
(124, 'Debajyoti', 'Sristinagar', 'Asansol', 713305, 'India', 9855555555);
(125, 'Jyotsana', 'KSTP, South Dhadka', 'Asansol', 713302, 'India',
8554585885);

SQL WHERE Clause


 The WHERE clause is used to filter records.
 It is used to extract only those records that fulfil a specified condition.
 The WHERE clause is not only used in SELECT statements, it is also used in UPDATE, DELETE, etc.!

SYNTAX SELECT <column1>, <column2>, ...


(use with Select Statement) FROM <table_name>
WHERE <condition>;
SELECT * FROM Customers
EXAMPLE
WHERE PIN=713305;

THE FOLLOWING OPERATORS CAN BE USED IN THE WHERE CLAUSE:


OPERATOR DESCRIPTION EXAMPLE
= Equal SELECT * FROM Pro WHERE Price = 2050;
> Greater than SELECT * FROM Pro WHERE Price > 2050;
< Less than SELECT * FROM Pro WHERE Price < 2050;
>= Greater than or equal SELECT * FROM Pro WHERE Price >= 2050;
<= Less than or equal SELECT * FROM Pro WHERE Price <= 2050;
Not equal. Note: In some SELECT * FROM Pro WHERE Price <> 2050;
<> versions of SQL this operator
may be written as != SELECT * FROM Pro WHERE Price != 2050;
BETWEEN Between a certain range SELECT * FROM Pro WHERE Price BETWEEN 30 AND 2050;
Search for a pattern, you can SELECT * FROM Pro WHERE Price Like 2050;
LIKE also use wildcard character
like % and _ SELECT * FROM Pro WHERE ProName Like '%ita';
To specify multiple possible SELECT * FROM Pro
IN
values for a column WHERE ProName IN ('Printer','Monitor');

SQL UPDATE Statement


The UPDATE statement is used to modify the existing records in a table.
UPDATE <table_name>
SYNTAX
SET <column1> = <value1>, <column2> = <value2>, ...
WHERE <condition>;
UPDATE Customers
EXAMPLE SET C_Name = 'Suraj Kumar', C_City= 'Asansol'
WHERE C_ID = 2512;
UPDATE Multiple Records
It is the WHERE clause that determines how many records will be updated.
UPDATE Customers
SET C_Discount='20'
WHERE C_City='Asansol';
EXAMPLE
(Note: The following SQL statement will update the C_Discount to "20" for all
records in Customers table where C_City is "Asansol")

IMPORTANT NOTE: Be careful when updating records. If you omit the WHERE clause, ALL records will be
updated!
UPDATE Customers
SET C_Discount='20' ;
EXAMPLE
(Note: The following SQL statement will update the C_Discount to "20" for all
records in Customers table)

SQL DELETE Statement


The DELETE statement is used to delete existing records in a table.
SYNTAX DELETE FROM <table_name>
WHERE <condition>;

DELETE Customers
EXAMPLE
WHERE C_ID = 2512;
DELETE Multiple Records
It is the WHERE clause that determines how many records will be updated.
DELETE Customers
WHERE C_City='Asansol';
EXAMPLE
(Note: The following SQL statement will delete all records from the Customers
table where C_City is "Asansol")

IMPORTANT NOTE: Be careful when deleting records. If you omit the WHERE clause, ALL records will be
deleted!
DELETE Customers;
EXAMPLE
(Note: The following SQL statement will delete all records from the Customers
table)

Delete All Records


It is possible to delete all rows in a table without deleting the table.
SYNTAX DELETE FROM <table_name>;
EXAMPLE DELETE FROM Customers;

You might also like