SQL Part1
SQL Part1
SQL Part1
(SQL)
PART - 1
www.linkedin.com/in/sushmithadogga
1. SQL INTRODUCTION
● SQL is one of the most important programming languages to know if you work with data.
● SQL stands for Structured Query Language .
● It is a standard language for storing, accessing and manipulating data in a relational database
management system (RDMS).
● SQL allows you to query the database in a variety of ways, using English-like statements.
● It is used on websites for back-end data storage and data processing solutions.
● SQL provides CRUD functionality for databases. CRUD stands for
Create, Read, Update, Delete.
Data Manipulation Language(DML): to insert, update and delete the existing records
from the database.
www.linkedin.com/in/sushmithadogga
2. DATA DEFINITION LANGUAGE
Data definition language (DDL) is a set of SQL commands used to create, modify, and delete
the structure of database objects but not data. These database objects include schemas, tables,
views, sequences, catalogs, indexes, variables, masks, permissions, and aliases.
The CREATE DATABASE statement is used to create a new SQL database named db1.
USE db1;
column1 datatype,
The CREATE TABLE statement is used to create a new table named tb1.
The USE statement is used to create the table within the db1 database.
www.linkedin.com/in/sushmithadogga
2.3 Data Types:
www.linkedin.com/in/sushmithadogga
2.3.2 Numeric Data Types:
www.linkedin.com/in/sushmithadogga
2.3.3 Date and Time Data Types:
2.4 Constraints:
SQL constraints are used to specify rules for data in a table. Constraints can be specified either
when the table is created using CREATE TABLE statement or after the table is created using
ALTER TABLE statement.
Constraints can be column level or table level. They limit the type of data that can go into a
table.
● NOT NULL - specifies that a column must not have NULL values
● UNIQUE - specifies that all values in a column must be different
● PRIMARY KEY - combination of NOT NULL and UNIQUE. It uniquely identifies each record in
a table
● FOREIGN KEY - refers to primary key of another table
● CHECK - enables a condition to check the value being entered into a record.
● DEFAULT - sets a default value for a column if no value is specified
● CREATE INDEX - creates index in tables to retrieve data from the database very quickly
www.linkedin.com/in/sushmithadogga
2.4.1 NOT NULL :
Marks int
);
To create a NOT NULL constraint on the "Marks" column when the "student" table is already
created, we use:
2.4.2 UNIQUE :
There can be many UNIQUE constraints per table, but only one PRIMARY KEY constraint per
table.
Class varchar(50),
Marks int
);
Class varchar(50),
Marks int
To create a UNIQUE constraint on the "ID" column when the “student” table is already created,
we use:
To create a UNIQUE constraint on multiple columns, when the “student” table is already
created, we use:
Class varchar(50),
Marks int
);
www.linkedin.com/in/sushmithadogga
To create a PRIMARY KEY constraint on multiple columns, we use:
Class varchar(50),
Marks int,
);
To create a PRIMARY KEY constraint on the "ID" column when the table “student” is already
created, we use:
To create a PRIMARY KEY constraint on multiple columns,when the table “student” is already
created, we use:
The table with the foreign key is called the child table, and the table with the primary key is
called the parent table.
www.linkedin.com/in/sushmithadogga
Here we have two tables:
Student table:
1 Air BatchA 85
2 Water BatchB 79
3 Land BatchC 67
Department table:
10 ECE 2
11 CSE 1
12 IT 3
To create a FOREIGN KEY on the "Student_id" column when the "Department" table is
created, we use:
To create a FOREIGN KEY constraint on the "Student_id" column when the "Department"
table is already created, we use:
When a CHECK constraint is defined on a table it can limit the values in certain columns based
on values in other columns in the row.
To create a CHECK constraint on the "Marks" column when the "student" table is created to
allow only those values having Marks = 80 or more, we use:
Class varchar(50),
);
Class varchar(50),
Marks int,
);
To create a CHECK constraint on the "Marks" column when the “student” table is already
created, we use:
All new records are added with the default value if no alternative value is specified.
To set a DEFAULT value for the "Marks" column when the "student" table is created, we use:
Class varchar(50),
);
To set a DEFAULT value for the "Marks" column when the "student" table is created, we use:
Although we cannot see indexes, they are used to speed up searches and queries.
www.linkedin.com/in/sushmithadogga
2.5 Create a View:
● Views are virtual tables based on the result-set of the SQL query.
● Views contain rows and columns like a real table.
● The fields in a view are fields from one or more real tables in the database.
● A view always displays the most recent data.
● Every time we run the query, the database recreates the view.
CREATE VIEW v1 AS
FROM tb1
WHERE condition;
www.linkedin.com/in/sushmithadogga