Sei sulla pagina 1di 10

SQL: SQL: Structured query language Using SQL we can create and maintain data manipulation objects such

as t ables, views, sequences etc. These data manipulation objects will be created and stored on the servers hard disk 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, and dropping 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 priviliges and 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 an organization is called an Entity Entity information is stored in an object is called Table For example a client is considered an entity A table is really a two dimensional matrix that consist of rows and col umns. The table must have a unique name ATTRIBUTES/COLUMNS/FIELDS: A client can have characteristics like name, address, telephone number, fax numb er etc. The characteristics of an entity are called Attributes. The values for these cha racteristics are called attribute values. NAME ADDRESS TELEPHONE NUMBER FAX NUMBER BALANCE DUE VRS CHIRALA 232334 8999777 5000.00 TUPLE/RECORD/ROW:

Multiple fields placed in a horizontal plane, is called a Record or Row or Tuple . The SQL engine requires three parameters 1. column name 2. column size 3. column data type DATA TYPES IN SQL CHAR(SIZE) This data type is used to store character strings values of fixed length. The size in brackets determines the number of characters the cell can hold. The maximum number of characters in e can hold is 255 characters VARCHAR(SIZE) VARCHAR2(SIZE) This data type is used to store variable length alphanumeric data. This data type can hold maximum of 2000 characters. The difference between CHAR and VARCHAR is VARCHAR uses non -padded comparison semantics NUMBER(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 is omitted then the default is zero

DATE This data type is used to represent date and time. The standard form is DDMON-YY as in 21-JUL-06. The default time is 12:00:00 am, if no time portion is specified. The default date for a date field is the first day of th e current month .Any date from january 4712 B.C to 4712 A.D LONG This data type is used to store variable length character strings containing upto 2GB . Long values cannot be indexed and the normal character functions such as SUBSTR can not be applied to LONG values RAW/LONGRAW The RAW/LONGRAW data types is used to store binary data such as digitized picture or image RAW data type can have a maximum length of 255 bytes. LONG RAW data types can contain up to 2GB. Values stored in columns having LONG RAW data type can not be indexed BLOB Binary 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 djjectives 2.Each verb is followed by a number of clauses 3. A space separates clauses within an SQL statement 4. A comma separates parameters within a clause 5. A semicolon is used to terminate the SQL statements DDL COMMANDS The Create Table Command

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

Example SQL> 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 FROM tablename PURPOSE: 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 FROM client_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 column b) To add an Integrity constraint c) To redefine a column (datatype, size, default value) d) To modify storage char's or other preameters e) To enabled disable or drop and integrity constraint. Syntax: Alter table<table-name> ADD MODIFY <Column-specification><constraint-spec> DROP ADD clause: Syntax: ALTER TABLE tablename ADD(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 (USED FOR RENAMING THE FIELD NAMES OF A TABLE)

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 mont hly salary must not exceed 5000. Alter table emp ADD check (Salary <=5000); MODIFY CLAUSE: EX: To change the width of ename to 25 char's Syntax ALTER TABLE tablename MODIFY (columnname newdatatype(newsize)); Alter table emp Modify ename varchar2 (25); There are four changes that you can't make 1.change the name of the table 2.change the name of the table 3. drop a column 4.decrease the size of the column if table data exists To modify other constraints you must drop constraints and Support, and then add them specifying changes. Drop Clause: This clause is used to drop the constraint or the column in the tab le. Dropping a column in the table : ALTER TABLE EMP1 DROP Column MGR; Dropping the constraint: Syntax: ALTER TABLE table name DROP constraint const name Primary key UNIQUE {Col1,Col2, ....., Coln) CASCADE Ex: ALTER TABLE salespeople DROP constraint PRIMARY KEY; CASCADE OPTION: If the primary key of one table is dropped then the depending fo reign keys should also be dropped . Ex: ALTER TABLE DEPT1 DROP constraint PRIMARY KEY CASCADE; Enabling & disabling constraints: ALTER TABLE table-name [Enable / diable ] constraint constraint name Primary key CASCADE UNIQUE (Col1, Col2,...., Coln) Ex: ALTER TABLE dept1 Disable costraint PRIMARY KEY CASCADE;

DESTROYING TABLES Drop table command: This command is used to drop or delete the db object table Syntax: DROP TABLE tablename Example 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: vrs Enter 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 e x(2) 2. We shall notice that the column commissions have been omitted. This means tha t it will set to default value automatically. The default value will be either NULL or an expl icitly defined default value. 3. Suppose the NOTNULL constraint is approved for the column COMM is the salespe ople

table then it prevents a NULL from being accepted.

INSERTING DATA INTO A TABLE FROM ANOTHER TABLE Syntax INSERT INTO tablename SELECT colmnname,columnname, FROM tablename; Example INSERT INTO supplier_master SELECT client_no, name, adress1, adress2, city, state, pincode, remarks FROM client_master; INSERTING OF A DATA SET INTO A TABLE FROM ANOTHER TABLE Syntax INSERT INTO tablename SELECT colmnname, columnname, FROM tablename WHERE column=expression; Example INSERT INTO supplier_master SELECT client_no, name, adress1, adress2, city, state, pincode, remarks FROM 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 view ing 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 tablename Example: SELECT client_no, client_name FROM client_master; SELECTED ROWS AND ALL COLUMNS When a WHERE clause is addeded to the SQL sentence, the Oracle server co mpares each record from the table with the condition specified in the where clause. Ora cle displays the records that satisfies the specified condition I Syntax: SELECT *

FROM

tablename WHERE search condition; Ex: SELECT * FROM client_master WHERE bal_due>0; SELECTED ROWS AND SELECTED COLUMNS I Syntax: SELECT columnname, columnname FROM tablename WHERE search condition; Ex: SELECT client_no , name FROM client_master WHERE bal_due>1000; ELLIMINATION OF DUPLICATES FROM THE SELECT STATEMENT I Syntax: SELECT DISTINCTcolumnname, columnname FROM 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 re trieved from the table will be sorted in either ascending or descending order depending on the co ndition specified in the select sentence I Syntax: SELECT * FROM tablename ORDER BY columnname, columnname [sort order) DELETE OPERATIONS DELETE: Delete the data from the table Syntax: 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 back TRUNCATE is faster and doesn't use as much undo space as a DELETE. Ex1: Truncate table emp; table truncated. Ex2: Select count(*) from emp;

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 table 2.To select set of rows from a table UPDATING ALL THE ROWS Syntax: UPDATE table name SET columnname=expression, columnname=expression Example Update emp_master SET netsal=net_sal+basic_sal*0.15 UPDATING RECORDS CONDITIONALLY Syntax: UPDATE table name SET columnname=expression, columnname=expression. WHERE columnname =expression; Example Update emp_master SET netsal=net_sal+basic_sal*0.15 WHERE client_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 database 2. REVOKE - withdraw access privileges given with the GRANT command TCL - Transaction Control:

Statements used to manage the changes made by DML statements. It allows statemen ts to be grouped 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 what rollback 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 transacti on to 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 to its 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.

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

Creating our OWN users:1) Boot with win 98 2) Click start?prog?Personal oracle 7 for windows 95/98/97?SQL * 7.3 3) Click on personal oracle 7 4) Click on local databses 5) Right click on user 6) select New 7) Give user name/password;

Potrebbero piacerti anche