0% found this document useful (0 votes)
0 views17 pages

10th DBMS Important Commands

The document provides an overview of SQL (Structured Query Language) and its capabilities, including executing queries, retrieving, inserting, updating, and deleting records in databases. It explains the structure of relational databases, including tables, fields, and records, and introduces important SQL commands and clauses such as SELECT, WHERE, ORDER BY, and INSERT INTO. Additionally, it covers functions like MAX(), MIN(), and AVG(), as well as the use of forms and reports for data collection and analysis.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views17 pages

10th DBMS Important Commands

The document provides an overview of SQL (Structured Query Language) and its capabilities, including executing queries, retrieving, inserting, updating, and deleting records in databases. It explains the structure of relational databases, including tables, fields, and records, and introduces important SQL commands and clauses such as SELECT, WHERE, ORDER BY, and INSERT INTO. Additionally, it covers functions like MAX(), MIN(), and AVG(), as well as the use of forms and reports for data collection and analysis.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 17

Notes Class

10thDatabaseManagementSystem

What is SQL?
SQL stands for Structured Query Language
SQL lets you access and manipulate databases
SQL became a standard of the American National
Standards Institute (ANSI) in 1986, and of the International
Organization for Standardization (ISO) in 1987

What Can SQL do?


SQL can execute queries against a database
SQL can retrieve data from a database
SQL can insert records in a database
SQL can update records in a database
SQL can delete records from a database
SQL can create new databases
SQL can create new tables in a database
SQL can create stored procedures in a database
SQL can create views in a database
SQL can set permissions on tables, procedures, and views
RDBMS
RDBMS stands for Relational Database Management
System.
RDBMS is the basis for SQL, and for all modern database
systems such as MS SQL Server, IBM DB2, Oracle, MySQL,
and Microsoft Access.
The data in RDBMS is stored in database objects called
tables. A table is a collection of related data entries and it
consists of columns and rows.
Fields
Every table is broken up into smaller entities called fields.
The fields in the Customers table consist of CustomerID,
CustomerName, ContactName, Address, City, PostalCode
and Country. A field is a column in a table that is designed
to maintain specific information about every record in the
table.
Records
A record, also called a row, is each individual entry that
exists in a table. A record is a horizontal entity in a table.

A column is a vertical entity in a table that contains all


information associated with a specific field in a table.
The following SQL statement selects all the records in the
"Customers" table:
Example
SELECT * FROM Customers;
SQL keywords are NOT case sensitive: select is the
same as SELECT
Semicolon after SQL Statements?
Some database systems require a semicolon at the end of
each SQL statement.
Semicolon is the standard way to separate each SQL
statement in database systems that allow more than one
SQL statement to be executed in the same call to the
server.
Some Important SQL Commands
SELECT - extracts data from a database
UPDATE - updates data in a database
DELETE - deletes data from a database
INSERT INTO - inserts new data into a database
CREATE DATABASE - creates a new database
ALTER DATABASE - modifies a database
CREATE TABLE - creates a new table
ALTER TABLE - modifies a table
DROP TABLE - deletes a table
CREATE INDEX - creates an index (search key)
DROP INDEX - deletes an index
SELECT CustomerName, City FROM Customers;
SELECT DISTINCT
SELECT DISTINCT Country FROM Customers;
SELECT COUNT(DISTINCT Country) FROM Customers;
Number of Records: 1

COUNT(DISTINCT
Country)

21

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.
WHERE Syntax
SELECT column1, column2, ...
FROM table_name
WHERE condition;
Note: The WHERE clause is not only used
in SELECT statements, it is also used in UPDATE, DELETE,
etc.!

Example
SELECT * FROM Customers
WHERE Country='Mexico';

SELECT * FROM Customers


WHERE CustomerID=1;

Operators in The WHERE Clause


Operator Description Example
= Equal
> Greater than
< Less than
>= Greater than or equal
<= Less than or equal
<> Not equal. Note: In some versions of SQL this operator
may be written as !=
BETWEEN Between a certain range
LIKE Search for a pattern
IN To specify multiple possible values for a column

The SQL ORDER BY Keyword

The ORDER BY keyword is used to sort the result-set in


ascending or descending order.
The ORDER 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;

SELECT * FROM Customers


ORDER BY Country;

SELECT * FROM Customers


ORDER BY Country DESC;

The SQL INSERT INTO Statement

The INSERT INTO statement is used to insert new records


in a table.
INSERT INTO Syntax
INSERT INTO table_name (column1, column2, column
3, ...)
VALUES (value1, value2, value3, ...);

INSERT INTO table_name


VALUES (value1, value2, value3, ...);

