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

SQL Datatype Is Used To Define The Values That A Column Can Contain. Every Column Is Required To Have A Name and Data Type in The Database Table

SQL stands for Structured Query Language and is used to manage data in relational database management systems. It enables users to create, read, update, and delete data from relational databases. SQL commands fall into several categories including DDL for defining data structures, DML for manipulating data, DCL for controlling access privileges, TCL for transaction processing, and DQL for querying data. Common SQL datatypes include numeric, date/time, character, and binary types to define column attributes in database tables.

Uploaded by

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

SQL Datatype Is Used To Define The Values That A Column Can Contain. Every Column Is Required To Have A Name and Data Type in The Database Table

SQL stands for Structured Query Language and is used to manage data in relational database management systems. It enables users to create, read, update, and delete data from relational databases. SQL commands fall into several categories including DDL for defining data structures, DML for manipulating data, DCL for controlling access privileges, TCL for transaction processing, and DQL for querying data. Common SQL datatypes include numeric, date/time, character, and binary types to define column attributes in database tables.

Uploaded by

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

SQL

 SQL stands for Structured Query Language. It is used for storing and managing data in relational
database management system (RDMS).
 It is a standard language for Relational Database System. It enables a user to create, read, update
and delete relational databases and tables.
 All the RDBMS like MySQL, Informix, Oracle, MS Access and SQL Server use SQL as their
standard database language.
 SQL allows users to query the database in a number of ways, using English-like statements.

Rules:
 SQL follows the following rules:
 Structure query language is not case sensitive. Generally, keywords of SQL are written in uppercase.

 Statements of SQL are dependent on text lines. We can use a single SQL statement on one or multiple
text line.

 Using the SQL statements, you can perform most of the actions in a database.

 SQL depends on tuple relational calculus and relational algebra.

SQL process:

 When an SQL command is executing for any RDBMS, then the system figure out the best way to
carry out the request and the SQL engine determines that how to interpret the task.
 In the process, various components are included. These components can be optimization Engine,
Query engine, Query dispatcher, classic, etc.
 All the non-SQL queries are handled by the classic query engine, but SQL query engine won't
handle logical files.

SQL Datatype
 SQL Datatype is used to define the values that a column can contain.
 Every column is required to have a name and data type in the database table.

Datatype of SQL:
1.

Binary Datatypes
There are Three types of binary Datatypes which are given below:

Data Type Description

binary It has a maximum length of 8000 bytes. It contains fixed-length binary data.

varbinary It has a maximum length of 8000 bytes. It contains variable-length binary data.

image It has a maximum length of 2,147,483,647 bytes. It contains variable-length binary data.

2. Approximate Numeric Data type:


The subtypes are given below:
Data type From To Description

float -1.79E + 308 1.79E + 308 It is used to specify a floating-point value e.g. 6.2, 2.9 etc.

real -3.40e + 38 3.40E + 38 It specifies a single precision floating point number

3. Exact Numeric Datatype

The subtypes are given below:

Data type Description

int It is used to specify an integer value.

smallint It is used to specify small integer value.

bit It has the number of bits to store.

decimal It specifies a numeric value that can have a decimal number.

numeric It is used to specify a numeric value.

4. Character String Datatype


The subtypes are given below:
Data Description
type
char It has a maximum length of 8000 characters. It contains Fixed-length non-unicode characters.

varchar It has a maximum length of 8000 characters. It contains variable-length non-unicode characters.

text It has a maximum length of 2,147,483,647 characters. It contains variable-length non-unicode


characters.

5. Date and time Datatypes


The subtypes are given below:
Datatype Description

date It is used to store the year, month, and days value.

time It is used to store the hour, minute, and second values.

timestamp It stores the year, month, day, hour, minute, and the second value.

SQL Commands
 SQL commands are instructions. It is used to communicate with the database. It is 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, modify the
table, set permission for users.

Types of SQL Commands


There are five types of SQL commands: DDL, DML, DCL, TCL, and DQL.

