SQLP.TXT

download SQLP.TXT

of 10

Transcript of SQLP.TXT

  • 7/29/2019 SQLP.TXT

    1/10

    SQL:SQL: Structured query language

    Using SQL we can create and maintain data manipulation objects such as tables, views,sequences etc. These data manipulation objects will be created and stored on theservers harddisk drive in a table space

    Data manipulation objects:

    1. DDL: Data Definition Language (CREATE, ALTER, DROP)The commands are used to define a database, including creating, altering, anddropping tables and establishing constraints

    2 DML: Data Manipulation Language(SELECT,INSERT,DELETE,UPDATE)The commands are used to maintain and query a database including inserting,modifying, updating and querying data

    3. DCL: Data control Language (COMMIT, ROLLBACK,GRANT)The commands are used to control a database including administering priviligesand the committing (saving) of data

    DATA MANIPULATION IN DATABASE MANAGEMENT SYSTEMS

    Table/Entity:In a DBMS a group of similar information or data which is of interest to

    anorganization is called an Entity

    Entity information is stored in an object is called TableFor example a client is considered an entityA table is really a two dimensional matrix that consist of rows and col

    umns. The tablemust have a unique name

    ATTRIBUTES/COLUMNS/FIELDS:

    A client can have characteristics like name, address, telephone number, fax number etc.The characteristics of an entity are called Attributes. The values for these characteristics arecalled attribute values.

    NAMEADDRESSTELEPHONENUMBERFAXNUMBER

    BALANCEDUEVRSCHIRALA23233489997775000.00

    TUPLE/RECORD/ROW:

  • 7/29/2019 SQLP.TXT

    2/10

    Multiple fields placed in a horizontal plane, is called a Record or Row or Tuple.The SQL engine requires three parameters1. column name2. column size3. column data type

    DATA TYPES IN SQLCHAR(SIZE)This data type is used to store character strings values of fixed length. Thesize in brackets determines the number of characters the cell can hold. Themaximum number of characters in e can hold is 255 charactersVARCHAR(SIZE)VARCHAR2(SIZE)This data type is used to store variable length alphanumeric data. This datatype can hold maximum of 2000 characters. The difference between CHARand VARCHAR is VARCHAR uses non -padded comparison semanticsNUMBER(P,S)The NUMBER data type is used to store numbers (fixed or floating point).Numbers. It may be stored up to 38 digits of precision. The precision (P)determiner the maximum length of the data, where as the scale (S)determines the number of places to the right of the decimal. If scale isomitted then the default is zero

    DATE

    This data type is used to represent date and time. The standard form is DD-MON-YY as in 21-JUL-06. The default time is 12:00:00 am, if no timeportion is specified. The default date for a date field is the first day of thecurrent month .Any date from january 4712 B.C to 4712 A.DLONGThis data type is used to store variable length character strings containingupto 2GB . Long values cannot be indexed and the normal characterfunctions such as SUBSTR can not be applied to LONG values

    RAW/LONGRAWThe RAW/LONGRAW data types is used to store binary data such asdigitized picture or image RAW data type can have a maximum length of255 bytes. LONG RAW data types can contain up to 2GB. Values stored incolumns having LONG RAW data type can not be indexedBLOBBinary large object capable of storing up to 4 G.B of binary data(photograph or soundclip

    The rules of (grammar) SQL syntax:

    1.An SQL statement starts with a verb. This verb may have additional nouns and a

    djjectives2.Each verb is followed by a number of clauses3. A space separates clauses within an SQL statement4. A comma separates parameters within a clause5. A semicolon is used to terminate the SQL statements

    DDL COMMANDS

    The Create Table Command

  • 7/29/2019 SQLP.TXT

    3/10

    Syntax:CREATE TABLE tablename (columnname datatype (size), columnname datatype(size));

    ExampleSQL> CREATE TABLE grade

    (Maths number (5),Phy number (5),Cs number (5),Grade varchar2 (10));

    Table created.

    CREATING A TABLE FROM A TABLE

    Syntax:CREATE TABLE tablename

    [(columnname,columnname)] AS SELECT columnane,columnname FROMtablename

    PURPOSE: (USED FOR RENAMING THE FIELD NAMES OF A TABLE)

    EXAMPLE:

    CREATE TABLE SUPPLIER_MASTER(supplier_no,supplier_name,address1,address2,city,state,pincode,remarks)AS SELECT client_no,name,address1,address2,city,state,pincode,remarks FROMclient_master

    This is creating a table supplier_master from client_master.Here client_no is renamed with supplier_no

    Name is renamed with supplier_name

    MODIFYING THE STRUCTUE OF TABLES

    Alter table command: To alter the definition of a table.a) To add a columnb) To add an Integrity constraintc) To redefine a column (datatype, size, default value)d) To modify storage char's or other preameterse) To enabled disable or drop and integrity constraint.Syntax: Alter tableADDMODIFY DROP

    ADD clause:Syntax:

    ALTER TABLE tablenameADD(newcolumnname datatype(size), newcolumnname data type(size)..);

    Ex: Add a column to the emp table that will held the name of an employee's space

    ALTER TABLE EMP

  • 7/29/2019 SQLP.TXT

    4/10

    ADD SPACE -NAME VARCHAR2 (20);It alters the def of emp by adding new column.Ex: To add a table constraint to an existing table which specifies that the monthly salary mustnot exceed 5000.Alter table empADD check (Salary

  • 7/29/2019 SQLP.TXT

    5/10

    DESTROYING TABLESDrop table command: This command is used to drop or delete the db object table

    Syntax:

    DROP TABLE tablenameExample

    DROP TABLE client_master

    DML COMMAND: INSERT, DELETE, UPDATE AND SELECT, UPDATE COMMANDS

    Insert : Insert rows into the table

    Syntax: Insert into table-name(attr1, attr2, ...attrn) Values (Val1, Val2,......Valn);

    [?Inserting for all the field values]

    Ex: 1) Insert INTO sales people(Sno, sname, city comm)Values( 1001,'peel', `London', 0.15);

    Syntax: Insert into table-name Values (&attr1, &attr2, ...&attrn)[?Inserting for all the field values]/Enter the name: vrsEnter the number: 100

    [?NOT inserting for all the field values] Here city remains as unfilled

    2) INSERT INTO salespeople(sno, sname, comm) values(102,'xxx', 0.16)

    [?Inserting NULL value in city]. INSERT into salespeople values (101,'peel', NULL, 0.15);

    3. Insert INTO salespeople(sno, sname, city) Values (101,' peel', `London') - produces error,

    Because here COMM is NOT NULL

    1. We can specify the columns we wish to insert a value into the table by name ex(2)2. We shall notice that the column commissions have been omitted. This means that it will setto default value automatically. The default value will be either NULL or an explicitlydefined default value.3. Suppose the NOTNULL constraint is approved for the column COMM is the salespeople

  • 7/29/2019 SQLP.TXT

    6/10

    table then it prevents a NULL from being accepted.

    INSERTING DATA INTO A TABLE FROM ANOTHER TABLESyntax

    INSERT INTO tablenameSELECT colmnname,columnname,FROM tablename;Example

    INSERT INTO supplier_masterSELECT client_no, name, adress1, adress2, city, state, pincode, remarksFROM client_master;

    INSERTING OF A DATA SET INTO A TABLE FROM ANOTHER TABLESyntax

    INSERT INTO tablenameSELECT colmnname, columnname,FROM tablenameWHERE column=expression;Example

    INSERT INTO supplier_masterSELECT client_no, name, adress1, adress2, city, state, pincode, remarksFROM client_master

    WHERE CLIENT_NO='VRS111';

    4. INSERT into salespeople;Select From salespeople 1 where city="LONDON";

    Provide the str of salespeople ? salespeople 1 must be same

    VIEWING DATA IN THE TABLES

    Once data has been inserted into a table the next most logical operation is viewing data

    VIEWING ALL ROWS AND ALL COLUMNS

    I Syntax: SELECT (columnname1. columname n ) FROM tablename;

    Here columnname1 . columnname n represents table columns.

    II Syntax: SELECT * from tablename

    1. SELECT NAME, SALARY FROM emp;2. SELECT * FROM emp;The oracle can expand the cahracter asterisk(*) mean all columns in the table;

    SELECTED COLUMNS AND ALL ROWS

    Syntax: SELECT columnanme, columnname FROM tablenameExample: SELECT client_no, client_name FROM client_master;

    SELECTED ROWS AND ALL COLUMNSWhen a WHERE clause is addeded to the SQL sentence, the Oracle server co

    mpareseach record from the table with the condition specified in the where clause. Oracle displays therecords that satisfies the specified conditionI Syntax: SELECT *

  • 7/29/2019 SQLP.TXT

    7/10

    FROM tablenameWHERE search condition;

    Ex: SELECT * FROM client_master WHERE bal_due>0;

    SELECTED ROWS AND SELECTED COLUMNSI Syntax: SELECT columnname, columnnameFROM tablename

    WHERE search condition;

    Ex: SELECT client_no , name FROM client_master WHERE bal_due>1000;

    ELLIMINATION OF DUPLICATES FROM THE SELECT STATEMENTI Syntax: SELECT DISTINCTcolumnname, columnnameFROM tablename

    Ex: SELECT DISTINCT job FROM emp;

    TO DISPLAY THE UNIQUE ROWS FROM THE TABLE- I Syntax: SELECT DISTINCT *FROM tablename

    Ex: SELECT DISTINCT * FROM emp;

    SORTING DATA INTO A TABLE

    Oracle allows data from a table to viewed in a sorted order. The rows retrieved from thetable will be sorted in either ascending or descending order depending on the conditionspecified in the select sentence

    I Syntax: SELECT * FROM tablename ORDER BY columnname, columnname [sort order)

    DELETE OPERATIONSDELETE: Delete the data from the tableSyntax:

    DELETE FROM tablename;

    Ex: 1. DELETE from EMP:

    Removal of a specified rows

    Syntax:1.DELETE FROM tablename WHERE search condition;

    2. DELETE From EMPwhere empno=73241

    TRUNCATE COMMAND

    TRUNCATE removes all rows from a table. The operation cannot be rolled backTRUNCATE is faster and doesn't use as much undo space as a DELETE.

    Ex1: Truncate table emp;table truncated.

    Ex2: Select count(*) from emp;

  • 7/29/2019 SQLP.TXT

    8/10

    count(*)--------

    0

    UPDATE:

    The UPDATE command is used to change or modify data values in a table.The update command cosists of a `Set'clause & an optional `where' clause.

    1.To update all the rows from a table2.To select set of rows from a tableUPDATING ALL THE ROWSSyntax: UPDATE table name SET columnname=expression, columnname=expressionExample Update emp_master SET netsal=net_sal+basic_sal*0.15

    UPDATING RECORDS CONDITIONALLYSyntax: UPDATE table name SET columnname=expression,columnname=expression. WHERE columnname =expression;Example Update emp_master SET netsal=net_sal+basic_sal*0.15 WHEREclient_no='co100'

    Finding out the tables created by a user:

    Syntax: SELECT * FROM TAB;

    Finding out the column details of a table created by a user:

    DESCRIBE tablename;

    Syntax: Select * from COL;

    Displays all information about the columns of one's own tables.

    Syntax: SELECT user FROM dual;

    user-------scott

    Syntax: DESC all_tables;

    Lists data kept on tables like owner, table_name, tablespace_name .

    Syntax: DESC all_objects;

    Lists data kept on objects like owner, object_name, object_id, object_type

    DCL - Data Control Language.

    1. GRANT - gives user's access privileges to database2. REVOKE - withdraw access privileges given with the GRANT command

    TCL - Transaction Control:

  • 7/29/2019 SQLP.TXT

    9/10

    Statements used to manage the changes made by DML statements. It allows statements to begrouped together into logical transactions.? COMMIT - save work done? SAVEPOINT - identify a point in a transaction to which you can later roll back? ROLLBACK - restore database to original since the last COMMIT? SET TRANSACTION - Change transaction options like isolation level and whatrollback segment to use DML

    COMMIT COMMAND

    The delete command is used to remove some or all rows from a table.After performing a DELETE operation you need to COMMIT or ROLLBACK the transactionto make the change permanent or to undo it.

    Ex1: Select count (*) from emp;Count (*)

    ----------14

    Ex2: Delete from emp where job = 'clerk';4 rows deleted.

    Ex3: commit;commit complete.

    Ex4: Select count (*) from emp;Count (*)----------

    10

    ROLLBACK COMMAND

    ROLLBACK is an SQL command used to undo changes made to the database (restore da

    ta toits state prior to the user making changes).

    Example:INSERT INTO tab1 VALUES ('val1', 'val2', 'val3');ROLLBACK;

    Ex1: Delete from emp where job = 'manager';

    3 rows deleted.

    Ex2: select count (*) from emp;

    count(*)---------

    11

    Ex3: rollback;

    Rollback complete.

  • 7/29/2019 SQLP.TXT

    10/10

    Ex4: Select count (*) from emp;

    count(*)---------

    14

    Creating our OWN users:-1) Boot with win 982) Click start?prog?Personal oracle 7 for windows 95/98/97?SQL * 7.33) Click on personal oracle 74) Click on local databses5) Right click on user6) select New7) Give user name/password;