0% found this document useful (0 votes)
11 views

SQL Notes

The document provides an overview of SQL commands used in MySQL for database management, including commands for creating, modifying, and deleting tables, as well as inserting and accessing data. It covers various SQL operations such as joins, aggregate functions, and pattern matching. Additionally, it explains the use of primary keys and distinct keywords in managing data within relational databases.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

SQL Notes

The document provides an overview of SQL commands used in MySQL for database management, including commands for creating, modifying, and deleting tables, as well as inserting and accessing data. It covers various SQL operations such as joins, aggregate functions, and pattern matching. Additionally, it explains the use of primary keys and distinct keywords in managing data within relational databases.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 25

Database Management

CLASS-XII
✔In order to access data within the MySQL database, all
SQL(structured Query Language)

programmers and users must use SQL.


✔SQL is the set of commands that is recognized by all
RDBMS.
✔All RDBMS like Mysql,Ms Access,Oracle,and SQL server
use sql as a standard database language
Classification of SQL statements
Using Database
•Following command is used to use a Database
mysql>
USE <database name >;
For ex -
mysql> USE school;
A message will come saying- “database changed”

See the Commands


carefully
Table Creation
• To create a table in Database, following command is used-
mysql> CREATE TABLE <Table Name> (<Col1>
<DataType(Size)>,
<Col2><DataType(size)>, .
. . );
For ex-
mysql>create table student (Roll INT(4) Primary Key, Name CHAR(20),
(Age INT(2), City CHAR(10) ) ;
A message will come saying- “Query OK”

Primary key restrict a column to have unique


values only.
Insertion of a record in Table
Syntax to insert a record in a Table is-
mysql> INSERT INTO <TableName> (<Col1> <Col2> <Col3> <Col4>
VALUES
(<val1>,<val2>,<val3>,<val4>,. . .);

We can change the order of columns as-

Here, we can insert values without specifying column names provided the order of
values for columns should be same as in table.
Dropping a Table
• To drop a table in Database, following command
is
mysql> DROP Table <Table Name>;

For ex -
mysql>drop table <Student>
A message will come saying- “Query OK” now if you want to
see the structure of the table you cant see because it has
already been deleted.
Modification in Table structure
• To modifystructure of a table in
Database, following command is used-
mysql>ALTER TABLE <Table name> ADD/MODIFY(<Col>
<type(size)>, . . . .)

For ex-mysql> Alter Table Student Add (class INT(2));


A message comes saying “Query OK” .

Again run the DESC command-

• A new column has been add.


• Columns can be added.
• Column size can be changed.
Accessing a Table
Syntax to access Data from a table is-
mysql> SELECT <Col Names> FROM <Table Name>
WHERE <Condition>

Here * means all


columns and without
condition it will displays
all records.

Here only those records will


display where city is
Barabanki.
Accessing a Table
Syntax to access Data from a table is-
mysql> SELECT <Col Names> FROM <Table Name>
WHERE <Condition>
Here Name and class of only
those records are displayed
which are not from
Barabanki.

Here columns have been


rearranged.
Updating a record in Table
Syntax to update a record in a Table is-
mysql> UPDATE <TableName> SET
<ColName>=<NewValue>
WHERE
<Condition>
In this table, age of
meera is to be set 6. and
city of roll 1004 and 1005
is to be set as Lucknow.
Deletion of a record from a Table
• Syntax to delete a record from a Table is-
mysql>DELETE FROM<TableName>WHERE <Condition>

Viewing records
after deletion.

To delete all records from a table, following command will be used-


Distinct keyword
l cities in the table.

Viewing Tables in a Database


Displays all tables in a Databse.
Table from another Table
Syntax for creation of a table from another table is
- mysql>CREATE TABLE <TableName>
AS (SELECT <Cols> FROM <ExistingTable>
WHERE <Condition>);

See the example carefully


Pattern Matching
With ‘like‘ two symbols are to
be used ‘%’ and ‘_’.
‘%’represent multiple
characters whereas ‘_’
represents one charachetr .

In above example all the names starting with ‘S’ are


shown.
In example given below all the names having ‘u’ as
second character are shown.
Other SQL Commands

• Select * from Student where city in (‘Jaipur’,’Ajmer’);

• Select * from Student where city Not in(‘Jaipur’,’Ajmer’);

• Select * from Student where age between 5 and 7;

• Select * from Student Order by name DESC ;

• Select 5 * 6 from DUAL ;


AGGREGATE FUNCTIONS

Aggregation is an operation that computes a single value


from all the values of an attribute.
SQL provides five functions that apply to an attribute of a
relation and produce some aggregatation of that column
-SUM: computes the sum of values in a column.
-AVG: Computes the average of value in an attribute.
-MIN/MAX: Computes the min/max value in an attribute.
-COUNT: Computes the number of values in an
attribute(including duplicates unless they are explicitly
eliminated with DISTINCT)
Example TABLE
SUM()
AVG()
MIN() & MAX()
COUNT()
0RDER BY()
JOINS
• Join is a query which combine rows of two or more
tables.
• In a join-query, we need to provide a list of tables in
FROM Clause.
• The process of combining multiple tables in order to
retrieve data is called joining. For ex-
SELECT * FROM emp1, dept;

• Unrestricted join or Cartesian product of both the


tables gives all possible concatenations of all the rows
of both the tables.
EQUI JOIN AND NATURAL JOIN
To get the details about the departments and their in- charges,
query will be-

mysql> SELECT name, deptname from emp1, dept


where emp1.empcode=dept.deptic;

When both the tables have same field name, then to show a
field from particular table, use the following pattern to access a
field- <Table name>.<Field Name>
Ex- emp1.empcode
This is an example of Equi-
Join, in which columns are
compared for equality and it
is also an example of
Natural- Join, in which only
one of the identical columns

You might also like