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

RDBMS: Manages Data in The Form of Tables. Related Tables in RDBMS Have Relationships Through Common Values

SQL is a query language used to manage data, define schema objects, and retrieve data from relational databases. It includes languages for data definition (DDL), data manipulation (DML), and data control (DCL). DDL is used to define database schema through commands like CREATE, ALTER, and DROP. DML maintains and queries data using commands like SELECT, INSERT, UPDATE, and DELETE. DCL controls user access and privileges.

Uploaded by

aggarwalmegha
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
71 views

RDBMS: Manages Data in The Form of Tables. Related Tables in RDBMS Have Relationships Through Common Values

SQL is a query language used to manage data, define schema objects, and retrieve data from relational databases. It includes languages for data definition (DDL), data manipulation (DML), and data control (DCL). DDL is used to define database schema through commands like CREATE, ALTER, and DROP. DML maintains and queries data using commands like SELECT, INSERT, UPDATE, and DELETE. DCL controls user access and privileges.

Uploaded by

aggarwalmegha
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 45

SQL

RDBMS manages data in the form of tables. related tables in RDBMS have relationships through common values. Catalog
It contains data about other data. i.e., in general catalog contains the data about the directories, subdirectories and files in the system. In relation to databases, catalog contains set of schemas, which describe databases.

SQL is a query language for relational databases.


SQL 1

SQL
We have seen conceptual, external (user view), physical (internal) schemas. There are some other schemas like star, snowflake which will be useful in data warehousing. A schema contains description of objects created by a user, such as base tables, views, constraints, domains, triggers etc., which are part of a database.
SQL 2

SQL
SQL is divided into three categories.
Data Definition Language (DDL) Data Manipulation Language (DML) Data Control Language (DCL)

SQL

SQL
DDL is used to create database, create tables in it, delete tables, alter tables, define constraints, etc., DML is use to maintain (update, insert, modify) and query the data in the database. DCL is used to control the database like giving privileges to users and overall administration of the database.
SQL 4

DDL
Some of the commands (statements) in DDL are
CREATE SCHEMA CREATE TABLE CREATE VIEW CREATE CHARACTER SET CREATE COLLATION CREATE TRANSLATION CREATE ASSERTION CREATE DOMAIN

SQL

Data Types
String
Fixed length character data of n characters (maximum 2000) VARCHAR2(n) variable length character data (maximum 4000) LONG variable length character data (max 4GB). Maximum one per table. CHAR(n)

Numeric NUMBER(n,m) signed decimal number with


INTEGER(n) n digits and assumed decimal point m digits from right. signed integer, decimal or binary of n digits floating point number in scientific notation of n binary digits precision

FLOAT(n)

SQL

Data Types
Date/time DATE
fixed length date and time

Logical

BOOLEAN

stores TRUE, FLASE or UNKNOWN

Temporal TIMESTAMP date and time of event occurred


(year, month, day, hour, minute, second (with fractional seconds, default 6digits))

SQL

CREATE TABLE
It creates a new table and its columns. Syntax: CREATE TABLE table-name (
column-name datatype

[column constraint] [default value] [, column-name datatype [column constraint] [default value] .] [,table constraint] ) ;
SQL 8

CREATE TABLE (cont.)


Ex 1: CREATE TABLE student_t (
char(11) not null, varchar2(30), date, char(1) CHECK (gender in (f,m)), fname varchar2(30), home_address varchar2(100), hostel_address varchar2(100), CONSTRAINT student_pk primary key (st_id)); st_id st_name birth_date gender

SQL

CREATE TABLE (cont.)