1. Data Definition Language (DDL)

 DDL changes the structure of the table like creating a table, deleting a table, altering a table, etc.
 All the command of DDL are auto-committed that means it permanently save all the changes in the
database.
Here are some
commands
that come under
DDL:

CREATE

ALTER

DROP

TRUNCATE

a. CREATE It is used to create a new table in the database.

Syntax:

1. CREATE TABLE TABLE_NAME (COLUMN_NAME DATATYPES[,....]);  

Example:
1. CREATE TABLE EMPLOYEE(Name VARCHAR2(20), Email VARCHAR2(100), DOB DA
TE);  

b. DROP: It is used to delete both the structure and record stored in the table.

Syntax

1. DROP TABLE ;  

Example

1. DROP TABLE EMPLOYEE;  

c. ALTER: It is used to alter the structure of the database. This change could be either to modify the
characteristics of an existing attribute or probably to add a new attribute.

Syntax:

To add a new column in the table

1. ALTER TABLE table_name ADD column_name COLUMN-definition;    

To modify existing column in the table:

1. ALTER TABLE MODIFY(COLUMN DEFINITION....);  

EXAMPLE

1. ALTER TABLE STU_DETAILS ADD(ADDRESS VARCHAR2(20));  

2. ALTER TABLE STU_DETAILS MODIFY (NAME VARCHAR2(20));  

d. TRUNCATE: It is used to delete all the rows from the table and free the space containing the table.

Syntax:

1. TRUNCATE TABLE table_name;  

Example:

1. TRUNCATE TABLE EMPLOYEE;  

2. Data Manipulation Language


 DML commands are used to modify the database. It is responsible for all form of changes
in the database.
 The command of DML is not auto-committed that means it can't permanently save all the
changes in the database. They can be rollback.
 Here are some commands that come under DML:

INSERT

UPDATE

DELETE
a. INSERT: The INSERT statement is a SQL query. It is used to insert data into the row of a table.

Syntax:

1. INSERT INTO TABLE_NAME    

2. (col1, col2, col3,.... col N)  

3. VALUES (value1, value2, value3, .... valueN);  

Or

1. INSERT INTO TABLE_NAME    

2. VALUES (value1, value2, value3, .... valueN);    

For example:

1. INSERT INTO RECORDS (Author, Subject) VALUES ("Sonoo", "DBMS");  

b. UPDATE: This command is used to update or modify the value of a column in the table.

Syntax:

1. UPDATE table_name SET [column_name1= value1,...column_nameN = valueN] [WHERE C
ONDITION]   

For example:

1. UPDATE students    

2. SET User_Name = 'Sonoo'    

3. WHERE Student_Id = '3'  

c. DELETE: It is used to remove one or more row from a table.

Syntax:

1. DELETE FROM table_name [WHERE condition];  

For example:

1. DELETE FROM RECORDS

2. WHERE Author="Sonoo";  

3. Data Control Language

DCL commands are used to grant and take back authority from any database user.

Here are some commands that come under DCL:

 Grant
 Revoke
a. Grant: It is used to give user access privileges to a database.

Example

1. GRANT SELECT, UPDATE ON MY_TABLE TO SOME_USER, ANOTHER_USER;  

b. Revoke: It is used to take back permissions from the user.

Example

1. REVOKE SELECT, UPDATE ON MY_TABLE FROM USER1, USER2;  

4. Transaction Control Language

TCL commands can only use with DML commands like INSERT, DELETE and UPDATE only.

These operations are automatically committed in the database that's why they cannot be used while
creating tables or dropping them.

Here are some commands that come under TCL:

 COMMIT
 ROLLBACK
 SAVEPOINT
a. Commit: Commit command is used to save all the transactions to the database.
Syntax:

1. COMMIT;  

Example:

1. DELETE FROM CUSTOMERS  

2. WHERE AGE = 25;  

3. COMMIT;  

b. Rollback: Rollback command is used to undo transactions that have not already been saved to the
database.

