+ All Categories
Home > Documents > Databases and Sql

Databases and Sql

Date post: 24-Feb-2016
Category:
Upload: tyson
View: 45 times
Download: 0 times
Share this document with a friend
Description:
Databases and Sql. Introduction . Relation: Relation means table(data is arranged in rows and columns) Domain : A domain is a pool of values appearing in given column. Eg : . Supplier # Domain (S001,S002,S003,SOO4). Tuple : The rows of a relation Attribute : The columns of a relation. - PowerPoint PPT Presentation
31
DATABASES AND SQL
Transcript
Page 1: Databases and  Sql

DATABASES AND SQL

Page 2: Databases and  Sql

Introduction • Relation: Relation means table(data is arranged in rows and columns)• Domain :A domain is a pool of values appearing in given column.Eg:

Sup# QtyS001 52S002 66S003 33S004 20

Supplier # Domain (S001,S002,S003,SOO4)

Page 3: Databases and  Sql

• Tuple : The rows of a relation• Attribute : The columns of a relation.

Attributes

Sup# QtyS001 52S002 66S003 33S004 20

Tuple

Page 4: Databases and  Sql

• Degree : Number of attributes(columns)• Cardinality: Number of tuples(rows)

Sup# QtyS001 52S002 66S003 33S004 20

Degree 2

Cardinality 4

Page 5: Databases and  Sql

Key• Primary Key: Set of one of one more attribute that can

uniquely identify tuples within a relation

• Candidate key: is one or more column in table that uniquely identify each row in a table (can act as primary key).

• Alternate Key: candidate key is not a primary key

Page 6: Databases and  Sql

Data types

SQL data types are classified into three • Numeric • String • Date

Page 7: Databases and  Sql

1. Numeric data type:

- Length of numeric data type 1-255 - Range decimal places ( 0 to 30) - decimal must be 2 less than length - 2 categories of integer data types are integer(whole numbers) data type TINYINT, SMALLINT, MEDIUMINT, INT, INTEGER, BIGINT• Floating point data types are FLOAT,DOUBLE, DOUBLEPRECISION, REAL, DECIMAL, DEC, • NUMERIC, FIXED

Page 8: Databases and  Sql

2. String data types

- used to store various types of data - four categories of string data types are CHARACTER, BINARY , TEXT and LIST commonly used data type is character• character is classified in two Char uses n byte storage (blank spaced filled with space) varchar uses less storage space

Page 9: Databases and  Sql

3. Date & Time

  -yyyy –mm-dd format categories of date & time data types DATE, TIME, DATETIME,YEAR, TIMESTAMP

Page 10: Databases and  Sql

SQL Statements

• Sql is made up of set of statements that define the structure of database, store and manage data within that structure and control access to data.

Sql statements are mainly divided in to three categories• DDL(Data Definition Language)• DML(Data Manipulation Language)• DCL(Data Control Language)

Page 11: Databases and  Sql

• DDL -( Data Definition Language ) DDL statements create, alter and delete data structure within table. DDL statements defines the type.Eg: create, alter, Drop

• DML – (Data Manipulation Language ) DML is concerned with data stored in a table.

Eg: Insert, update, delete and select.

• DCL – (Data Control Language) DCL statements controlling the access to database. Eg: grant and revoke.

Page 12: Databases and  Sql

CREATE TABLE command

 Create table <tablename> (<column name> <type> [<SIZE>][NOT NULL|NULL][DEFAULT <VALUE> ] [PRIMARY KEY(<column name>],[CHECK EXPRESSION][UNIQUE] ,..)

Page 13: Databases and  Sql

Examplecreate table student( rollno int primary key, column level definition name char (10) );

Page 14: Databases and  Sql

Create a table student using following information

create table student( Rollno int(4) Primary key , Name varchar(10) Total int(2)); • you can’t define a primary key on multiple columns at

the column level

Column name Data type Size constrain

Rollno Int 4 Primary key

Name Varchar 10  

Total Int 2  

       

Page 15: Databases and  Sql

INSERT Command

To insert row in tableSyntax 1 :INSERT INTO <table name> (<column name>, <column name><column name>..) VALUES (<value>,<value><value><value>..);Eg:Insert into student (rollno, name,total) values (12101,’arun’,400);

if column name are included value clause must include a value for each column 

Page 16: Databases and  Sql

Format 2

INSERT INTO <table name> VALUES (<value>,<value><value><value>..),VALUES (<value>,<value><value><value>..),VALUES (<value>,<value><value><value>..),

Insert into student values (12101,’arun’,400), values(12102,’anil’, 500);

 

Page 17: Databases and  Sql

INSERT INTO <table name>VALUES (<value>,<value><value><value>..),(<value>,<value><value><value>..),  Eg:

Insert into student values (12101,’arun’,400), (12102,’anil’, 500), (12103,’amit’,550), (12104,neem,450);

Page 18: Databases and  Sql

*Points to remember

inserting number : <value>inserting char : <’value’>inserting date :<’yyyy-mm-dd’)inserting null : NULLif column name are not included you must provide value

