You are on page 1of 39

Text

CH-6 :-> QUERIES


● Structured Query Language(SQL) as we all know is the database language by
the use of which we can perform certain operations on the existing database
and also we can use this language to create a database.

● These SQL commands are mainly categorized into four categories as:

● DDL – Data Definition Language


● DQL – Data Query Language
● DML – Data Manipulation Language
● DCL – Data Control Language
DDL
● DDL stands for Data Definition Language.
● Data Definition Language actually consists of the SQL commands that can be
used to define the database.
● It simply deals with descriptions of the database schema and is used to create
and modify the structure of database.

● CREATE:
● To create new database and its object like Table, View or Index.

● ALTER:

● In Alter command, alter the structure of existing database and its object like
table.
● In this command we can also add new column, Modify column , and drop
column.
● DROP :

● Delete the object from the database and its object like table.

● TRUNCATE –

● Remove all records from a table, including all spaces allocated for the records
are removed

● RENAME –

● In this command , Rename an object like table. We also rename table name.
DML- Data Manipulation Language
● DML is short name of Data Manipulation Language which deals with data
manipulation and includes most common SQL statements.
● Such SELECT, INSERT, UPDATE, DELETE, etc., and it is used to store, modify,
retrieve, delete and update data in a database.

● SELECT:

● The SELECT statement is used to select data from a database.


● The data returned is stored in a result table, called the result-set.

● Syntax:

● SELECT column1, column2, ...


FROM table_name;
● Example:

● SELECT * FROM stud;

● INSERT Commands:

● The insert command is used to insert new records in table.

● Syntax:
● INSERT INTO table_name (column1, column2, column3, ...)
VALUES (value1, value2, value3, ...);
● Example:

● Insert into stud values(1,’Raj’,’Rajkot’)

● UPDATE Command:
● 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 stud set Name = ‘Ravi’ where id =1;

● DELETE

● The DELETE statement is used to delete existing records in a table.

● Syntax
● DELETE FROM table_name WHERE condition;

● Example:
● DELETE from stud where id =1;
● The column parameters specify the names of the columns of the table.

● The datatype parameter specifies the type of data the column can hold.

● Example:

● Create table stud


● (id number,
Name char(10),
City char(10))
Constraint : Primary Key
● 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).

● SQL PRIMARY KEY on CREATE TABLE

● The following SQL creates a PRIMARY KEY on the "ID" column when the
"Persons" table is created:
CREATE TABLE

● CREATE TABLE Persons (


ID int PRIMARY KEY,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Age int
);
FOREIGN KEY
● A FOREIGN KEY is a key used to link two tables together.

● A FOREIGN KEY is a field (or collection of fields) in one table that refers to the
PRIMARY KEY in another table.

● The table containing the foreign key is called the child table, and the table
containing the candidate key is called the referenced or parent table.

● Look at the following two tables:

PersonID LastName FirstName Age


● "Persons" table:
1 Hansen Ola 30

●2 Svendson Tove 23

3 Pettersen Kari 20
OrderID OrderNumber PersonID

1 77895 3

2 44678 3
● "Orders" table: 3 22456 2

4 24562 1

● The "PersonID" column in the "Persons" table is the PRIMARY KEY in the
"Persons" table.
● The "PersonID" column in the "Orders" table is a FOREIGN KEY in the "Orders"
table.
● The FOREIGN KEY constraint is used to prevent actions that would destroy
links between tables.
● The FOREIGN KEY constraint also prevents invalid data from being inserted into
the foreign key column, because it has to be one of the values contained in the
table it points to.
UNIQUE KEY
● A unique key is a set of one or more than one fields/columns of a table that
uniquely identify a record in a database table.

● You can say that it is little like primary key but it can accept only one null value
and it cannot have duplicate values.

● The unique key and primary key both provide a guarantee for uniqueness for a
column or a set of columns.

● There is an automatically defined unique key constraint within a primary key


constraint.

● There may be many unique key constraints for one table, but only one
PRIMARY KEY constraint for one table.
● The CHECK constraint is used to limit the value range that can be placed in a
column.

● If you define a CHECK constraint on a single column it allows only certain


values for this column.

● If you define a CHECK constraint on a table it can limit the values in certain
columns based on values in other columns in the row.

● The following SQL creates a CHECK constraint on the "Age" column when the
"Persons" table is created. The CHECK constraint ensures that the age of a
person must be 18, or older:
Example

● CREATE TABLE Persons (


ID int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Age int CHECK (Age>=18)
);

Not Null
● By default, a column can hold NULL values.

● The NOT NULL constraint enforces a column to NOT accept NULL values.

● This enforces a field to always contain a value, which means that you cannot
insert a new record, or update a record without adding a value to this field.

● SQL NOT NULL on CREATE TABLE