Syntax:

1. ROLLBACK;  

Example:

1. DELETE FROM CUSTOMERS  

2. WHERE AGE = 25;  

3. ROLLBACK;  

c. SAVEPOINT: It is used to roll the transaction back to a certain point without rolling back the entire
transaction.
Syntax:

1. SAVEPOINT SAVEPOINT_NAME;  

5. Data Query Language

DQL is used to fetch the data from the database.

It uses only one command:

o SELECT

a. SELECT: This is the same as the projection operation of relational algebra. It is used to select the
attribute based on the condition described by WHERE clause.

Syntax:

1. SELECT expressions    

2. FROM TABLES    

3. WHERE conditions;  

For example:

1. SELECT emp_name  

2. FROM employee  

3. WHERE age > 20;  

SQL Table

 SQL Table is a collection of data which is organized in terms of rows and columns. In DBMS, the
table is known as relation and row as a tuple.
 Table is a simple form of data storage. A table is also considered as a convenient representation of
relations.

Let's see an example of the EMPLOYEE table:

In the above table, "EMPLOYEE" is the table name, "EMP_ID", "EMP_NAME", "CITY", "PHONE_NO" are
the column names. The combination of data of multiple columns forms a row, e.g., 1, "Kishen", "Indore" and
7289201223 are the data of one row.

Operation on Table

1. Create table

2. Drop table
3. Delete table

4. Rename table

SQL Create Table


SQL create table is used to create a table in the database. To define the table, you should define the name of the
table and also define its columns and column's data type.
Syntax

1. create table "table_name"    

2. ("column1" "data type",    

3. "column2" "data type",    

4. "column3" "data type",    

5. ...    

6. "columnN" "data type");   

Example

1. SQL> CREATE TABLE EMPLOYEE (    

2. EMP_ID INT                           NOT NULL,    

3. EMP_NAME VARCHAR (25) NOT NULL,    

4. PHONE_NO INT                         NOT NULL,    

5. ADDRESS CHAR (30),    

6. PRIMARY KEY (ID)    

7. );    

If you create the table successfully, you can verify the table by looking at the message by the SQL server. Else
you can use DESC command as follows:

SQL> DESC EMPLOYEE;

Field Type Null Key Default Extra

EMP_ID int(11) NO PRI NULL

EMP_NAME varchar(25) NO NULL

PHONE_NO NO int(11) NULL

ADDRESS YES NULL char(30)

o 4 rows in set (0.35 sec)

Now you have an EMPLOYEE table in the database, and you can use the stored information related to the
employees.

Drop table
A SQL drop table is used to delete a table definition and all the data from a table. When this command is
executed, all the information available in the table is lost forever, so you have to very careful while using this
command.

Syntax

1. DROP TABLE "table_name";    

Firstly, you need to verify the EMPLOYEE table using the following command:

1. SQL> DESC EMPLOYEE;    

Field Type Null Key Default Extra

EMP_ID int(11) NO PRI NULL

EMP_NAME varchar(25) NO NULL

PHONE_NO NO int(11) NULL

ADDRESS YES NULL char(30)

o 4 rows in set (0.35 sec)

This table shows that EMPLOYEE table is available in the database, so we can drop it as follows:

1. SQL>DROP TABLE EMPLOYEE;    

Now, we can check whether the table exists or not using the following command:

1. Query OK, 0 rows affected (0.01 sec)    

As this shows that the table is dropped, so it doesn't display it.

SQL DELETE table

In SQL, DELETE statement is used to delete rows from a table. We can use WHERE condition to delete a
specific row from a table. If you want to delete all the records from the table, then you don't need to use the
WHERE clause.

Syntax

1. DELETE FROM table_name WHERE condition;    

Example

Suppose, the EMPLOYEE table having the following records:

EMP_ID EMP_NAME CITY PHONE_NO SALARY

1 Gourav Indore 9737287378 150000

2 Ritika Bhopal 9262738271 200000

