Difference Between SQL and Mysql
Difference Between SQL and Mysql
————————–
Overview
iv) Database
iv) Database
> Each column contains a different type of attribute and each row
corresponds a single record.
Inserting:
insert into ECEsection(id,name,section,backlogs)values(1,'srinu','A',2)
select * from ECEsection
insert into ECEsection(id,name,section,backlogs)values(2,'mani','c',3)
insert into ECEsection(id,name,section,backlogs)values(4,'hari','b',1)
update ECEsection set name='nani' where id=2;
delete from ECEsection where id=1;
x) SQL Joins
The SQL Joins clause is used to combine records from two or more
tables in a database.
1) Inner Join
2) Left Join
3) Right Join
4) Full Join
1.CREATING TABLE:
CREATE TABLE tablename(sridevi)(id int,name varchar(),course varchar();
CREATE TABLE IF NOT EXISTS tablename()
● Before creating a table we should select database means in which
database the table should be created.
● For first time CREATE database databasename(xxxxx)
● To see all databases ……show databases then list will be displayed from
that select one
● Use database name ….then table will be created in that database.
● Which database we are using….select database();
● To see all created tables in database…..show tables;
● If you want to see the structure of the table …..DESCRIBE tablename;
2.INSERT DATA INTO TABLE:
● INSERT INTO tablename(xxxx) values(id,name,course)
● To see the inserted data
SELECT * from tablename;
● If you want to select only id or name or course then
SELECT id or name or course from tablename
We can insert data like this also
OR
● INSERT INTO tablename(xxxx) (id,name,course) values(10,sri,java)
SQL commands:
1. SELECT STATEMENT-----RETRIEVE DATA FROM MYSQL TABLES
SELECT * from tablename where id=xxx ORDER BY name asc or desc;
2. SELECT LIMIT STATEMENT
SELECT * from tablename where id=xxx ORDER BY name LIMIT 2;--2 will
display
SELECT COUNT(*) from tablename where id=1;
3. DELETE STATEMENT
DELETE * from tablename where name=’xxxx’ AND id=1;
4. DELETE LIMIT STATEMENT
DELETE * from tablename where id=xxx ORDER BY name LIMIT 1;--2 will
display
5. UPDATE STATEMENT
UPDATE columnname SET name='nani' where id=3;
MYSQL FUNCTIONS:
1. COUNT
Count gives number of records from a column
Ex…..if two columns id and name, there are 10 rows in id column and 15
rows in name column. So COUNT gives no.of records from a column
2. MIN
SELECT min(expression) from table where condition;
It gives min value(least number) from a column.
3. MAX
SELECT max(expression) from table where condition;
It gives max value (largest number) from a column.
4. SUM
SELECT sum (expression) from table where condition;
It gives addition of all records from a column.
5. AVERAGE
SELECT avg(expression) from table where condition;
It gives average value of a column.
MYSQL CLAUSES:
1. DISTINCT
It avoids duplicate values
Select distinct(column name id or name or etc) from table name where
condition;
2. WHERE
It is used to filter results from insert, update, select and delete
statements
Select distinct(column name id or name or etc) from table name where
condition;
3. FROM
Select distinct(column name id or name or etc) FROM table name
where condition;
4. ORDER BY
Select * from tablename where id=xxxx ORDER BY name asc or desc;
5. GROUP BY
Select exp1,exp2(id,name) from tablename GROUP BY exp(id or name)
6. HAVING
Select exp1,exp2(id,name) from tablename GROUP BY exp(id or
name) having id or name = xxxxxxxxxxx
MYSQL CONDITIONS:
1. AND CONDITION
select * from tablename where id=10 AND name='hari';
2. OR
select * from tablename where id=10 OR name='hari';
3. AND and OR
select * from tablename where (id=10 AND name='hari') OR id=1;
4. LIKE
select *from tablename where name like 'h%';
select *from tablename where name like '_ari%';
5. IN
Select * from tablename where name IN (‘xxxxx’,’xxxx’);
6. NOT IN
Select * from tablename where name NOT IN (‘xxxxx’,’xxxx’);
7. IS NULL
select * from tablename where name or xxxx is null;
8. IS NOT NULL
select * from tablename where name or xxxx is not null;
9. BETWEEN
select * from tablename where id or name between xxx and xxx;
10. EXISTS
JDBC-----sun microsytems
Java application-------------------------------------database
2. Jdbc API is divided into 2 types of packages
Java.sql
Javax.sql
These two contains lot of packages and classes
3. When ever java application interfaces with database , java
representation converts to query representations and when database
sends response to java application, query representations converts to
java representations. In order to do this process we have a process
called Jdbc driver software.
4. Every driver software must register with driver manager service.
5. In order to establish a connection between java application and
database the driver manager class is having a static method called get
connection method.
Drivermanager.getconnection()
Public class Drivermanager
{
Public static connection getconnection()
Getconnection method always ask for 3 parameters
MYSQL…….com.mysql.jdbc.driver------jdbc:mysql://hostname/dbname
ORACLE……com.jdbc.driver.oracleDriver---oracle driver class is available as a
part of jar files. Need to download ojdbc jar files
jdbc:oracle:thin:@hostname:portnumber:dbname
1. Statement(I)
Interface statement---whenever we want to execute all queries
independently as a new sql query
If you want to execute static sql statements then we go for statement object
{
}
Whenever we want to send sql queries using statement object the sql
command is compiled first and then executed and this statement object
doesn’t store compiled code. The command again compiled when it is send
to a database. This process kill our application performance. So this
statement object can transfer values to database but not binary data.
The queries coming from java application to database are executed by a
software called “Database Engine”
Database Engine executing queries follows below steps:
● Query Tokenisation---dividing query into number of pieces,each
piece is called a token
● Query parsing---used to check all query syntax and grammatical
mistakes. If all queries are ok means everthing is fine then
database engine forms a tree called Query tree
● Query optimization----It tries to find optimized way to execute
sql queries by following some algorithms.
● Query Execution----It executes optimized query
Disadvantage…..Every time it treats as a new query and it executes as a new
query and compiles everytime so database engine performs above steps
everytime. So our application performance comes down. To overcome this
we can go for prepared statement
Methods:
1.Execute method----for both select and nonselect
2. PreparedStatement(I)
When we want to execute sql statements many times. By using prepared
statement to send queries to database, database engine performs all 4 steps
for first time only so that application performance will be improved.
Sql queries send to database for precompilation process so the compiled
code will be stored in prepared statement. If youwant to execute the code
for next time it won’t go for compilation. One time activity is done.
JDBC API:
API is a collection of interfaces and classes
From java.sql-----package