Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 20

Unit -2

SQL Data types and Schemas – Accessing SQL


from a Programming Language

Lecture 12

21BCT3DA - Discipline Specific Core - RDBMS | ODD 2022-23


Review of Previous Class Topic

• Modification of Database – Join Expressions –


Views – Integrity Constraints

21BCT3DA - Discipline Specific Core - RDBMS | ODD 2022-23


Topic Outcome

• SQL Data types and Schemas – Accessing SQL


from a Programming Language

21BCT3DA - Discipline Specific Core - RDBMS | ODD 2022-23


SQL Data types and Schemas

Date and Time Types in SQL : the SQL standard supports


several data types relating to dates and times

date: A calendar date containing a (four-digit) year, month,


and day of the month.

time: The time of day, in hours, minutes, and seconds.

timestamp: A combination of date and time.

21BCT3DA - Discipline Specific Core - RDBMS | ODD 2022-23


Accessing SQL From a Programming
Language

Dynamic SQL: A general-purpose program can connect to and


communicate with a database server using a collection of
functions (for procedural languages) or methods (for object-
oriented languages).

Embedded SQL: Like dynamic SQL, embedded SQL provides a


means by which a program can interact with a database
server.

21BCT3DA - Discipline Specific Core - RDBMS | ODD 2022-23


JDBC
The JDBC standard defines an application program interface
(API) that Java programs can use to connect to database
servers.

21BCT3DA - Discipline Specific Core - RDBMS | ODD 2022-23


JDBC

21BCT3DA - Discipline Specific Core - RDBMS | ODD 2022-23


Connecting to the Database

The first step in accessing a database from a Java program is to


open a connection to the database. This step is required to
select which database to use, for example, an instance of
Oracle running on your machine, or a PostgreSQL database
running on another machine. Only after opening a connection
can a Java program execute SQL statements. A connection is
opened using the getConnection method of the Driver
Manager class (within java.sql). This method takes three
parameters.

21BCT3DA - Discipline Specific Core - RDBMS | ODD 2022-23


Connecting to the Database

• The first parameter to the getConnection call is a string that specifies the URL, or machine
name, where the server runs (in our example, db.yale.edu), along with possibly some other
information such as the protocol to be used to communicate with the database (in our
example, jdbc:oracle:thin:; we shall shortly see why this is required), the port number the
database system uses for communication (in our example, 2000), and the specific database on
the server to be used (in our example, univdb). Note that JDBC specifies only the API, not the
communication protocol. A JDBC driver may support multiple protocols, and we must specify
one supported by both the database and the driver. The protocol details are vendor specific.
• The second parameter to getConnection is a database user identifier, which is a string.
• The third parameter is a password, which is also a string. (Note that the need to specify a
password within the JDBC code presents a security risk if an unauthorized person accesses
your Java code.)

21BCT3DA - Discipline Specific Core - RDBMS | ODD 2022-23


Connecting to the Database

21BCT3DA - Discipline Specific Core - RDBMS | ODD 2022-23


ODBC

The Open Database Connectivity (ODBC) standard defines an


API that applications can use to open a connection with a
database, send queries and updates, and get back results.

21BCT3DA - Discipline Specific Core - RDBMS | ODD 2022-23


ODBC

21BCT3DA - Discipline Specific Core - RDBMS | ODD 2022-23


ODBC

21BCT3DA - Discipline Specific Core - RDBMS | ODD 2022-23


Try it yourself

• Create table department with following fields


department name, building, budget.

• In this table department name should be


primary key

21BCT3DA - Discipline Specific Core - RDBMS | ODD 2022-23


Solution

21BCT3DA - Discipline Specific Core - RDBMS | ODD 2022-23


Try it yourself

• Create table course with following field course


id, title, department name, credits.

• In this table Course id should be primary key


• And also, department name should be foreign
key.

21BCT3DA - Discipline Specific Core - RDBMS | ODD 2022-23


Solution

21BCT3DA - Discipline Specific Core - RDBMS | ODD 2022-23


Question

Expand DML __________________


a. Data-Definition Language
b. Data-Manipulation Language
c. Data Maintain Language
d. All of these

What is a foreign key?


A. A foreign key is a primary key of a relation which is an attribute in
another relation
B. A foreign key is a superkey of a relation which is an attribute in more
than one other relations
C. A foreign key is an attribute of a relation that is a primary key of
another relation..
D. A foreign key is the primary key of a relation that does not occur
anywhere else in the schema

21BCT3DA - Discipline Specific Core - RDBMS | ODD 2022-23


Evaluation Questionnaires

1. What is SQL?
2. What is DML?

3. What is Primary Key?


4. What foreign Key?

5. Define Check Constraints.

21BCT3DA - Discipline Specific Core - RDBMS | ODD 2022-23


Next topic for Lect. 13

• Functions and Procedures – Triggers.

21BCT3DA - Discipline Specific Core - RDBMS | ODD 2022-23

You might also like