3 Diksha Dewas 7353662627 100000

4 Yash Ujjain 9232673822 600000


5 Rahul Sanwer 9367238263 350000

6 Vandana Khandwa 7253847382 260000

The following query will DELETE an employee whose ID is 2.

1. SQL> DELETE FROM EMPLOYEE  

2. WHERE EMP_ID = 3;  

Now, the EMPLOYEE table would have the following records.

EMP_ID EMP_NAME CITY PHONE_NO SALARY

1 Kristen Chicago 9737287378 150000

2 Russell Austin 9262738271 200000

4 Angelina Denver 9232673822 600000

5 Robert Washington 9367238263 350000

6 Christian Los angels 7253847382 260000

If you don't specify the WHERE condition, it will remove all the rows from the table.

1. DELETE FROM EMPLOYEE;    

Now, the EMPLOYEE table would not have any records.

SQL SELECT Statement

In SQL, the SELECT statement is used to query or retrieve data from a table in the database. The returns data is
stored in a table, and the result table is known as result-set.

Syntax

1. SELECT column1, column2, ...  

2. FROM table_name;  

Here, the expression is the field name of the table that you want to select data from.

Use the following syntax to select all the fields available in the table:

1. SELECT  *  FROM table_name;  

Example:

EMPLOYEE

EMP_ID EMP_NAME CITY PHONE_NO SALARY

1 ABC Indore 9737287378 150000

2 ABCX Bhopal 9262738271 200000

3 ABCY Dewas 9232673822 600000


4 ABCZ Ujjain 9367238263 350000

5 ABCAA Khandwa 7253847382 260000

To fetch the EMP_ID of all the employees, use the following query:

1. SELECT EMP_ID FROM EMPLOYEE;  

Output

EMP_ID

To fetch the EMP_NAME and SALARY, use the following query:

1. SELECT EMP_NAME, SALARY FROM EMPLOYEE;  

EMP_NAME SALARY

ABC 150000

ABCX 200000

ABCY 600000

ABCZ 350000

ABCAA 260000

To fetch all the fields from the EMPLOYEE table, use the following query:

1. SELECT * FROM EMPLOYEE  

Output

EMP_ID EMP_NAME CITY PHONE_NO SALARY


1 ABC Indore 9737287378 150000

2 ABCX Bhopal 9262738271 200000

3 ABCY Dewas 9232673822 600000

4 ABCZ Ujjain 9367238263 350000

5 ABCAA Khandwa 7253847382 260000

SQL INSERT Statement


The SQL INSERT statement is used to insert a single or multiple data in a table. In SQL, You can insert the
data in two ways:
1. Without specifying column name

2. By specifying column name

Sample Table

EMPLOYEE

EMP_ID EMP_NAME CITY SALARY AGE

1 ABC Indore 200000 30

2 ABCX Bhopal 300000 26

3 ABCY Dewas 100000 42

4 ABCZ Ujjain 500000 29

5 ABCAA Khandwa 200000 36

1. Without specifying column name

If you want to specify all column values, you can specify or ignore the column values.

Syntax

1. INSERT INTO TABLE_NAME    

2. VALUES (value1, value2, value 3, .... Value N);    

Query

1. INSERT INTO EMPLOYEE VALUES (6, 'Manthan', 'jaipur ', 600000, 48);  

Output: After executing this query, the EMPLOYEE table will look like:

EMP_ID EMP_NAME CITY SALARY AGE

1 ABC Indore 200000 30

2 ABCX Bhopal 300000 26

3 ABCY Dewas 100000 42

4 ABCZ Ujjain 500000 29

5 ABCAA Khandwa 200000 36

6 Manthan jaipur 600000 48

2. By specifying column name

To insert partial column values, you must have to specify the column names.

Syntax

1. INSERT INTO TABLE_NAME    
2. [(col1, col2, col3,.... col N)]    

3. VALUES (value1, value2, value 3, .... Value N);    

Query

