Unit III Rdbms

Embed Size (px)

Citation preview

  • 8/8/2019 Unit III Rdbms

    1/8

    UNIT - III

    SQL (pronounced ess-cue-elorsee-kwul): often referred to as Structured Query Language,[2][3]

    is a database

    computer language designed for managing data in relational database management systems (RDBMS), and

    originally based upon relational algebra. Its scope includes data query and update, schema creation and

    modification, and data access control. SQL was one of the first languages for Edgar F. Codd's relational model in his

    influential 1970 paper, "A Relational Model of Data for Large Shared Data Banks"

    [4]

    and became the most widelyused language for relational databases.[2][5]

    Structured Query Language -- SQL

    As indicated above, a very popular data base language for data definition, manipulation,and query is structured query language (SQL). In the context of the three types of

    database languages discussed earlier (i.e., DDL, DML, and DQL), SQL includesstatements for all three purposes. The set of "create" commands within SQL are used to

    define tables, i.e., as a DDL. SQL statements can be embedded into conventionalprogramming languages (i.e., used as a DML). Finally, and in its most common use, SQL

    can be used as a DQL by end users seeking answers to ad hoc queries. There are four

    primary operations that can be performed on tables using SQL -- the SELECT operation(to select rows from one or more tables), the INSERT operation (to insert rows into atable), the UPDATE operation (to modify one or more rows in a table), and the DELETE

    operation (to delete one or more rows from a table). The most commonly used SQLoperation to answer ad hoc queries is the SELECT operation. The SELECT operation

    can simultaneously (1) create a horizontal subset of a table, i.e., selecting all rows from atable that meet a certain condition, (2) link tables using the common field between them,

    and (3) create a vertical subset of a table or tables, i.e., displaying only certain fields in

    tables.

    Schema

    The schema (pronounced skee-ma) of a database system is its structure described in a formal language supported by

    the database management system (DBMS). In a relational database, the schema defines the tables, the fields,

    relationships, views, indexes, packages, procedures, functions, queues, triggers, types, sequences, materialized

    views, synonyms, database links, directories, Java, XML schemas, and other elements.

    Schemas are generally stored in a data dictionary. Although a schema is defined in text database language, the term

    is often used to refer to a graphical depiction of the database structure. In other words, schema is the structure of the

    database that defines the objects in the database.

    In an Oracle system, the term "schema" has a slightly difference connotation. For the interpretation used in Oracle,

    see schema object.

    Table Definition

    The SQLPlus command describe returns the definitions of tables and views. For example, information about yourtables are stored in the table TABS. However, since you do know the columns names of that table, you can not query

    that table. The command

    describe tabs;

    will produces that information

  • 8/8/2019 Unit III Rdbms

    2/8

    Name Null? Type------------------------------- -------- ----TABLE_NAME NOT NULL VARCHAR2(30)

    TABLESPACE_NAME VARCHAR2(30)CLUSTER_NAME VARCHAR2(30)IOT_NAME VARCHAR2(30)PCT_FREE NUMBERPCT_USED NUMBERINI_TRANS NUMBERMAX_TRANS NUMBERINITIAL_EXTENT NUMBERNEXT_EXTENT NUMBERMIN_EXTENTS NUMBER

    MAX_EXTENTS NUMBERPCT_INCREASE NUMBERFREELISTS NUMBERFREELIST_GROUPS NUMBERLOGGING VARCHAR2(3)

    BACKED_UP VARCHAR2(1)NUM_ROWS NUMBER

    BLOCKS NUMBEREMPTY_BLOCKS NUMBERAVG_SPACE NUMBER

    CHAIN_CNT NUMBERAVG_ROW_LEN NUMBERAVG_SPACE_FREELIST_BLOCKS NUMBERNUM_FREELIST_BLOCKS NUMBERDEGREE VARCHAR2(10)

    INSTANCES VARCHAR2(10)CACHE VARCHAR2(5)TABLE_LOCK VARCHAR2(8)SAMPLE_SIZE NUMBERLAST_ANALYZED DATEPARTITIONED VARCHAR2(3)IOT_TYPE VARCHAR2(12)TEMPORARY VARCHAR2(1)NESTED VARCHAR2(3)BUFFER_POOL VARCHAR2(7)

    The first column is the list of column names. The second column is a list of Not Null indicators. The third column is

    the list of data types. This output reveals that the column name "TABLE_NAME" is not allowed to be null. Now toget an alphabetical list of all the tables in your account, use the commandselect table_name from tabs order by table_name;

    You can use the describe command on the following views to obtain information on your account.

    View Name Description

    DICT table names and table description

    DICT_COLUMN column names of table names and column description

    CAT names of all user's tables, views, synonyms, and sequences

    OBJ information on all objects in your account

    TABS table information on all user's tables

    COLS column information on all user's columns

    USER_VIEWS view information on all user's views

    SYN synonyms information on all user's synonyms

  • 8/8/2019 Unit III Rdbms

    3/8

    SEQ sequence information on all user's sequences

    USER_CONSTRAINTS constraint information on user's constraints

    USER_CONS_COLUMNS column information on user's constraints

    IND index information on all user's indices

    USER_IND_COLUMNS column information on user's indices

    Data Manipulation Language (DML)

    Is a family of computer languages used by computer programs and/or database users to insert, delete and update data

    in a database. Read-only querying, i.e. SELECT, of this data may be considered to be either part of DML or outside

    it, depending on the context.

    Currently the most popular data manipulation language is that of SQL , which is used to retrieve and manipulate data

    in a Relational database.[1] Other forms of DML are those used by IMS/DLI, CODASYL databases (such as IDMS),

    and others.

    Data Manipulation Language comprises the 'SQL-data change' statements[2], which modify stored data but not the

    schema or database objects. Manipulation of persistent database objects (e.g. tables or stored procedures) via the

    'SQL-schema' statements[2], rather than the data stored within them, is considered to be part of a separate Data

    Definition Language. In SQL these two categories are similar in their detailed syntax, data types, expressions etc.,

    but distinct in their overall function.[2]

    Data Manipulation Languages have their functional capability organized by the initial word in a statement, which is

    almost always a verb. In the case of SQL, these verbs are:

    y SELECT ... INTO

    y INSERT

    y UPDATE

    y DELETE

    The purely read-only SELECT query statement is classed with the 'SQL-data' statements[2]

    and so is considered by

    the standard to be outside of DML. The SELECT ... INTO form is considered to be DML because it manipulates

    (i.e. modifies) data. In common practice though, this distinction is not made and SELECT is widely considered to be

    part of DML.[3]

    Most SQL database implementations extend their SQL capabilities by providing imperative, i.e., procedural,

    languages. Examples of these are Oracle's PL/SQL and DB2's SQL PL.

    Data manipulation languages tend to have many different flavors and capabilities between database vendors. There

    have been a number of standards established for SQL by ANSI,[1] but vendors still provide their own extensions to

    the standard while not implementing the entire standard.

    There are two types of data manipulation languages:

    y Procedural

    y Declarative

  • 8/8/2019 Unit III Rdbms

    4/8

    Each SQL DML statement is a declarative command. The individual SQL statements are declarative, as opposed to

    imperative, in that they describe what the program should accomplish, rather than describing how to go about

    accomplishing it.

    Data manipulation languages were initially only used by computer programs, but (with the

    advent of SQL) have come to be used by people as well.

    A database server is a computer program that provides database services to other computer programs or computers,

    as defined by the clientserver model. The term may also refer to a computer dedicated to running such a program.

    Database management systems frequently provide database server functionality, and some DBMSs (e.g., MySQL)

    rely exclusively on the clientserver model for database access.

    Such a server is accessed either through a "front end" running on the users computer which displays requested data

    or the back end which runs on the server and handles tasks such as data analysis and storage.

    In a master-slave model, database master servers are central and primary locations of data while database slave

    servers are synchronized backups of the master acting as proxies.

    Some examples of Database servers are Oracle, DB2, Informix, Ingres, SQL Server. Every server uses its own querylogic and structure. The SQL query language is more or less the same in all the database servers.

    Graphical Query Language (GQL)

    GQL is a formal query language for graphically manipulating a semantic database. Thelanguage provides a set of pattern images and a set of simple rules for constructing patterngraphs. Users describe their queries by drawing pattern graphs against the graphicalschema of the database. This paper describes various aspects ofGQL: the underlyingmodel, the pattern images and graphs, and the representation of operations.

    Implementational issues of aG

    QL interpreter and its relation to other query languages arealso discussed.

    Client Server and Table Database

    A database server is a computer program that provides database services to other computer programs or computers,

    as defined by the clientserver model. The term may also refer to a computer dedicated to running such a program.

    Database management systems frequently provide database server functionality, and some DBMSs (e.g., MySQL)

    rely exclusively on the clientserver model for database access.

    Such a server is accessed either through a "front end" running on the users computer which displays requested data

    or the back end which runs on the server and handles tasks such as data analysis and storage.

    In a master-slave model, database master servers are central and primary locations of data while database slave

    servers are synchronized backups of the master acting as proxies.

    Some examples of Database servers are Oracle, DB2, Informix, Ingres, SQL Server. Every server uses its own query

    logic and structure. The SQL query language is more or less the same in all the database servers.

    In relational databases and flat file databases, a table is a set of data elements (values) that isorganized using a model of vertical columns (which are identified by their name) and horizontal

  • 8/8/2019 Unit III Rdbms

    5/8

    rows. A table has a specified number of columns, but can have any number of rows. Each row isidentified by the values appearing in a particular column subset which has been identified as a

    candidate key.

    Table is another term forrelations; although there is the difference in that a table is usually a

    multi-set (bag) of rows whereas a relation is a set and does not allow duplicates. Besides theactual data rows, tables generally have associated with them some meta-information, such asconstraints on the table or on the values within particular columns.

    The data in a table does not have to be physically stored in the database. Views are also

    relational tables, but their data are calculated at query time. Another example are nicknames,

    which represent a pointer to a table in another database.

    Data manipulation

    The Data Manipulation Language (DML) is the subset of SQL used to add, update and delete

    data:

    y INSERT adds rows (formally tuples) to an existing table, e.g.,:

    INSERT INTO My_table(field1, field2, field3)

    VALUES('test', 'N', NULL);

    y UPDATE modifies a set of existing table rows, e.g.,:

    UPDATE My_table

    SET field1 = 'updated value'WHERE field2 = 'N';

    y DELETE removes existing rows from a table, e.g.,:

    DELETE FROM My_tableWHERE field2 = 'N';

    y TRUNCATE deletes all data from a table in a very fast way. It usually implies a subsequentCOMMIT operation.

    y MERGE is used to combine the data of multiple tables. It combines the INSERT and UPDATEelements. It is defined in the SQL:2003 standard; prior to that, some databases provided

    similar functionality via different syntax, sometimes called "upsert".

    Developing Client Applications

    The majority of documents in this specification series describe the services an Open Services forLifecycle Collaboration provider MUST and MAY provide to ALM applications. The focus of thisdocument is how to develop clients that interact with these Open Services for Lifecycle Collaboration.

  • 8/8/2019 Unit III Rdbms

    6/8

    Introduction

    Among the goals of the Open Services for Lifecycle Collaboration initiative is to provide a simple,standards-based approach to lifecycle services. With this in mind the set of requirements for a clientto interact with Open Services for Lifecycle Collaboration have been kept to a minimum and whereverpossible to standard packages likely to be available for common languages. As you might expect for aserver based on Internet principles many operations can be carried out by a browser (at least thoseusing GET) and a browser is particularly useful during client development for validating contentreturned by thes services.

    The descriptions below are intended to be quite pedantic, and are intended to show the details ofcommunication but are not intended to be ready-to-use client libraries.

    Creating Relational Schema using SQL

    Relational schemas can be created using DDL (Data Definition Language) operations of SQL. In

    order to create tables, the type of each column (attribute) has to be defined. The following table

    summarizes available data types:

    Type Description

    Char(n) Text having a length of exactly n characters

    Varchar(n) Variable-length text having a maximum length of n characters

    Numeric(p, s) p (precision): the maximum total number of decimal digits that can be stored,both to the left and to the right of the decimal point.s (scale): the maximum number of decimal digits that can be stored to the right ofthe decimal point. Scale must be a value from 0 throughp.

    Int An integer number

    Float A floating point number

    Datetime Date and time specifier

    Tables can be created using Create table operation:

    CREATE TABLE #table_name# (

    #column_name# #data_type# [#default_value#] [NULL|NOT NULL] #column_restrictions#

    #column_name# #data_type# [#default_value#] [NULL|NOT NULL] #column_restrictions#,

    #table_restrictions# )

    Available restrictions:

    PRIMARY KEY: to define a primary key on a column

  • 8/8/2019 Unit III Rdbms

    7/8

    UNIQUE [KEY]: to define that values of this column have to be unique

    [FOREIGN KEY] REFERENCES #table#(#columns#): to define a foreign key to column(s) of

    another table

    CHECK [CONTRAINT] #logical_expression#: any logical expression can be applied

    Example DDL scripts to create tables:

    create table student

    (

    ID int primary key,

    Name Varchar(50) not null,

    IdentityNumber int not null unique,

    Faculty Varchar(40) default Informatics

    )

    create table course

    (

    ID int primary key,

    Name Varchar(30)

    )

  • 8/8/2019 Unit III Rdbms

    8/8

    create table result

    (

    StudentID int references student(ID),

    CourseID int references course(ID),

    ExamDate datetime,

    Result int,

    Primary key (StudentID,CourseID, ExamDate)

    )