Database & SQL
Database & SQL
Database is a systematic collection of data. Databases support storage and manipulation of data.
Databases make data management easy.
Database is a collection of inter-related data which helps in efficient retrieval, insertion and deletion
of data from database and organizes the data in the form of tables, views, schemas, reports etc.
An online telephone directory would definitely use database to store data pertaining to people, phone
numbers, other contact details, etc.
Your electricity service provider is obviously using a database to manage billing , client related
issues, to handle fault data, etc.
What is DBMS
The software which is used to manage database is called Database Management System
(DBMS).
For Example, MySQL, Oracle etc. are popular commercial DBMS used in different
applications.
It also helps to control access to the database.
Database Management Systems are not a new concept and as such had been first
implemented in 1960s.
SQL
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.
There are five types of SQL commands: DDL, DML, DCL, TCL, and DQL.
Types of SQL command
Create and Drop Database
CREATE DATABASE databasename;
Example Create database BCA
CREATE TABLE table_name (
column1 datatype,
column2 datatype,
column3 datatype,
....
);
The column parameters specify the names of the columns of the table.
The datatype parameter specifies the type of data the column can hold (e.g. varchar, integer, date, etc.).
CREATE TABLE Students(
ID int,
LastName varchar(255),
FirstName varchar(255),
Address varchar(255),
Phone varchar(255)
);
Drop table
DROP TABLE table_name;
Drop table Students;
SQL Constraints
Syntax :
INSERT INTO table_name (column1, column2, column3, ...)
VALUES (value1, value2, value3, ...)
OR
INSERT INTO table_name
VALUES (value1, value2, value3, ...);
Update table
UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition;
Example
Update students set name=“Seema” where id=3;
Delete
Syntax: DELETE FROM table_name WHERE condition;
Example : Delete from Students where id=4;
Select
SELECT * FROM table_name
SELECT col1,col2 from table_name
Select distinct col1,col2 from table_name
Select * from table_name where address=“Bhaktapur”
Select * from table_name where name=“sudip”
SQL min max
SELECT MAX(column_name)
FROM table_name
WHERE condition;
SELECT MAX(column_name)
FROM table_name
WHERE condition;
SQL Between operator
SELECT column_name(s)
FROM table_name
WHERE column_name BETWEEN value1 AND value2;
Like operator
The LIKE operator is used in a WHERE clause to search for a specified pattern in a
column.
There are two wildcards often used in conjunction with the LIKE operator:
% - The percent sign represents zero, one, or multiple characters
_ - The underscore represents a single character
SELECT column1, column2, ...
FROM table_name
WHERE columnN LIKE pattern;
SELECT name
WHERE students LIKE ’a%’;
SQL COUNT(), AVG() and SUM() Functions
SELECT COUNT(column_name)
FROM table_name
WHERE condition
SELECT AVG(column_name)
FROM table_name
WHERE condition;
SELECT SUM(column_name)
FROM table_name
WHERE condition;