Ex 2: CREATE TABLE course_t (
course_no char(12) not null, c_name varchar2(30), units integer CHECK (units in (1,2,3,4,5,8,18), CONSTRAINT course_pk primary key (course_no));

SQL

10

CREATE TABLE (cont.)


Ex 3:
CREATE TABLE st_course_t (
st_id char(11) not null, course_no char(12) not null, section integer, grade varchar(2) CHECK (grade in (A,B,C,D,E,F,I,W,GA,NC,RC)), year integer, sem integer CHECK (sem in (1,2)), CONSTRAINT st_course_pk primary key (st_id, course_no), CONSTRAINT st_course_fk1 foreign key(st_id) REFERENCES student_t(st_id), CONSTRAINT st_course_fk2 foreign key(course_no) REFERENCES course_t(course_no));
SQL 11

CREATE VIEW
Creates a view of as per the requirements of user from the related tables in the database. Syntax:
CREATE [OR REPLACE] VIEW view-name AS [(column [,column .])] subquery;

SQL

12

CREATE VIEW (cont.)


where view-name is the name of the view. columns are column names in the in the view. subquery is the query to be applied on
the related tables as per the requirements. The query is normally a SELECT statement.

SQL

13

CREATE VIEW (cont.)


Let us take a table
student_t( st_id, st_name, birth_date, gender, fname, home_addr, hostel_addr)

Ex 1:

CREATE VIEW st_addr_vb3 AS select st_id, st_name, home_addr from student_t where st_id LIKE 2009B3%; CREATE VIEW st_addr_vb4 AS select st_id, st_name, home_addr from student_t where st_id LIKE %B4%;

Ex 2:

SQL

14

CREATE VIEW (cont.)


Ex 3: Let us take a table
student_t( st_id, st_name, birth_date, gender, fname, house_no, street_name, locality_name, city_name, state_name) CREATE VIEW st_addr_v1 (st_id,st_name, address) AS select st_id, st_name, addr(house_no, street_name, locality_name, city_name, state_name) from student_t ; CREATE VIEW st_addr_vgoa (st_id,st_name, address) AS select st_id, st_name, addr(house_no, street_name, locality_name, city_name, state_name) from student_t where state_name = GOA;
SQL 15

CREATE VIEW (cont.)


Ex 4: Let us take the tables student_t( st_id, st_name, birth_date, gender, fname, home_addr, hostel_addr) st_course_t (st_id, course_no , section, grade, year, sem)

CREATE VIEW st_performance_v (st_id, st_name, course_no, sem, year, grade) AS select student_t.st_id, st_name, course_no, sem, year, grade FROM student_t, st_course_t WHERE student_t.st_id = st_course_t.st_id;

SQL

16

ALTER TABLE
ALTER TABLE is used to change table definition. Syntax:
ALTER TABLE table_name alter_table_action; where alter_table_action is on of the following:
ADD [COLUMN] column_definition ALTER [COLUMN] column_name SET DEFAULT default_value ALTER [COLUMN] column_name DROP DEFAULT DROP [COLUMN] column_name [RESTRICT] [CASCADE] ADD table_constraint
SQL 17

ALTER TABLE
CREATE TABLE course_t (
course_no char(12) not null, c_name varchar2(30), units integer CHECK (units in (1,2,3,4,5,8,18), CONSTRAINT course_pk primary key (course_no));

In the above table, if we want to add the semester number (in an academic year) the course is floated.
ALTER TABLE course_t ADD (sem_no integer); Note: not null can not be used, if already data exists in the table.
SQL 18

SQL

19

DML
Some of the commands (statements) in DML are
UPDATE INSERT DELETE SELECT

SQL

20

WILD CHARACTERS
There are 3 wild caracters(cards). * (asterisk) _ (underscore) % (percentage)

SQL

21

WILD CHARACTERS
* is used
(i) to select all columns (attributes) of a table in a SELECT statement (ii) in functions like COUNT, to count number of row selected as per the condition in the WHERE clause

SQL

22

WILD CHARACTERS
_ and % are used in the WHERE clause when an exact match is not possible. In this case ,the keyword LIKE is paired with _ and/or % continuous characters that are known to be desired matches. These are used
before the continuous characters that are known to be desired matches after the continuous characters that are known to be desired matches before and after continuous characters that are known to be desired matches
SQL 23

WILD CHARACTERS
_ is used as wild character to represent only one character. % is used as wild character to represent any number of characters.

SQL

24

Comparison operators
operator ----------= > < >= <= != or <> meaning -----------equal to greater than less than greater than or equal to less than or equal to not equal to
SQL 25

Boolean operators
Boolean operators are used in WHERE clause to join more than one condition or to negate an expression. operator meaning ---------------------AND joins two or more conditions and returns results only when all conditions are true. OR joins two or more conditions and returns results only when at least one condition is true. NOT negates an expression

SQL

26

UPDATE
Update statement is use to change the existing data in the database. Syntax:
UPDATE table-name SET {clolumn = expression [,column = expression] .} [WHERE condition]; UPDATE table-name SET {clolumn = expression/subquery [,column = expression/subquery] .} [WHERE condition];

SQL

27

UPDATE (cont.)
Ex: update st_course_t set course_no = AAOCC311 where st_id = 2007P1PS001 and course_no = AAOCGC311;

SQL

28

UPDATE (cont.)
Ex: update course_t set course_no = AAOCC311, course_title = Database Management Applications where course_no = AAOCGC311;

SQL

29

SQL

30

INSERT
Insert command is used to add/insert one or more rows to the table. Syntax: INSERT INTO table-name [(column [,column] .)] VALUES (expression [,expression] .); INSERT INTO table-name query;

SQL

31

INSERT (cont.)
Ex: st_course_t(st_id, course_no, section, sem, year) insert into st_course_t values (2007B3A7100, AAOCC311, 2, 1, 2009-10); insert into st_course_t (st_id, course_no, sem, year) values (2007B3A7100, AAOCC311, 1, 2009-10);
SQL 32

INSERT (cont.)
Ex: (cont.) insert into st_course_t select * from temp_st_course_t;

insert into st_course_t select * from temp_st_course_t where course_no =AAOCC311;

SQL

33

DELETE
Deletes all rows from a table that satisfy the condition. Syntax: DELETE FROM table-name [WHERE condition];

SQL

34

DELETE (cont.)
Ex: delete from st_course_t where st_id = 2007A7PS001 and course_no = AAOCC311; delete from st_course_t where course_no = AAOCC311; delete from st_course_t where st_id = 2007B3A7450; delete from st_course_t;

SQL

35

How to access database contents faster?


There are various ways/techniques to improve the database access time:
index primary and/or secondary keys to increase the speed of row selection, tables joining and row ordering. Drop indexes to increase speed of table updating. select file organizations for base tables that match the type of processing activity on those tables (for example, keeping a table physically sorted by a key that is frequently used.)

SQL

36

How to access database contents faster? (Cont.)


selecting file organizations for indexes, which are also tables, appropriate to the way the indexes are used and allocating extra space for an index file, so that an index can grow without having to be reorganized. clustering date, so that related rows of frequently joined tables are stored close together in secondary storage to minimize retrieval time. maintaining statistics about tables and their indexes, so that the DBMS can find the most efficient ways to perform various database operations.

SQL

37

File types
HSAM Hierarchical Sequential Access Methods HISAM Hierarchical Indexed Sequential Access Methods HDAM Hierarchical Direct Access Methods HIDAM Hierarchical Indexed Direct Access Methods
SQL 38

INDEXES
As we have discussed, indexes have great impact on random and sequential access to base-table data. Creating INDEXES Syntax:
CREATE INDEX index-name ON table-name(col-name [,col-name] );

SQL

39

INDEXES (cont.)
Ex: cteate index sstid on student_t(st_id); create index cno on course_t(course_no); create index stcourse on st_course_t(st_id,course_no);

SQL

40

Deleting tables/views
If we dont need the tables/views that are created, then we can delete them. Before deleting the tables/views, we should delete the indexes created on them.

SQL

41

Deleting INDEXES
Syntax: DROP INDEX index-name; Ex: drop index sstid; drop index cno; drop index stcourse;

SQL

42

Deleting views
Syntax: DROP VIEW view-name; Ex: drop view st_grade;

SQL

43

Deleting tables
Syntax: DROP TABLE table-name; Ex: drop table student_t;

SQL

44

DCL
DCL is used to control the database, like giving privileges to users and overall administration of the database.

SQL

45

You might also like