Upload
edureka
View
472
Download
3
Tags:
Embed Size (px)
DESCRIPTION
Citation preview
Database
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
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.
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
DDL(Data Definition Language)
• These statements are used for creation or deletion of database.
• The commands present in DDL are:CreateAlterDrop
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
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
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
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
DML(Data Manipulation Language)
• These statements are used for managing the data in database.
• The commands present in DDL are:InsertUpdateDeleteSelect
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
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
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
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
Integrity Constraints
• Not Null• Unique• Primary Key• Foreign Key• Check
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 );
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);
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);
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.
SQL clauses
• select• from• where• update
• order by• group by• having
Order by
• This clause is used the values of a column in ascending or descending order.
• select empname from emp1order by empname;
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;
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;
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.
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
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.
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.
JDBC
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.
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
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 ); }}}
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
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
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
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}
•Q& A..?
Thanks..!