1. INSERT INTO EMPLOYEE (EMP_ID, EMP_NAME, AGE) VALUES (7, 'Jai', 40);  

Output: After executing this query, the table will look like:

EMP_ID EMP_NAME CITY SALARY AGE

1 ABC Indore 200000 30

2 ABCX Bhopal 300000 26

3 ABCY Dewas 100000 42

4 ABCZ Ujjain 500000 29

5 ABCAA Khandwa 200000 36

6 Manthan jaipur 600000 48

7 Jai null null 40

In SQL INSERT query, if you add values for all columns then there is no need to specify the column name. But,
you must be sure that you are entering the values in the same order as the column exists.

SQL Update Statement

The SQL UPDATE statement is used to modify the data that is already in the database. The condition in the
WHERE clause decides that which row is to be updated.

Syntax

1. UPDATE table_name  

2. SET column1 = value1, column2 = value2, ...  

3. WHERE condition;  

EMPLOYEE

EMP_ID EMP_NAME CITY SALARY AGE

1 ABC Indore 200000 30

2 ABCX Bhopal 300000 26

3 ABCY Dewas 100000 42

4 ABCZ Ujjain 500000 29

5 ABCAA Khandwa 200000 36

6 Manthan jaipur 600000 48


Updating single record

Update the column EMP_NAME and set the value to 'ABCZZ' in the row where SALARY is 500000.

Syntax

1. UPDATE table_name    

2. SET column_name = value   

3. WHERE condition;   

Query

1. UPDATE EMPLOYEE   

2. SET EMP_NAME = 'ABCZZ'   

3. WHERE SALARY = 500000;  

Output: After executing this query, the EMPLOYEE table will look like:

EMP_ID EMP_NAME CITY SALARY AGE

1 ABC Indore 200000 30

2 ABCX Bhopal 300000 26

3 ABCY Dewas 100000 42

4 ABCZZ Ujjain 500000 29

5 ABCAA Khandwa 200000 36

6 Manthan jaipur 600000 48

Updating multiple records

If you want to update multiple columns, you should separate each field assigned with a comma.

In the EMPLOYEE table, update the column EMP_NAME to 'Kevin' and CITY to 'Boston' where EMP_ID is
5.

Syntax

1. UPDATE table_name    

2. SET column_name = value1, column_name2 = value2    

3. WHERE condition;  

Query

1. UPDATE EMPLOYEE   

2. SET EMP_NAME = 'Karan', City = 'Burhanpur'   
3. WHERE EMP_ID = 5;  

Output

EMP_ID EMP_NAME CITY SALARY AGE

1 ABC Indore 200000 30

2 ABCX Bhopal 300000 26

3 ABCY Dewas 100000 42

4 ABCZZ Ujjain 500000 29

5 Karan Burhanpur 200000 36

6 Manthan jaipur 600000 48

Without use of WHERE clause

If you want to update all row from a table, then you don't need to use the WHERE clause.

In the EMPLOYEE table, update the column EMP_NAME as 'XYZ'.

Syntax

1. UPDATE table_name    

2. SET column_name = value1;  

Query

1. UPDATE EMPLOYEE   

2. SET EMP_NAME = 'XYZ';  

Output

EMP_ID EMP_NAME CITY SALARY AGE

1 XYZ Indore 200000 30

2 XYZ Bhopal 300000 26

3 XYZ Dewas 100000 42

4 XYZ Ujjain 500000 29

5 XYZ Burhanpur 200000 36

6 XYZ jaipur 600000 48

SQL DELETE Statement

The SQL DELETE statement is used to delete rows from a table. Generally, DELETE statement removes one or
more records form a table.
Syntax

1. DELETE FROM table_name WHERE some_condition;  

EMPLOYEE

EMP_ID EMP_NAME CITY SALARY AGE

1 ABC Indore 200000 30

2 ABCX Bhopal 300000 26

3 ABCY Dewas 100000 42

4 ABCZZ Ujjain 500000 29