● The following SQL ensures that the "ID", "LastName", and "FirstName" columns
will NOT accept NULL values when the "Persons" table is created:

Example

● CREATE TABLE Persons (


ID int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255) NOT NULL,
Age int
);
Aggregate Functions
● SQL aggregation function is used to perform the calculations on multiple rows
of a single column of a table. It returns a single value.
● It is also used to summarize the data.
● Type of Aggregate Functions:
● COUNT FUNCTION

● COUNT function is used to Count the number of rows in a database table. It


can work on both numeric and non-numeric data types.

● COUNT function uses the COUNT(*) that returns the count of all the rows in a
specified table. COUNT(*) considers duplicate and Null.

● Syntax
● COUNT(*)

● Example
● SELECT COUNT(*)
● FROM PRODUCT_MAST;
● SUM Function

● Sum function is used to calculate the sum of all selected columns. It works on
numeric fields only.

● Syntax

● SUM()

● Example
● SELECT SUM(COST)
● FROM PRODUCT_MAST;
● AVG function

● The AVG function is used to calculate the average value of the numeric type.
AVG function returns the average of all non-Null values.
● Syntax

● AVG()

● Example:

● SELECT AVG(COST)
● FROM PRODUCT_MAST;
● MAX Function

● MAX function is used to find the maximum value of a certain column. This
function determines the largest value of all selected values of a column.

● Syntax

● MAX()

● Example:
● SELECT MAX(RATE)
● FROM PRODUCT_MAST;
● MIN Function

● MIN function is used to find the minimum value of a certain column. This
function determines the smallest value of all selected values of a column.

● Syntax
● MIN()

● Example:
● SELECT MIN(RATE)
● FROM PRODUCT_MAST;
Crosstab Query Wizard
● Using the Crosstab Query Wizard requires that you use a single table or query
as the record source for your crosstab query.
● If a single table does not have all the data that you want to include in your
crosstab query, start by creating a select query that returns the data that you
want.
● How to create Queries through Query wizard step by step:

● 1. On the Create tab, in the Queries group, click Query Wizard.


2. In the New Query dialog box, click Crosstab Query Wizard, and then
click OK. The Crosstab Query Wizard starts.

3. On the first page of the wizard, choose the table or query that you want
to use to create a crosstab query. For this example, select
the Products table and then click Next.
4. On the next page, choose the field that contains the values that you want to
use as row headings. You can select up to three fields to use as row headings
sources, but the fewer row headings you use, the easier your crosstab
datasheet will be to read. If you choose more than one field to supply row
headings, the order in which you choose the fields determines the default order
in which your results are sorted.

● For this example, select Supplier IDs.Value and then click the button labeled
with a > symbol. Notice that Access displays the field name along the left side
of the sample query preview at the bottom of the dialog box. Click Next to
continue.
5. On the next page, choose the field that contains the values that you want
to use as column headings. In general, you should choose a field that
contains few values, to help keep your results easy to read. For example,
using a field that has only a few possible values (such as gender) might be
preferable to using a field that can contain many different values (such as
age).
● If the field that you choose to use for column headings has the Date/Time data
type, the wizard adds a step that lets you specify how to group the dates into
intervals, such as months or quarters.
● For this example, select Category and notice that Access displays category
sample names along the top of the sample query preview at the bottom of the
dialog box. Click Next to continue.
6. If you choose a Date/Time field for column headings, the next page of the
wizard asks you to specify the interval to use to group the dates. You can
specify Year, Quarter, Month, Date, or Date/Time. If you do not choose a
Date/Time field for column headings, the wizard skips this page.

7. On the next page, choose a field and a function to use to calculate summary
values. The data type of the field that you select determines which functions
are available.
8. On the last page of the wizard, type a name for your query and then
specify whether you want to view the results or modify the query design.
● You can change the function that is used to produce row sums by editing the
crosstab query in Design view.
9. If you've walked through this example using the Products table from the
Northwind database, the crosstab query displays the list of supplier
names as rows, the product category names as columns, and a count of
the number of products in each intersection.
Parameter query
● The best part about queries is that you can save and run the same query again
and again, but when you run the same query again and again by only changing
the criteria then you might consider the query to accept parameters.

● If you frequently want to run variations of a particular query, consider using a


parameter query

● Parameter query retrieves information in an interactive manner prompting the


end user to supply criteria before the query is run.

● You can also specify what type of data a parameter should accept.
● You can set the data type for any parameter, but it is especially important to
set the data type for numeric, currency, or date/time data.

● When you specify the data type that a parameter should accept, users see a
more helpful error message if they enter the wrong type of data, such as
entering text when currency is expected.

● If a parameter is set to accept text data, any input is interpreted as text, and no
error message is displayed.

You might also like