for every column in table

Page 19: Databases and  Sql

UPDATE Command

• To modify the existing row content UPDATE <table name> SET<column name = expression>[where <expression>]; where clause determines the which row in a table are updated.Eg : Update student set total =450 where rollno=12101; 

Page 20: Databases and  Sql

DELETE Command

To remove row /data from a table DELETE FROM <table name> [where expression];

Eg : delete from student where roll = 12102;  

Page 21: Databases and  Sql

SELECT Command

To retrieve data from MySql tables we can use SELECT statement SELECT {ALL | DISTINCT }{* | <column name > | <expression > [AS] <alias> } FROM {<table name>}{,[<table name>]}{…….}[WHERE <expression>][GROUP BY <column name>][HAVING <expression>][ORDER BY <column name [ASC|DESC]>]

Page 22: Databases and  Sql

Select command let you to make queries on the database

Form1: To retrive columnsSelect <colum name>[, colum name, colum name …] from <table name>;Eg : select rollno, name from student; Form 2: if you want to see the entire table. The (*) can be substituted . Select * from <table name>Eg: Select * from student;

Page 23: Databases and  Sql

Select with - DISTINCT clause or ALL

• To eliminate duplicates rows from result of a SELECT statement.

Form 3Select DISTINCT column name from <table name>Eg: Select distinct name from student;

Form 4Select ALL column name from <table name>Eg: Select ALL name from student;

Page 24: Databases and  Sql

Retrieving specific rows – where clause

Select <colum name>[, colum name, colum name …] from <table name> where <condition>;

Eg: select * from student where rollno=12101;

Relational operators: <(less than) ,>(Greater than),<=(less than or equal),>=(greater than or equal), =(Equal) ,!= or <>(Not equal).Logical operator : and, or not

Page 25: Databases and  Sql

Between clause – to set rangeSelect <colum name>[, colum name, colum name …] from <table name> [where column name between value1 and value 2 ; The above statement means Select <colum name>[, colum name, colum name …] from <table name> [where column name>= value1 and column name <= value 2 ;

Eg:select * from student where total between 400 and 500.select * from student where total >= 400 and total<= 500.

Page 26: Databases and  Sql

IN clause – to select values Select <colum name>[, colum name, colum name …] from <table name> [where column name IN( value1,value 2) ; The above statement means Select <colum name>[, colum name, colum name …] from <table name> [where column name= value1 or column name < value 2 ;

Eg:select * from student where total IN(400, 500).select * from student where total = 400 or total = 500.

Page 27: Databases and  Sql

Order by clause – for Sorting - Arrange rows in either ascending / descending orderSelect <column name>[, colum name, colum name …] from <table name> [where <condition> order by <column name>[asc/desc]]Default sorting ascending order(asc) Eg:select * from student where total >= 400 and total<= 500 order by name.

Page 28: Databases and  Sql

Group by – for grouping similar records

Select <column name>[, column name, column name …] from <table name>[ group by <column name> having <condition>]If you are using group by clause and you want to restrict rows use having clause instead of where. Eg:select * from student where group by total having total >= 400 and total<= 500.

Page 29: Databases and  Sql

Group functions

• MAX() -> Returns Maximum value from a given column• MIN() -> Returns Maximum value from a given column • AVG() -> Returns Average value from a given column • SUM()-> Returns Sum of values in a given column • COUNT() -> count number of rows (Excluding NULL) : COUNT(*) -> returns number of rows( including null)

Page 30: Databases and  Sql

Group function - Example• Select Sum(Total) from student;• Select Max(Total) from student;• Select Min(Total) from student;• Select Avg(Total) from student;• Select count() from student;• Select Count(*) from student;

Page 31: Databases and  Sql

THANK YOU


Recommended