37
Database

Java class 8

  • Upload
    edureka

  • View
    472

  • Download
    3

Embed Size (px)

DESCRIPTION

 

Citation preview

Page 1: Java class 8

Database

Page 2: Java class 8

What is Database ?

• Database is a collection of data in an organized way so that we can update, modify data present in database easily.

• Data storage in database is permanent.• Eg: Oracle, MySQL, MS-Access, DB2, Sybase

Page 3: Java class 8

How to store data in database?

• Data will stored in “Tables” in database in the form of “Records”.

• Table is a collection of relational data in 2D i.e in the form of rows and columns.

Page 4: Java class 8

What is SQL ?

• SQL – Structure Query Language• It is language used to access and modify the data

stored in database.• It uses statements to access the database.• SQL Statements can be divided into two types:

DMLDDL

Page 5: Java class 8

DDL(Data Definition Language)

• These statements are used for creation or deletion of database.

• The commands present in DDL are:CreateAlterDrop

Page 6: Java class 8

Create

• This command is used to create table or database.

• create database;//to create database

• create table table_name(column1_name datatype,column2_name datatype); // to create table

Page 7: Java class 8

Alter

• This command is used to rename a table or column name of an existing table.

• It can also be used to modify, add, drop a column/field present in an existing table.

• alter table table_name rename new_table_name; //to rename table

• alter table table_name rename column old_column_name to new_column_name; //to rename column

Page 8: Java class 8

Alter

• alter table table_name add column_name datatype; //to add a column to table

• alter table table_name modify column_name new_datatype; //to modify datatype of column of a table

• alter table table_name drop column column_name; //to drop a column of a table

Page 9: Java class 8

Drop

• This command is used to remove an object(value).

• We can not get the table or values in it once the table is dropped.

• drop table table_name; //to drop a table

Page 10: Java class 8

DML(Data Manipulation Language)

• These statements are used for managing the data in database.

• The commands present in DDL are:InsertUpdateDeleteSelect

Page 11: Java class 8

Insert

• This command is used to enter the records into the table.

• insert into table_name values(‘value_col_1’, ‘value_col_2’); //to insert values into table

Page 12: Java class 8

Update

• This command is used to update the value of a record or column of a table.

• We use condition to know where the updation is to be made.

• update table set column_name=“value” where some_column_name = “value_of_some_column”; //to update the column where it satisfies condition

Page 13: Java class 8

Delete

• This command is used to delete the records (one or many) from a table by specifying some condition where to delete the record.

• delete from table_name where column_name = “value”;

//to delete record where it satisfies given condition

Page 14: Java class 8

select

• Select command is used to retrieve the records from the table.

• select column1_name, column2_name,…………. from table_name; //to select the specific columns from table

• select * from table_name; // to retrieve all records present in table

Page 15: Java class 8

Integrity Constraints

• Not Null• Unique• Primary Key• Foreign Key• Check

Page 16: Java class 8

Not Null

• It is a constraint that ensures that every row is filled with some value for the column which has been specified as “not null”.

• create table table_name(column1_name datatype NOT NULL,column2_name datatype );

Page 17: Java class 8

Unique

• It is a constraint used for column of a table so that rows of that column should be unique.

• It allows null value also.

• create table table_name(column1_name datatype UNIQUE,

column2_name datatype);

Page 18: Java class 8

Primary Key

• It is a constraint used for a column of a table so that we can identify a row in a table uniquely.

• create table table_name(• column1_name datatype PRIMARY KEY,• column2_name datatype);

Page 19: Java class 8

Foreign key

• It is a constraint used to establish a relationship between two columns in the same or different tables.

• For a column to be defined as a Foreign Key, it should be a defined as a Primary Key in the table which it is referring.

• One or more columns can be defined as Foreign key.• This constraint identifies any column referencing the

PRIMARY KEY in another table.

Page 20: Java class 8

SQL clauses

• select• from• where• update

• order by• group by• having

Page 21: Java class 8

Order by

• This clause is used the values of a column in ascending or descending order.

• select empname from emp1order by empname;

Page 22: Java class 8

Group by