5 ABCAA Khandwa 200000 36

6 Manthan jaipur 600000 48

Deleting Single Record

Delete the row from the table EMPLOYEE where EMP_NAME = 'ABCZZ'. This will delete only the fourth
row.

Query

1. DELETE FROM EMPLOYEE   

2. WHERE EMP_NAME = 'ABCZZ';  

Output: After executing this query, the EMPLOYEE table will look like:

EMP_ID EMP_NAME CITY SALARY AGE

1 ABC Indore 200000 30

2 ABCX Bhopal 300000 26

3 ABCY Dewas 100000 42

5 ABCAA Khandwa 200000 36

6 Manthan jaipur 600000 48

Deleting Multiple Record

Delete the row from the EMPLOYEE table where AGE is 30. This will delete first row.

Query

1. DELETE FROM EMPLOYEE WHERE AGE= 30;  

Output: After executing this query, the EMPLOYEE table will look like:
EMP_ID EMP_NAME CITY SALARY AGE

2 ABCX Bhopal 300000 26

3 ABCY Dewas 100000 42

5 ABCAA Khandwa 200000 36

6 Manthan jaipur 600000 48

Delete all of the records


Delete all the row from the EMPLOYEE table. After this, no records left to display. The EMPLOYEE table will
become empty.
Syntax
1. DELETE * FROM table_name;    
2. or  
3. DELETE FROM table_name;  
Query
1. DELETE FROM EMPLOYEE;  
Output: After executing this query, the EMPLOYEE table will look like:
EMP_ID EMP_NAME CITY SALARY AGE

Using the condition in the WHERE clause, we can delete single as well as multiple records. If you want to
delete all the records from the table, then you don't need to use the WHERE clause.

Views in SQL
 Views in SQL are considered as a virtual table. A view also contains rows and columns.
 To create the view, we can select the fields from one or more tables present in the database.
 A view can either have specific rows based on certain condition or all the rows of a table.

Student_Detail
STU_ID NAME ADDRESS
1 Sahil Delhi
2 Kartik Noida
3 Dinesh Ghaziabad
4 Alka Gurugram
Student_Marks
STU_ID NAME MARKS AGE
1 Sahil 97 19
2 Kartik 86 21
3 Dinesh 74 18
4 Alka 90 20
5 Satish 96 18

1. Creating view
A view can be created using the CREATE VIEW statement. We can create a view from a single table or
multiple tables.
Syntax:
1. CREATE VIEW view_name AS  
2. SELECT column1, column2.....  
3. FROM table_name  
4. WHERE condition;  

2. Creating View from a single table


In this example, we create a View named DetailsView from the table Student_Detail.
Query:
1. CREATE VIEW DetailsView AS  
2. SELECT NAME, ADDRESS  
3. FROM Student_Details  
4. WHERE STU_ID < 4;  
Just like table query, we can query the view to view the data.
1. SELECT * FROM DetailsView;  
Output:
NAME ADDRESS
Sahil Delhi
Kartik Noida
Dinesh Ghaziabad

3. Creating View from multiple tables


View from multiple tables can be created by simply include multiple tables in the SELECT statement.
In the given example, a view is created named MarksView from two tables Student_Detail and Student_Marks.
Query:
1. CREATE VIEW MarksView AS  
2. SELECT Student_Detail.NAME, Student_Detail.ADDRESS, Student_Marks.MARKS  
3. FROM Student_Detail, Student_Mark  
4. WHERE Student_Detail.NAME = Student_Marks.NAME;  
To display data of View MarksView:
1. SELECT * FROM MarksView;  
NAME ADDRESS MARKS
Sahil Delhi 97
Kartik Noida 86
Dinesh Ghaziabad 74
Alka Gurugram 90

4. Deleting View
A view can be deleted using the Drop View statement.
Syntax
1. DROP VIEW view_name;  
Example:
If we want to delete the View MarksView, we can do this as:
1. DROP VIEW MarksView;  

You might also like