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

Difference Between SQL and Mysql

MySQL is a relational database management system (RDBMS) that uses Structured Query Language (SQL) to store, retrieve, modify and administrate a database. Some key differences between SQL and MySQL include: - SQL is a query language while MySQL is a database software that uses the SQL language. - MySQL offers additional tools like MySQL Workbench for database design and development. - MySQL provides frequent updates and is easier to install than SQL alone.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
49 views

Difference Between SQL and Mysql

MySQL is a relational database management system (RDBMS) that uses Structured Query Language (SQL) to store, retrieve, modify and administrate a database. Some key differences between SQL and MySQL include: - SQL is a query language while MySQL is a database software that uses the SQL language. - MySQL offers additional tools like MySQL Workbench for database design and development. - MySQL provides frequent updates and is easier to install than SQL alone.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 18

MYSQL

Difference between SQL and MySQL

Parameter SQL MYSQL

SQL is a Structured MySQL is an RDBMS to


Definition Query Language. It is store, retrieve, modify and
useful to manage administrate a database
relational databases. using SQL.
You need to learn the It is readily available
Complexity SQL language to use it through download and
effectively. installation.
MySQL is database
SQL is a query software. It used "SQL"
Type
language. language to query the
database.
MySQL offers an integrated
Support for SQL does not provide tool called 'MySQL
connector connectors. workbench' to design and
develop databases.

Allows data handling,


To query and operate
Purpose storing, modifying, deleting
database system.
in a tabular format.

SQL code and


commands are used in
MYSQL is used as an
Usage various DBMS and
RDBMS database.
RDMS systems
including MYSQL.
The language is fixed,
Updates and command remains Get the frequent updates
the same.
SQL (Structured Query Language) is a standard language for accessing
databases.

• SQL is used to Create New Databases

• SQL is used to Create New Tables in a Database

• SQL is used to Insert records in a Database

• SQL is used to Update records in a Database

• SQL is used to Delete records in a Database

• SQL is used to Retrieve data from a Database

• SQL is used to execute queries against a Database

• SQL can set permissions on tables, procedures and views

• SQL is used to Create stored procedures in a Database

• SQL is used to Create views in a Database

Database Engine is required to practice / use SQL, we can use any


Database engine like Oracle, MS SQL Server or MySQL etc…

————————–

Overview

i) Database Management System

ii) Database Engine

iii) Database Server

iv) Database

v) Table and Record

i) Database Management System

> Database Management System is designed to allow the definition,


creation, querying, update and administration of Databases

> It is a Computer Software Application that interacts with the user,


other applications, and the database itself to capture and analyze the
data

ii) Database Engine


Software that stores and retrieves data in a database. It may be self-
contained program or the part of DBMS that performs the storage and
retrieval operations.

iii) Database Server

Database Management Systems provide Database server functionality,


database server provides database services to other computer programs
or computers.

iv) Database

> A Database is a Systematic collection of data

> Databases support storage and manipulations of data

> Databases make data management easy

v) Table and Record

> A Table is a predefined format of rows and columns that define an


entity

> Each column contains a different type of attribute and each row
corresponds a single record.

MYSQL SERVER STUDIO-----create database databasename and


then use that database….by default is should be in MASTER database ,when
we write statement as USE databasename then shifts to that database.
Create database srideviDB1
Create table studenttable(id int,name char(20),course varchar(30))

Select * from studenttable


1.view table structure
Select * from INFORMATION_SCHEMA.COLUMNS where TABLE_NAME =
'studenttable'

2. Modify table name


EXEC sp_rename 'studenttable','newrecords'
Select * from newrecords

create database DB1


use DB1
create table facultylist(id int,name char(200),department varchar(70))
Select * from facultylist
Select * from INFORMATION_SCHEMA.COLUMNS where TABLE_NAME =
'facultylist'
EXEC sp_rename 'facultylist','professorlist'
Select * from professorlist
Select * from INFORMATION_SCHEMA.COLUMNS where TABLE_NAME =
'professorlist'
Alter table professorlist add salary char(80)
Select * from INFORMATION_SCHEMA.COLUMNS where TABLE_NAME =
'professorlist'
Alter table professorlist alter column name varchar(300)
Select * from INFORMATION_SCHEMA.COLUMNS where TABLE_NAME =
'professorlist'
Alter table professorlist drop column department
Select * from INFORMATION_SCHEMA.COLUMNS where TABLE_NAME =
'professorlist'
Truncate table professorlist
Drop table professorlist

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;

In MySQL we have to specify what type of data to be used.


1. Numeric data types
2. String data types
3. Date and Time datatypes
Numeric---int,smallint,mediumint,bigint,float,double,decimal
Date and Time---DateTime ---yyyy—mm—dd hh-mm—ss
String----char(1-255),varchar()
SQL commands:
1.DDL---Data Definition Language---CREATE,DROP,ALTER,RENAME,TRUNCATE
2.DML---Data Manipulation Language---SELECT,INSERT,UPDATE,DELETE,LOCK
TABLE
3.DCL----Data Control Language---GRANT,REVOKE
4.TCL---Transversal Control Language----COMMIT,ROLLBACK,SAVEPOINT,SET
TRANSACTION

x) SQL Joins

The SQL Joins clause is used to combine records from two or more
tables in a database.

Different SQL Joins:

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;

OTHER IMP COMMANDS IN SQL:


