2 Unit-SQL BCS
2 Unit-SQL BCS
Rules:
o Structure query language is not case sensitive. Generally, keywords of SQL are
written in uppercase.
o Statements of SQL are dependent on text lines. We can use a single SQL statement
on one or multiple text line.
o Using the SQL statements, you can perform most of the actions in a database.
o SQL depends on tuple relational calculus and relational algebra.
Characteristics of SQL
o SQL is easy to learn.
o SQL is used to access data from relational database management systems.
o SQL can execute queries against the database.
o SQL is used to describe the data.
o SQL is used to define the data in the database and manipulate it when needed.
o SQL is used to create and drop the database and table.
o SQL is used to create a view, stored procedure, function in a database.
o SQL allows users to set permissions on tables, procedures, and views.
Advantages of SQL
There are the following advantages of SQL:
High speed
Using the SQL queries, the user can quickly and efficiently retrieve a large amount of
records from a database.
No coding needed
In the standard SQL, it is very easy to manage the database system. It doesn't require a
substantial amount of code to manage the database system.
Long established are used by the SQL databases that are being used by ISO and ANSI.
Portability
SQL can be used in laptop, PCs, server and even some mobile phones.
Interactive language
SQL is a domain language used to communicate with the database. It is also used to receive
answers to the complex questions in seconds.
Using the SQL language, the users can make different views of the database structure.
SQL Datatype
o SQL Datatype is used to define the values that a column can contain.
o 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:
binary It has a maximum length of 8000 bytes. It contains fixed-length binary data.
Data Description
type
Datatype Description
timestamp It stores the year, month, day, hour, minute, and the second value.
SQL | Constraints
Constraints are the rules that we can apply on the type of data in a table. That is, we
can specify the limit on the type of data that can be stored in a particular column in a
table using constraints.
The available constraints in SQL are:
NOT NULL: This constraint tells that we cannot store a null value in a column. That
is, if a column is specified as NOT NULL then we will not be able to store null in this
particular column any more.
UNIQUE: This constraint when specified with a column, tells that all the values in the
column must be unique. That is, the values in any row of a column must not be
repeated.
PRIMARY KEY: A primary key is a field which can uniquely identify each row in a
table. And this constraint is used to specify a field in a table as primary key.
FOREIGN KEY: A Foreign key is a field which can uniquely identify each row in a
another table. And this constraint is used to specify a field as Foreign key.
CHECK: This constraint helps to validate the values of a column to meet a particular
condition. That is, it helps to ensure that the value stored in a column meets a
specific condition.
DEFAULT: This constraint specifies a default value for the column when no value is
specified by the user.
How to specify constraints?
We can specify constraints at the time of creating the table using CREATE TABLE
statement. We can also specify the constraints after creating a table using ALTER TABLE
statement.
Syntax:
Below is the syntax to create constraints using CREATE TABLE statement at the time of
creating the table.
1. NOT NULL –
If we specify a field in a table to be NOT NULL. Then the field will never accept null
value. That is, you will be not allowed to insert a new row in the table without specifying
any value to this field.
For example, the below query creates a table Student with the fields ID and NAME as
NOT NULL. That is, we are bound to specify values for these two fields every time we
wish to insert a new row.
Orders
O_I
D ORDER_NO C_ID
1 2253 3
2 3325 3
3 4521 2
4 8532 1
Customers
1 RAMESH DELHI
2 SURESH NOIDA
3 DHARMESH GURGAON
As we can see clearly that the field C_ID in Orders table is the primary key in Customers
table, i.e. it uniquely identifies each row in the Customers table. Therefore, it is a
Foreign Key in Orders table.
Syntax:
SQL Commands
o 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.
o SQL can perform various tasks like create a table, add data to tables, drop the table,
modify the table, set permission for users.
o DDL changes the structure of the table like creating a table, deleting a table, altering
a table, etc.
o All the command of DDL are auto-committed that means it permanently save all the
changes in the database.
o CREATE
o ALTER
o DROP
o TRUNCATE
Syntax:
b. DROP: It is used to delete both the structure and record stored in the table.
Syntax
Example
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:
EXAMPLE
d. TRUNCATE: It is used to delete all the rows from the table and free the space containing
the table.
Syntax:
Example:
o DML commands are used to modify the database. It is responsible for all form of
changes in the database.
o 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.
o INSERT
o UPDATE
o DELETE
a. INSERT: The INSERT statement is a SQL query. It is used to insert data into the row of a
table.
Syntax:
Or
For example:
Here,
table_name is the name of the table into which the rows are to be inserted
column1, column2, column3, ... are the names of columns where the values are to be
inserted
value11, value12, value13, ... , value21, value22, value23, ... are the values to be
inserted
Insert Row Into a Table
In SQL, the INSERT INTO statement is used to insert new row(s) into a database table.
For example,
-- insert a row in the Customers table
INSERT INTO Customers(customer_id, first_name, last_name, age, country)
VALUES
(5, 'Harry', 'Potter', 31, 'USA');
Run Code
Here, the SQL command inserts a new row into the Customers table with the given values.
Exa
mple: SQL Insert Into
Here, the SQL command automatically sets the new customer_id for the new row and
inserts it in a table.
Exa
mple: SQL INSERT INTO
Note: If we want to insert data from any other existing table, we can use the SQL INSERT
INTO SELECT statement.
Insert Multiple Rows at Once in SQL
It's also possible to insert multiple rows to a database table at once. For example,
INSERT INTO Customers(first_name, last_name, age, country)
VALUES
('Harry', 'Potter', 31, 'USA'),
('Chris', 'Hemsworth', 43, 'USA'),
('Tom', 'Holland', 26, 'UK');
Run Code
Here, the SQL command inserts three rows to the Customers table.
b. UPDATE: This command is used to update or modify the value of a column in the table.
Syntax:
For example:
1. UPDATE students
2. SET User_Name = 'Sonoo'
3. WHERE Student_Id = '3'
Here, the SQL command changes the value of the first_name column
to Johnny if customer_id is equal to 1.
Exa
mple: SQL UPDATE Statement
Note: If we want to insert a new row instead of updating an existing row, we can use
the SQL INSERT INTO statement.
Update Multiple Values in a Row
We can also update multiple values in a single row at once. For example,
-- update multiple values in the given row
UPDATE Customers
SET first_name = 'Johnny', last_name = 'Depp'
WHERE customer_id = 1;
Run Code
Here, the SQL command changes the value of the first_name column
to Johnny and last_name to Depp if customer_id is equal to 1.
Here, the SQL command changes the value of the country column to NP if age is 22. If
there are more than one rows where age equals to 22, all the matching rows will be edited.
Here, the SQL command changes the value of the country column to NP for all rows.
Note: We should be cautious while using the UPDATE statement. If we omit the WHERE clause,
all the rows will be changed and this change is irreversible.
4.
Syntax:
For example:
DCL commands are used to grant and take back authority from any database user.
o Grant
o Revoke
Example
Example
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.
a. Commit: Commit command is used to save all the transactions to the database.
Syntax:
1. COMMIT;
Example:
b. Rollback: Rollback command is used to undo transactions that have not already been
saved to the database.
Syntax:
1. ROLLBACK;
Example:
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;
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;
DDL DML
Used to define database objects like tables,
Used to manipulate data within the database.
indexes, views, etc.
Examples of DDL statements include Examples of DML statements include SELECT,
CREATE, ALTER, and DROP. INSERT, UPDATE, and DELETE.
Changes made using DDL affect the Changes made using DML affect the data
structure of the database. stored in the database.
DDL statements are not transactional, DML statements are transactional, meaning
meaning they cannot be rolled back. they can be rolled back if necessary.