DATA TYPES
There are following datatypes in mysql:-
1. Char:-
This datatype is used to store character string values of fixed length.
Specification - Char (Size)
The size in ( ) determines the no. of characters can hold. The maximum size is 255
bytes.
2. Varchar:-
This datatype is used to store variable length alphanumeric data. The maximum size is 65535
bytes. The processing of varchar() is slower than char.
3. Float/decimal:
Specification:- float(p,s) decimal(p,s)
4. Date/datetime:- Specification:-Date
This data type is used to represent date . The standard format is YYYY-MM-DD.
Specification: DATETIME
Format YYYY-MM-DD hh:mm:ss
Working with table
1. Create table command:-
The create table command is used to create a table. This command defines each column of the
table uniquely. Each column generally has 3 attribute and they are column name, column datatype
and size.
Syntax:-
Create table <table name> (<Column name1> <data type> (<size>),
<Column name2> <data type> (<size>), …………………………
………………………………………………………………………..
………………………………………………………………………..
<Column name n> <data type> (<size>)
);
Eg:- Emp
Emp_id E_name E_address Dob
Create table Emp (Emp_id int,
E_name varchar(15),
E_address varchar(15),
Dob date);
*Describe command (Desc):- To Show the structure of a table.
Ex. Desc Emp;
2. Insertion of data into table:-
A record is inserted into a table by using the insert command.
Syntax:-
Insert into <table name> (<column name1>,<column name2>) values (<value1>,<value2>);
Eg.:- 1. Insert into emp(Emp_id,E_name)values(1,’Ravi’);
2. Insert into emp values (1, ‘Ajay’, ‘Vns’, ‘1990-01-23’);
3. Insert into emp(emp_id,e_name,e_address,DOB)
values (3, ‘Rakesh’, ‘Lko’, ‘1990-01-23’);
3. Viewing data in the table:-
Once data has been inserted into a table the next requirement is to view the inserted data. For this
purpose we use select command. This command is use to retrieve the records from one or more
tables.
(a)To view all rows and all columns:-
Syntax: - Select * from <table name>;
Eg: - Select * from emp;
(b)To view selected column and all rows:-
Syntax: - Select <column name1>,<column name2> from <table name>;
Eg: - Select id, name from emp;
(c)To view selected rows and all columns:-
Syntax: - Select * from <table name> where <condition>;
Eg: - Select * from emp where address=’VNS’;
(d)To view selected rows and selected column:- Syntax:-
Select <column name1>,<column name2> from <table name> where <condition>;
Eg: Select id, name from emp where address= ‘VNS’;
(e)Sorting data in a table:-
We can retrieve records from the table in sorted order(either in ascending order or descending
order) using order by clause.
Syntax: Select * from <table name> order by <column name>[<sort order>];
Eg: (i) Select * from emp order by name;
(ii) Select * from emp order by name desc;
Note: - By default ascending order, use Desc for descending order.
Distinct Keyword:
It is used with select statement to eliminate duplicate rows.
Select Distinct e_address from emp;
e_address
VNS
LKO
Logical Operator
There are following logical operator:-
1.OR operator:-
The or operator returns a true value if either of the specified condition is true.
Emp
Id Name Address Salary
1 Ravi LKO 2000
2 Ajay ALD 3000
3 Amit LKO 5000
4 Anil VNS 8000
5 Rajesh LKO 20,000
Eg. Select * from emp where salary<5000 OR address= ‘VNS’;
Id Name Address Salary
1 Ravi LKO 2000
2 Ajay ALD 3000
4 Anil VNS 8000
2. AND Operator:-
The AND operator return the true value if all the specified condition are true. The result of query
display only those records where all of the condition specified using the And operator are satisfied.
\ Select * from emp where salary <8000 AND address = ‘LKO’;
Id Name Address Salary
1 Ravi LKO 2000
3 Amit LKO 5000
3.Not Operator:- The Not operator returns a TRUE value when the specified condition is false
and vice versa.If we use Not operator with Condition then the oracle engine will process rows in a
table and display only those records that do not satisfy the condition specified. Select * from emp
where NOT(Salary < 8000 and address = ‘Lko’);
Id Name Address Salary
2 Ajay ALD 3000
4 Anil VNS 8000
5 Rajesh LKO 20000
Use of BETWEEN
In order to select data that is within a range of values, the “Between” operator is used. The Between
operator allows the selection of rows that contain values within a specified lower and upper limit.
The two values in between the range must be linked with the keyword “AND”
(i) Select * from emp where salary between 2000 and 8000;
Id Name Address Salary
1 Ravi LKO 2000
2 Ajay ALD 3000
3 Amit LKO 5000
4 Anil VNS 8000
(ii)Select * from emp where salary not between 2000 and 8000;
Id Name Address Salary
5 Rajesh LKO 20000
Pattern Matching
1. Use of Like:- The like operator allows comparison of one string value with another string
value which is not identical. This is achieved by using wild card characters. There are two wild
card characters.
(i)% → To match zero or more character
_ (underscore) → To match exactly one character
Eg. (i) If we want to get the records of those employees whose names begin with character A, we
can use following query:-
Select * from emp where name like ‘A%’;
(ii)If we want to match the last two character then
Select * from emp where name like ‘%sh’;
(iii)If we want to match 2nd character from begining then
Select * from emp where name like ‘_a%’;
(iv)If we want to match 2nd character from last then,
Select * from emp where name like ‘%s_’;
2. Use of IN: The equality operator (=) compares a single value to another single value. In case a
value needs to be compared to a list of values IN operator can be used.
(i)Select * from emp where address IN (‘VNS’, ‘LKO’, ‘ALD’);
(ii)Select * from emp where address NOT IN (‘VNS’,’LKO’,”ALD’);
Data constraints
The rules which are enforced on data in a table are called constraints. Once data constraints are
part of a table column construct, the oracle database engine checks the data being entered into a
table column against the data constraints. If the data passes this check, it is stored in the table
column otherwise rejected. Even if a single column of the record fails a constraint, the entire record
is rejected and not stored in the table.
Defining different constraints on the table:-
1. Primary key constraints:-
A primary key is one or more columns in a table used to unequally identify each record in a table.
Primary key will not allow duplicate values and null values. A table can have only one primary
key. A single column primary key is called a simple key while a multicolumn primary key is
called composite key. A primary key can be defined in either a create table statement or an alter
table statement.
(i) Primary key defined at column level:
Syntax:- <column name> <datatype>(<size>) primary key Eg:
Create table emp (id int primary key,
Name varchar(20),
age int
);
(ii)Primary key defined at table level:-
Syntax: - Primary key(<column name>,<column name>) Eg:
Create table dep (id int ,
dname varchar(20),
age int,
primary key(id, dname)
);
2. Foreign key constraints:-
Foreign keys represent relationships between tables. A foreign key is a column (or a group of
columns) whose values are derived from the primary key or unique key of some other table. The
table in which the foreign key is defined is called a foreign table or detail table. The table that
defines the primary key or unique key and is referenced by the foreign key is called the primary
table or master table. A foreign key can be defined either in create table statement or an alter table
statement.
(i)Foreign key defined at column level:
Syntax: - <column name> <datatype>(<size>) references<master table name>[(<column name>)]
Eg: Create table dep (did int references emp (id) ,
dname varchar(20),
dage int
);
Emp Dep
Id Name Age Did Dname Dage
1 Ravi 50 1 Sunita 40
2 Rajesh 40 1 Sonu 10
3 Ramesh 40
2 Kavita 35
2 Monu 5
(ii)Foreign key defined at table level:-
Syntax:- Foreign key (<column name>) references <master table name>[(<column name>)]
Eg:- Create table dep (did number(3), dname varchar2(15), dage number(2), foreing key (did)
references emp(id) );
3. Unique key constraints:-
If we want to make a column or more than one columns unique other than primary
key we use the unique constraint. The unique constraint allows NULL values.
(i)Unique constraint defined at column level:
Syntax: <column name> <data type>(<size>) unique;
Eg:- create table emp(id int primary key,
name varchar(10),
mobile varchar(10) unique,
Address varchar(10)
);
(ii)Unique constraint defined at table level:-
Syntax:-Unique(<column name>,<column name>)
Eg:- create table emp(id int primary key,
name varchar(10),
mobile varchar(10),
address varchar(10),
unique(mobile)
);
.
(i)Check constraints defined at column level:-
Syntax: <column name> <data type>(<size>) check(<logical expression>)
Eg: Create table emp(id int primary key,
name varchar(10),
age int check (age>=18 and age<=60),
address varchar(10)
);
(ii)Check constraints defined at table level:-
Syntax:- check(<logical expression>) Eg:
Create table emp(id int primary key,
name varchar(10),
age int,
address varchar(10),
check (age>=18 and
address=’vns’)
);
Modifying the structure of table using alter table command
With the help of alter table command we can add or delete columns, change the datatype
and size of existing column, renaming columns, dropping columns and adding or dropping
constraints.
(a)Adding new column:-
Syntax:- Alter table <table name> add <column name> <data type>(<size>);
Eg:- Alter table emp add phone varchar(10);
(b)Modifying Column:-
Syntax:Alter table <table name> modify <column name> <data type>(<size>);
Eg:- Alter table emp modify name varchar(15);
(c)Dropping column:-
Syntax:- Alter table <table name> drop column <column name>;
Eg:- Alter table emp drop column phone;
(d)Renaming column:-
Syntax:- Alter table <table name> change <old column name> <new column definition>;
Eg:- Alter table emp change name emp_name varchar(16);
(e)Defining integrity constraints through alter table command:-
Integrity constraint can be defined using the constraint clauses in the alter table
command. Oracle will not allow constraint defined using the alter table command to be apply to
the table if data previously placed in the table violate such constraints.
*Constraints for primary key:
Syntax:- Alter table <table name> add constraint <constraint name> Primary key(<column name>)
Eg:- (i) Alter table emp add constraint con_pk primary key(emp_id);
ii) Alter table emp add primary key(emp_id); *Constraints for forign
key:
Syntax: Alter table <table name> add constraint<constraint name> foreign key(<column name>)
references <parent table name> (<column name>);
Eg: Alter table dep add constraint con_fk foreign key(did) references emp(emp_id);
*Constraints for unique Key:-
Syntax:- Alter table <table name> add constraint <constraint name> unique(<column name>); Eg:
Alter table emp add constraint con_Uk unique(mobile);
*Constraints for Check:-
Syntax:- Alter table <table name> add constraint <constraint name) check(<logical expression>)
Eg: Alter table emp add constraint chk_age check (age>=18and age<=60);
Dropping Integrity Constraint through alter table command:- Syntax:
Alter table <table name> drop constraint<constraint name>;
Eg: Alter table emp drop constraint chk_age;
Dropping table
Syntax: Drop table <table name>;
Eg.: Drop table Emp;
Manipulating Data in SQL
Different Data manipulation operations are:
1. Select
2. Insert
3. Update
4. Delete
Updating contents of a table
The update command is used to change or modify data values in a table. Update command
can either update all thee rows of a table or selected records of the table.
(a)Updating all the records:-
Syntax:- Update <table name> set <column
name>=<value>,<column name>=<value>;
Eg: Update emp
set salary= salary+1000;
(b)Updating selected record:-
Syntax:- Update <table name> set <column
name>=<value>,<column name>=<value> where <condition>;
Eg1: Update emp
set salary = salary +1000
where salary <4000;
Eg2:Update emp
set salary = salary +1000
where id =2;
Delete operation
The delete command deletes records from the table that satisfies the condition
provided by its where clause and returns the number of records deleted. With the delete
command either we can delete all the records from a table or selected records from the table.
(a)To delete all the record:-
Delete from <table name>;
Eg. Delete from emp;
Note:- Difference between delete operation and truncate table operation is that truncate table
operation drops and recreates the table which is much faster than deleting rows one by one. In
addition to this in case of truncate table the number of deleted rows is not returned.
(b)To delete selected records:-
Delete from <table name> Where <condition>;
Delete from emp where salary<5000;