Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

ELEMENTS in SQL-1

Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

ELEMENTS in SQL

1. Literals (Means value)


a) Numeric: Maximum up to 53 digits including decimal places
b) Text: Maximum up to 4000 bytes can be stored
2. Datatypes
a) Numeric
DATATYPE DESCRIPTION
To store an integer value.
int/integer/int(m)/integer(m) m->Maximum possible length(no. of digits).

To store values having decimal.


Float/float(m,d) m-> Maximum possible length including decimal
d->Fixed no. of decimal places
Eg: m=5, d=2 [m=5-2=3, d=2
Decimal and double works in the exact way as float/float (m,d) works

b) Date & Time


Datatype FORMAT EXAMPLE
Date YYYY-MM-DD 2011-08-12
Time HH:MM:SS 07:12:55
Datetime YYYY-MM-DD HH:MM:SS 2011-08-12 07:12:55
Timestamp YYYYMMDDHHMMSS 20121231080944
Year(m) YY 12
m = 2 OR 4 YYYY 2012

c) String/Text
DATATYPE DESCRIPTION
It is of fixed length, used to store text. It has a
char/char(m) default value of 1. m--> represents the maximum
allowed length of the text.
It is of variable length, used to store text. It doesn’t
varchar(m) have any default value. m--> represents the
maximum allowed length of the text.
BLOB It stands for Binary Large Objects, used to store
large binary files or images.
It provides a list of option from which one value
Enum has to be picked up. For eg:
Section enum( ‘A’ , ‘B’ , ‘C’ )
3. Null Values:

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.

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!

4. Comments
a) Single Line commenting
Two ways to perform single line commenting:
i) -- followed by text
ii) # followed by text
b) Multiline commenting
/* anyText */
****************************************************************
MySQL statements (Broad categorization of commands)
1. Data Definition Language (DDL)
2. Data Manipulation Language (DML)
3. Transaction Control Language (TCL)
4. Session Control Commands
5. System Control Commands
Types of Commands

DDL:
Purpose:

a) Create: It is used to create a database, table and view.


b) Alter: It is used to add a column or change datatype of a column or to
change name of a column or to drop(delete) a column.
c) Drop: To permanently remove a table.
Syntax:

CREATE DATABASE DBNAME;


CREATE TABLE TN( ATT1 DT,
ATT2 DT,
------------) ;
CREATE VIEW VIEWNAME
AS SELECT _______________________ FROM TN CLAUSE ;
Optional

ALTER TABLE TN ADD ATT DT ;


ALTER TABLE TN MODIFY ATT NewDT ;
ALTER TABLE TN CHANGE OldName NewName NewDT ;
ALTER TABLE TN DROP ATT;

DROP DATABASE DbNAME ;


DROP TABLE TN ;
DROP VIEW ViewName ;
DML:
Purpose:

a) Insert: It is used to insert one record at a time.


b) Delete: It is used to delete one or more record at a time.
c) Update: It is used to update one or more record at a time.
d) Select: It is used to display the required attributes according to a
condition.
Syntax:

INSERT INTO TN VALUES (VAL1, VAL2,……………………..) ;


DELETE FROM TN CLAUSE ;
Optional
UPDATE TN SET ATT = NewValue CLAUSE ;
Optional
SELECT * FROM TN CLAUSE ;
Optional
SELECT ATT1, ATT2,…….. FROM TN CLAUSE ;
Optional
****************************************************************
CLAUSE

1. Where
Sub-clauses
a) Relational Operator: Used to write conditions based on:
➢ < >= <= != = is null
For eg: where salary > 10000 ;
b) Logical operator: Used to write when more than one condition is there base on:
And Or
For eg:
Where salary > 10000 and salary<35000
c) IN : used to write a condition when two or more values are from the same
attribute.
For eg: where att in ( val1, val2, ….. ) ;
d) Between: Used to write a condition for a range of values.
For eg:
Where Fees between 15000 and 44000 ;
e) Like: Used to perform search operation by using pattern.
For eg:
Name starts with A
Where name like ‘A%’ ;
Name ends with B
Where name like ‘%B’ ;
Name contains S
Where name like ‘ % S % ’ ;
3rd Letter of Name is A
Where name like ‘_ _ A % ’ ;
2nd Last letter of Name is A
Where name like ‘ A_ % ’ ;

2. Order by: Used to arrange records in ascending or descending order.


For eg:
Order by att asc ; #ascending order
Order by att desc ; #descending order
3. Group by: Used to display data in a group format.
Syntax:
Group by att ;
Having
Group by att having aggFn(att) RelOp value ;

You might also like