• This clause is used to collect data across multiple records and group results by one or more columns.

• select department from Student where subject1> 20group by department;

Page 23: Java class 8

Having

• This clause is used in combination with sql group by clause.

• It is used to filter the records that a sql group by returns.

• select max(subject1) from Student group by department having min(subject1)>20;

Page 24: Java class 8

Basic Functions

• COUNT() – to count the number of records satisfy condition.

• MAX()- to find the maximum value of a column of all the records present in table.

• MIN()- to find the minimum value of a column of all the records present in table.

• AVG()- to find the average value of a column of all the records present in table.

• SUM()- to find the sum of all the values of a column in a table.

Page 25: Java class 8

Basic Functions

• AVG()- to find the average value of a column of all the records present in table.

• SUM()- to find the sum of all the values of a column in a table.

• Distinct() - to find the distinct record of a table

Page 26: Java class 8

Joins

• Join is used to retrieve data from two or more database tables.

• Joins are of four types:Cross join- This join returns the combination of

each row of the first table with every column of second table.

Inner join-This join will return the rows from both the tables when it satisfies the given condition.

Page 27: Java class 8

Joins

Left Outer join –suppose if we have two tables. One on the left and the other on right side.

When left outer join is applied on these two tables it returns all records of the left side table even if no matching rows are found in right side table

Right Outer join- it is converse of left outer join. It returns all the rows of the right side table even if no matching rows are found in left side table.

Page 28: Java class 8

JDBC

Page 29: Java class 8

JDBC

• JDBC-Java Database Connectivity.• JDBC is an API that helps us to connect our

java program to the database and do the manipulation of the data through java program.

Page 30: Java class 8

How to write a program in jdbc?

• Register the Driver• Connecting to database• Create SQL statement in java• Execute SQL statements• Retrieve Result• Close Connection

Page 31: Java class 8

JDBC program

public class JDBCProgram{public static void main(String[] args) {try { Class.forName(“sun.jdbc.odbc.JdbcOdbcDriver”);//REGISTERING DATABASE

Connection con = DriverManager.getConnection("jdbc:odbc:dsn", "",""); //CONNECTING DATABASE Statement st=con.createStatement();//CREATE STATEMENT st.execute("create table Employee(empid integer, empname varchar(50))");//EXECUTING STATEMENT st.close();// CLOSING STATEMENT con.close();//CLOSING CONNECTION }catch (Exception err) { System.out.println( "Error: " + err ); }}}

Page 32: Java class 8

How to insert values into table using jdbc?

• To insert the values into the table we use prepared statement.

• PreparedStatement ps = con.prepareStatement("insert into Table_name(col1,col2)values(?,?)");//creating preparedstatement

ps.setInt(1,value_col1);//inserting the value in column1 ps.setString(2, “value_col2”); //inserting the value in column2

ps.executeUpdate();//to execute the prepared statement

Page 33: Java class 8

How to update values in table using jdbc?

• To update the values into the table also we use prepared statement.

• PreparedStatement ps = con.prepareStatement(“update Table_name set col1=?,col2=? where col1=?);//creating preparedstatement

ps.setInt(1,new_value_col1);//updating new value in column1 ps.setString(2, “value_col2”); //updating the value in column2 ps.setInt(1,old_value_col1);//getting old value of column1 ps.executeUpdate();//to execute the prepared statement

Page 34: Java class 8

How to delete record in table using jdbc?

• To delete the record in the table also we use prepared statement.

• PreparedStatement ps = con.prepareStatement(“delete from Table_name where col1=?); //creating preparedstatement

ps.setInt(1,old_value_col1);//getting value of column1 ps.executeUpdate();//to execute the prepared statement

Page 35: Java class 8

How to retrieve record from table using jdbc?

• To store the retrieved records we should use ResultSet.

• ResultSet rs = st.executeQuery("select * from Employee"); //using result set to store the result

while(rs.next()){System.out.println(rs.getInt(1)); //to retrieve value of col1System.out.println(rs.getString(2)); //to retrieve value of col2}

Page 36: Java class 8

•Q& A..?

Page 37: Java class 8

Thanks..!