1. ALTER-----ADD,MODIFY,DROP
ADD----ALTER TABLE tablename ADD columnname1(varchar),
columnname1(varchar);
MODIFY---- ALTER TABLE tablename MODIFY columnname3(varchar);
DROP COLUMN----ALTER TABLE tablename DROP COLUMN
cloumnname(varchar);
RENAME---ALTER TABLE tablename CHANGE COLUMN name other
name(varchar);
RENAME TABLE----ALTER TABLE tablename RENAME TO new tablename;
2. DROP TABLE----DROP TABLE tablename;
It completely removes table data along with structure
3. TRUNCATE-----TRUNCATE TABLE tablename;
It removes table data only but table structure exists
SQL constraints:
MYSQL constraints are used to define rules to allow or restrict the values in
columns.
1. NOT NULL----Column doesn’t contain any null value
2. UNIQUE----Column doesn’t allow duplicate values. contains only unique
values. It allows null value.
3. CHECK-----it checks whether the value is valid or not from logical exps
4. PRIMARY KEY---- Column doesn’t allow duplicate values and null values
both. In every table one column must and should declare as a primary key.
● A table can accept one primary key.
● One primary key is allowed at column level.
● Primary key as a combination of columns is allowed.
Ex……..id int unique ,name varchar(50) primary key,course varchar(50)
primary key----------------it won’t alllow
We can specify primary key with combination of columns
Ex……primary key(id,name);-----it allow
Example……..CREATE TABLE IF NOT EXISTS sqlbasics(id int PRIMARY
KEY,name varchar(100) NOT NULL,course varchar(100) UNIQUE);
insert into tablename(name,course) values('ravi','ece');----id is not mentioned

mysql> insert into sqlbasics values(10,'mani','java with selenium');


ERROR 1062 (23000): Duplicate entry '10' for key 'PRIMARY'
mysql> insert into sqlbasics values(11,'mani','java with selenium');
ERROR 1062 (23000): Duplicate entry 'java with selenium' for key 'course'
mysql> insert into sqlbasics values(11,'mani','java');
Query OK, 1 row affected

CREATE TABLE teaching(id int,name varchar(200),primary key(id,name));


insert into teaching values(544,'hari');
Query OK, 1 row affected (0.21 sec)

mysql> insert into teaching values(544,'hari');


ERROR 1062 (23000): Duplicate entry '544-hari' for key 'PRIMARY'
mysql> insert into teaching values(544,'hariom');
Query OK, 1 row affected (0.15 sec)

4. FOREIGN KEY-----links two or more tables means there is a common


column between two tables.

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

SELECT COUNT(expression) from table where condition;


● Count function includes NOT NULL values but not null values.

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 WITH MYSQL COMMANDS:


JDBC-----JAVA DATBASE CONNECTIVITY------------API
JDBC is a collection of classes and interfaces.
1. Jdbc allows java applications to connect with a database

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

Public static connection getconnection(string url(database url that is where


database server is running/local host),string username(database
username),string pwd(database pwd)
6. Whenever we call getconnection() method, it establishes a connection
between java application and database and it returns that established
connection in the form of connection object.

STANDARD STEPS TO CONNECT WITH DATABASE USING JDBC:


1. Load the driver class and register with driver manager service
If the class is implementing java.sql.Driver(I) then that class is driver
class.
Type4 driver: oracleDriver
Public class oracleDriver implements Driver(I)
Whenever we want to load a class into java application then we have a class
called ‘Class’ and this class has a method
Class.forname(“oracle.jdbc.driver.oracledriver”)
---------Loading the driver class
oracleDriver driver = new oracleDriver()
DriverManager.register(driver);
Public class oracleDriver implements Driver(I)
{
static
{
oracleDriver driver = new oracleDriver()
try
{
DriverManager.re
gister(driver);
}
Catch (SQL exception e)
{
}

2. Establish the connection between java application and database


Public class DriverManager
{
Public static connection getconnection(string url,string username,string pwd)
{
Connection con=Driverconnect();---getconnection has internal method called
Driverconnect
Whenever jvm executed getconnection method it internally executes
Driverconnect method
:;
return con;--------------returns connection object
}
Connection connection =Driver Manager.getconnection ----it establishes
socket connection between java application and database (“jdbc(main
protocol):odbcthin:@hostname:1521(sub protocol):dbname”)

3. Create statement/prepared statement/callable statement object

Public statement createstatement();


Statement statement = connection.create statement();
Statement----returning implementation class of that object
System.out.pritnln(statement.getclass().getname();
Whenever we call create statement then we will get statement object
4. Send and execute queries
● Public ResultSet Execute Query(string sql);
perform select type operations(select * from tablename)
string sql = select * from tablename:
Resultset resultset = statement.executequery(sql);
Resultset----object holds multiple records that are fetched from database
● Public int executeUpdate(string sql);
perform non-select type operations(create,update,insert,delete,alter)
string sql = insert into tablename values(xxxx)
statement.executeupdate(sql);
● Public boolean execute(string sql);
Perform both select and nonselect type of operations
Whenever we perform select oprations this execute method True and
when it performs nonselect operations this method execute false.
Boolean b= statement.execute(“select * from tablename”);//true
Boolean b = statement.execute(“insert into tablename
values(xxxx);//false
5. Gather and process the results
Results we see in resultset interface
While (resultset.next())---------it iterates the data
6. Close the connections(connection statement)
Connection.close();
Statement.close();

RDBMS jdbc driver name URL

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

we need to download Mysql connector

TO SEND SQL QUERIES TO DATABASE :


By using following interfaces we can send sql queries to database and
retrieve the data from database.

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.

Prepare statement object allows us to send argument inputs at run time


Interface prepared statement extends statement
{
}
3. Callablestatement(I)
When we want to access stored procedures and functions from database
Interface callablestatement extends preparedstatement
{
}

JDBC API:
API is a collection of interfaces and classes
From java.sql-----package

You might also like