Dbms 1
Dbms 1
• What is Data?
• Data is a collection of a distinct small unit of information. It can be used in a variety of forms like text, numbers, media,
bytes, etc. it can be stored in pieces of paper or electronic memory, etc.
• A database is an organized collection of data, so that it can be easily accessed and managed.
• You can organize data into tables, rows, columns, and index it to make it easier to find relevant information.
• Database handlers create a database in such a way that only one set of software program provides access of data
to all the users.
• The main purpose of the database is to operate a large amount of information by storing, retrieving, and
managing data.
• There are many dynamic websites on the World Wide Web nowadays which are handled through databases. For
example, a model that checks the availability of rooms in a hotel. It is an example of a dynamic website that uses
a database.
• There are many databases available like MySQL, Sybase, Oracle, MongoDB, Informix, PostgreSQL, SQL Server, etc.
• Modern databases are managed by the database management system (DBMS).
• SQL or Structured Query Language is used to operate on the data stored in a database. SQL depends on relational
algebra and tuple relational calculus.
SQL
• SQL (Structured Query Language) is used to perform operations on
the records stored in the database, such as updating records, inserting
records, deleting records, creating and modifying database tables,
views, etc.
• SQL is not a database system, but it is a query language.
• Suppose you want to perform the queries of SQL language on the
stored data in the database. You are required to install any database
management system in your systems, for example, Oracle, MySQL,
MongoDB, PostgreSQL, SQL Server, DB2, etc.
• Why SQL?
• Nowadays, SQL is widely used in data science and analytics. Following are the
reasons which explain why it is widely used:
• The basic use of SQL for data professionals and SQL users is to insert, update, and
delete the data from the relational database.
• SQL allows the data professionals and users to retrieve the data from the relational
database management systems.
• It allows SQL users to create, drop, and manipulate the database and its tables.
• It also helps in creating the view, stored procedure, and functions in the relational
database.
• It allows you to define the data and modify that stored data in the relational
database.
• It also allows SQL users to set the permissions or constraints on table columns,
views, and stored procedures.
• Some SQL Commands
• The SQL commands help in creating and managing the database. The
most common SQL commands which are highly used are mentioned
below:
• CREATE command
• UPDATE command
• DELETE command
• SELECT command
• DROP command
• INSERT command
CREATE Command
This command helps in creating the new database, new table, table view, and other objects of the database.
UPDATE Command
This command helps in updating or changing the stored data in the database.
DELETE Command
This command helps in removing or erasing the saved records from the database tables. It erases
single or multiple tuples from the tables of the database.
SELECT Command
This command helps in accessing the single or multiple rows from one or multiple tables of the
database. We can also use this command with the WHERE clause.
DROP Command
This command helps in deleting the entire table, table view, and other objects from the database.
INSERT Command
This command helps in inserting the data or records into the database tables. We can easily insert the
records in single as well as multiple rows of the table.
Types of DBMS Language
• Data Definition Language (DDL)
• DDL stands for Data Definition Language. It is used to define database structure or pattern.
• It is used to create schema, tables, indexes, constraints, etc. in the database.
• Using the DDL statements, you can create the skeleton of the database.
• Data definition language is used to store the information of metadata like the number of tables
and schemas, their names, indexes, columns in each table, constraints, etc.
• Here are some tasks that come under DDL:
• Create: It is used to create objects in the database.
• Alter: It is used to alter the structure of the database.
• Drop: It is used to delete objects from the database.
• Truncate: It is used to remove all records from a table.
• Rename: It is used to rename an object.
• Comment: It is used to comment on the data dictionary.
• These commands are used to update the database schema that's why they come under Data
definition language.
Data Manipulation Language (DML)
DML stands for Data Manipulation Language. It is used for accessing and manipulating data
in a database. It handles user requests.
Here are some tasks that come under DML:
• create table tablename (column1 datatype, column2 datatype, .......
columnN datatype);
Let us take an example to create a STUDENTS table with ID as primary key and NOT NULL
are the constraint showing that these fields cannot be NULL while creating records in the
table.
• SQL> CREATE TABLE STUDENTS (
ID Number NOT NULL,
NAME VARCHAR (20) NOT NULL,
AGE Number NOT NULL,
ADDRESS VARCHAR2 (25),
PRIMARY KEY (ID)
);
The SQL INSERT INTO Statement
The INSERT INTO statement is used to insert new records in a table.
INSERT INTO Syntax
It is possible to write the INSERT INTO statement in two ways:
1. Specify both the column names and the values to be inserted:
2. If you are adding values for all the columns of the table, you do not need to specify the column names in the
SQL query. However, make sure the order of the values is in the same order as the columns in the table. Here, the
INSERT INTO syntax would be as follows:
Ex: INSERT INTO Customers (CustomerName, ContactName, Address, City, PostalCode, Country)
VALUES ('Cardinal', 'Tom B. Erichsen', 'Skagen 21', 'Stavanger', '4006', 'Norway');
Here, column1, column2, ... are the field names of the table you want to select data from.
If you want to select all the fields available in the table, use the following syntax:
The SELECT DISTINCT statement is used to return only distinct (different) values.
Inside a table, a column often contains many duplicate values;
and sometimes you only want to list the different (distinct) values.
SELECT DISTINCT Syntax
The following SQL statement selects only the DISTINCT values from the "Country" column in the
"Customers" table:
EX: SELECT DISTINCT Country FROM Customers;
The following SQL statement lists the number of different (distinct) customer countries:
EX: SELECT COUNT(DISTINCT Country) FROM Customers;
The SQL WHERE Clause
The WHERE clause is used to filter records.
It is used to extract only those records that fulfill a specified condition.
The WHERE clause can be combined with AND, OR, and NOT operators.
The AND and OR operators are used to filter records based on more than one condition:
•The AND operator displays a record if all the conditions separated by AND are TRUE.
•The OR operator displays a record if any of the conditions separated by OR is TRUE.
•The NOT operator displays a record if the condition(s) is NOT TRUE.
AND Syntax:
Ex: The following SQL statement selects all fields from "Customers" where country is
"Germany" AND city is "Berlin":
The ORDER BY keyword is used to sort the result-set in ascending or descending order.
TheORDER BY keyword sorts the records in ascending order by default.
To sort the records in descending order, use the DESC keyword.
ORDER BY Syntax
SELECT column1, column2, ...
FROM table_name
ORDER BY column1, column2, ... ASC|DESC;
The following SQL statement selects all customers from the "Customers" table, sorted by the
"Country" column:
SELECT * FROM Customers
ORDER BY Country;
The following SQL statement selects all customers from the "Customers" table, sorted
DESCENDING by the "Country" column:
SELECT * FROM Customers
ORDER BY Country DESC;
SQL ALTER TABLE Statement
The ALTER TABLE statement is used to add, delete, or modify columns in an existing table.
The ALTER TABLE statement is also used to add and drop various constraints on an existing table.
ALTER TABLE table_name
MODIFY column_name datatype;
SQL Cloning Tables
Cloning or Copying a Table
There may be a situation when you just want to create an exact copy or clone of an existing table to test
or perform something without affecting the original table.
The following section describes how to do this in few easy steps.
Step 1: Creating an Empty Table
First use the following statement to create an empty table based on the definition of original table. It also
includes the column attributes and indexes defined in the original table:
CREATE TABLE emp1 LIKE employee;
Step 2: Inserting Data into Table
Now, use the following statement to populate the empty table with data from original table:
INSERT INTO new_table SELECT * FROM original_table;
Simple Cloning
However, if you just want to create a table from another table without taking into account any column
attributes and indexes you can use the simple one line statement:
CREATE TABLE new_table as SELECT * FROM original_table;
CREATE TABLE Persons (
ID int NOT NULL UNIQUE,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Age int
);
The PRIMARY KEY constraint uniquely identifies each record in a table.
Primary keys must contain UNIQUE values, and cannot contain NULL values.
A table can have only ONE primary key; and in the table, this primary key can consist
of single or multiple columns (fields).
CREATE TABLE Persons (
ID Number NOT NULL PRIMARY KEY,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Age Number
);
SQL PRIMARY KEY on ALTER TABLE
To create a PRIMARY KEY constraint on the "ID" column when the table is already created, use the following
SQL:
ALTER TABLE Persons
ADD PRIMARY KEY (ID);
DROP a PRIMARY KEY Constraint
To drop a PRIMARY KEY constraint, use the following SQL:
ALTER TABLE Persons
DROP PRIMARY KEY;
SQL FOREIGN KEY Constraint
The FOREIGN KEY constraint is used to prevent actions that would destroy links between tables.
A FOREIGN KEY is a field (or collection of fields) in one table, that refers to the PRIMARY KEY in another
table.
The table with the foreign key is called the child table, and the table with the primary key is called the referenced
or parent table.
CREATE TABLE Orders (
OrderID int NOT NULL PRIMARY KEY,
OrderNumber int NOT NULL,
PersonID int FOREIGN KEY REFERENCES Persons(PersonID)
);
To allow naming of a FOREIGN KEY constraint, and for defining a FOREIGN KEY constraint on multiple
columns, use the following SQL syntax:
CREATE TABLE Orders (
OrderID int NOT NULL,
OrderNumber int NOT NULL,
PersonID int,
PRIMARY KEY (OrderID),
CONSTRAINT FK_PersonOrder FOREIGN KEY (PersonID)
REFERENCES Persons(PersonID)
);
Create table student_Details (
Roll No. number,
Name varchar(20),
Course_Id number,
Primary key (Course_Id)
);