INSERT INTO Customers (CustomerName, ContactName,


Address, City, PostalCode, Country)
VALUES ('Cardinal', 'Tom B. Erichsen', 'Skagen
21', 'Stavanger', '4006', 'Norway');

OUTPUT:

What is a NULL Value?

A field with a NULL value is a field with no value.


If a field in a table is optional, it is possible to insert a new
record or update a record without adding a value to this
field. Then, the field will be saved with a NULL value.
Note: A NULL value is different from a zero value or a field
that contains spaces. A field with a NULL value is one that
has been left blank during record creation!

The SQL UPDATE Statement

The UPDATE statement is used to modify the existing


records in a table.
Syntax
UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition;
Example:
UPDATE Customers
SET ContactName = 'Ajay, City= 'Rewa'
WHERE CustomerID = 1;

UPDATE Multiple Records

UPDATE Customers
SET ContactName='Vibhu'
WHERE Country='INDIA';

Dangerous:
UPDATE Customers
SET ContactName='Jadu';

The SQL DELETE Statement

The DELETE statement is used to delete existing records in


a table.
Syntax
DELETE FROM table_name WHERE condition;
Example
DELETE FROM Customers WHERE CustomerName='Vibhu’;

Delete All Records


It is possible to delete all rows in a table without deleting
the table. This means that the table structure, attributes,
and indexes will be intact:
DELETE FROM table_name;
DELETE FROM Customers;

The SQL CREATE TABLE Statement

The CREATE TABLE statement is used to create a new table


in a database.
Syntax
CREATE TABLE table_name (
column1 datatype,
column2 datatype,
column3 datatype,
....
);

Example
CREATE TABLE Persons (
PersonID int,
LastName varchar(255),
FirstName varchar(255),
Address varchar(255),
City varchar(255)
);
Create Table Using Another Table

A copy of an existing table can also be created


using CREATE TABLE.
The new table gets the same column definitions. All
columns or specific columns can be selected.
If you create a new table using an existing table, the new
table will be filled with the existing values from the old
table.
Syntax
CREATE TABLE new_table_name AS
SELECT column1, column2,...
FROM existing_table_name
Example
CREATE TABLE TestTable AS
SELECT customername, contactname
FROM customers;

SQL DROP TABLE Statement

The DROP TABLE statement is used to drop an existing


table in a database.

Syntax
DROP TABLE table_name;

Example
DROP TABLE Shippers;

SQL TRUNCATE TABLE

The TRUNCATE TABLE statement is used to delete the data


inside a table, but not the table itself.

Syntax
TRUNCATE TABLE table_name;
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 - ADD Column

To add a column in a table

Syntax:
ALTER TABLE table_name
ADD column_namedatatype;

Example
ALTER TABLE Customers
ADD Email varchar(255);

ALTER TABLE - DROP COLUMN

To delete a column in a table


Syntax:
ALTER TABLE table_name
DROP COLUMN column_name;

Example
ALTER TABLE Customers
DROP COLUMN Email;

ALTER TABLE - RENAME COLUMN

ALTER TABLE table_name


RENAME COLUMN old_name to new_name;

ALTER TABLE - ALTER/MODIFY DATATYPE

To change the data type of a column in a table


ALTER TABLE table_name
ALTER COLUMN column_namedatatype;

MAX() Function

The MAX() function returns the maximum value in a set of


values.
SELECT MAX(Price) AS LargestPrice FROM Products;
MIN() Function
The MIN() function returns the minimum value in a set of
values.

SELECT MIN(Price) AS SmallestPrice FROM Products;

POWER() Function
The POWER() function returns the value of a number raised
to the power of another number.
SELECT POWER(4, 2);
Parameter Values

Parameter Description

a Required. A number (the base)

b Required. A number (the exponent)

SUM() Function

The SUM() function calculates the sum of a set of values.


Note: NULL values are ignored.
SELECT SUM(Quantity) AS TotalItemsOrdered FROM
OrderDetails;

AVG() Function

The AVG() function returns the average value of an


expression.
SELECT AVG(Price) AS AveragePrice FROM Products;

SELECT * FROM Products


WHERE Price > (SELECT AVG(Price) FROM Products);

COUNT() Function

The COUNT() function returns the number of records


returned by a select query.
SELECT COUNT(ProductID) AS NumberOfProducts FR
OM Products;
UPPER() Function

The UPPER() function converts a string to upper-case.


SELECT UPPER('SQL is FUN!');
SELECT UPPER(CustomerName) AS UppercaseCusto
merName
FROM Customers;

LOWER() Function
The LOWER() function converts a string to lower-case.
SELECT LOWER('SQL Tutorial is FUN!');

SELECT LOWER(CustomerName) AS LowercaseCusto


merName
FROM Customers;

LEN() Function
The LEN() function returns the length of a string.
SELECT LEN('School is as school');

Forms and Report:

Forms:
Forms are used to collect data, display results of the
queries, perform computations etc. Reports are used to
give the summary data.

Reports:
Database Reports are created from data visualized for
analysis, data discovery, and decision-making, and
contains useful data for decision-making and analysis. Most
business applications come with a built-in reporting tool,
which is a front-end interface that calls back-end database
queries.

You might also like