Lecture 9 SQL
Lecture 9 SQL
BASIC DEFINITIONS
⮚ Database: A collection of related data.
⮚ Data: Known facts that can be recorded and have an implicit
meaning.
⮚ Database Management System (DBMS): A software
package/ system to facilitate the creation and maintenance
of a computerized database.
⮚ Database System: The DBMS software together with the
data itself. Sometimes, the applications are also included.
RELATIONAL DATABASE MANAGEMENT SYSTEM
(RDBMS):
⮚CHAR (size): This data type is used to store character strings values of fixed length.
The size in the brackets determines the number of characters it can hold.
The maximum number of characters (i.e. the size) this data type can hold is 255
characters.
The data held is right padded with spaces to whatever length specified.
⮚VARCHAR (size): This data type is used to store variable length alphanumeric data.
It is a flexible form of CHAR data type.
The maximum data this data type can hold is 4000 characters.
⮚NUMBER (P,S): The NUMBER data type is used to store numbers(fixed or floating point).
Numbers of virtually any magnitude maybe stored up to 38 digits of precision.
SQL COMPONENTS
This component of the SQL language is used to create and modify tables and other
objects in the database.
Purpose:- Tables are the basic unit of data storage in Database. Data
is stored in rows and columns.
Syntax:-
SQL Constraints
• Constraints are used to limit the type of data that can go into a table.
• Constraints can be specified when a table is created (with the CREATE TABLE
statement) or after the table is created (with the ALTER TABLE statement).
• The NOT NULL constraint enforces a column to NOT accept NULL values.
• The NOT NULL constraint enforces a field to always contain a value. This means that you cannot
insert a new record, or update a record without adding a value to this field.
We see that “CustomerNumber”, “LastName” and “FirstName” is set to “NOT NULL”, this means these columns
needs to contain data. While “AreaCode”, “Address” and “Phone” may be left empty, i.e, they don’t need to be filled
out.
• The CHECK constraint is used to limit the value range that can be placed in a column.
• If you define a CHECK constraint on a single column it allows only certain values for this column.
• If you define a CHECK constraint on a table it can limit the values in certain columns based on
values in other columns in the row.
• In this case, when we try to insert a Customer Number less than zero we will get an error message.
DEFAULT
Purpose:- The structure of a table can be modified by using a alter table command. Alter
Table allows changing the structure of an existing table. With Alter Table it is possible to add
or delete columns, create or destroy indexes, change the data type of existing columns, or
rename columns or the table itself
Syntax:-
Adding New Columns
Alter table <table name> add(<new col name> <data type> (<Size>), <new col name> <data
type> (<Size>),…..);
Dropping A Column
Alter table <table name> drop column <col name>;
ALTER TABLE
• The ALTER TABLE statement is used to add, delete, or modify columns in
an existing table.
Example:-
Example:-
Drop table CSEA_Data101;
DATA MANIPULATION LANGUAGE (DML)
Purpose:- The Insert Into Table command is used to load the created table
with data to be manipulated later.
Example:-
Insert into STUDENT values(101, ‘Aanchal’, ’CSE’);
Insert into STUDENT values(126, ‘Jotnain’, ’CSE’);
Insert into STUDENT values(149, ‘Sapanpreet’, ’ECE’);
Insert into STUDENT values(151, ‘Savita’, ’CSE’);
UPDATE
2. It is used to delete rows or records 2. It is used to delete the entire table 2. It is used to delete the entire
based on conditions specified in the along with its schema and structure records of a table without affecting
WHERE clause. respectively. the schema of the table.
• If you want the data to appear in a specific order you need to use the “order by” keyword.
• If you use the “order by” keyword, the default order is ascending (“asc”). If you want the order to
be opposite, i.e., descending, then you need to use the “desc” keyword.
The WHERE Clause
• The WHERE clause is used to extract only those records that fulfill a specified
criterion.
Operators
• The BETWEEN operator selects a range of data between two values. The values can be numbers,
text, or dates.
AND & OR Operators
• The AND operator displays a record if both the first condition and the second condition is true.
• The OR operator displays a record if either the first condition or the second condition is true.
Thank you
47