PROGRAM : 1 Aim : To create a new user account SQL> -- TO CREATE A NEW USER ACCOUNT SQL>CONNECT SCOTT/TIGER Connected. SQL> CREATE USER PUNEET IDENTIFIED BY RAJ; User created. SQL> GRANT CONNECT, RESOURCE TO PUNEET; Grant succeeded. SQL> CONNECT PUNEET/RAJ Connected. SQL>CONNECT SYSTEM/MANAGER Connected. SQL> ALTER USER PUNEET IDENTIFIED BY SANJAY; User altered. SQL> CONNECT PUNEET/SANJAY Connected. 1
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
PROGRAM : 1
Aim : To create a new user account
SQL> -- TO CREATE A NEW USER ACCOUNT
SQL>CONNECT SCOTT/TIGERConnected.
SQL> CREATE USER PUNEET IDENTIFIED BY RAJ;User created.
SQL> GRANT CONNECT, RESOURCE TO PUNEET;Grant succeeded.
SQL> CONNECT PUNEET/RAJ Connected.
SQL>CONNECT SYSTEM/MANAGER
Connected.SQL> ALTER USER PUNEET IDENTIFIED BY SANJAY;User altered.
SQL> CONNECT PUNEET/SANJAY Connected.
1
PROGRAM : 2AIM: TO CREATE, INSERT, DISPLAY, DELETE,
UPDATE ,DROP A GIVEN TABLESQL> -- TO CREATE, INSERT, DISPLAY, DELETE, UPDATE ,DROP A GIVEN TABLE SQL> CONNECT MANVEE/MANU Connected.SQL> CREATE TABLE FRIENDS(SERIAL NUMBER(3),NAME VARCHAR2(20), AGE NUMBER(2), INCOME NUMBER(6));
Table created.
SQL> DESC FRIENDS; Name Null? Type---------------------SERIAL NUMBER(3)NAME VARCHAR2(20) AGE NUMBER(2)INCOME NUMBER(6)
SQL> INSERT INTO FRIENDS VALUES(1,'PUNEET',19,20000);
1 row created.
SQL> INSERT INTO FRIENDS VALUES(2,'RICHA' ,20,12000);
1 row created.
SQL> INSERT INTO FRIENDS VALUES(3,'GAURAV',21,17000);
1 row created.
SQL> INSERT INTO FRIENDS VALUES(4,'ANKUSH',23,20000);
1 row created.
SQL> INSERT INTO FRIENDS VALUES(5,'PRINCE',19,30000);
1 row created.
SQL> SELECT * FROM FRIENDS;
SERIAL NAME AGE INCOME--------- -------------------- 1 PUNEET 19 120002 RICHA 20 12000 3 GAURAV 21 170004 ANKUSH 23 200005 PRINCE 19 30000
SQL> INSERT INTO FRIENDS(SERIAL,AGE,INCOME)VALUES(6,23,23000);
2
1 row created.
SQL> INSERT INTO FRIENDS(SERIAL,AGE,INCOME)VALUES(7,24,24000);
1 row created.
SQL> INSERT INTO FRIENDS(SERIAL,AGE,INCOME)VALUES(8,25,25000);
1 row created.
SQL> INSERT INTO FRIENDS(SERIAL,AGE,INCOME)VALUES(9,26,26000);
1 row created.
SQL> INSERT INTO FRIENDS(SERIAL,AGE,INCOME)VALUES(10,27,27000);
SQL> INSERT INTO FRIENDS VALUES(&SERIAL,'&NAME',&AGE,&INCOME);Enter value for serial: 11Enter value for name: FRIENDS Enter value for age: 19Enter value for INCOME: 19000 old 1: INSERT INTO FRIENDS VALUES(&SERIAL,'&NAME',&AGE,&INCOME) new 1: INSERT INTO FRIENDS VALUES(ll,'FRIENDS' ,19,19000)
1 row created.
SQL>/ Enter value for serial: 12.Enter value for name: RICHA Enter value for age: 20 Enter value for INCOME: 20000 old 1: INSERT INTO FRIENDS VALUES(&SERIAL,'&NAME',&AGE,&INCOME) new 1: INSERT INTO FRIENDS V ALUES(12,'RICHA',20,2QOOO)
1 row created.
3
SQL>/ Enter value for serial: 13 Enter value for name: GAURAV Enter value for age: 21 Enter value for INCOME: 21000 old 1: INSERT INTO FRIENDS VALUES(&SERIAL,'&NAME',&AGE,&INCOME) new 1: INSERT INTO FRIENDS VALUES(13,'GAURAV',21,21000)
1 row created.
SQL>/ Enter value for serial: 14 Enter value for name: ANKUSH Enter value for age: 22 Enter value for INCOME: 22000 old 1: INSERT INTO FRIENDS VALUES(&SERIAL,'&NAME',&AGE,&INCOME) new 1: INSERT INTO FRIENDS VALUES(14,'ANKUSH',22,22000)
1 row created.
SQL>/ Enter value for serial: 15 Enter value for name: PRINCE Enter value for age: 23 Enter value for INCOME: 23000 old 1: INSERT INTO FRIENDS VALUES(&SERIAL,'&NAME',&AGE,&INCOME) new 1: INSERT INTO FRIENDS VALUES(15,'PRINCE',23,23000)
SQL> SELECT PROD_NO, SUM(QTY _ ORDERED) "TOTAL QTY _ORDERED" FROM SALES GROUP BY PROD_NO HA VINGPROD _ NO='POOOO l' ;
P00001 24
8
PROGRAM : 4 AIM: TO CREATE TABLE WITH NOT NULL, PRIMARY,
FOREIGN, UNIQUE AND CHECK CONSTRAINTSSQL> --TO CREATE TABLE WITH NOT NULL, PRIMARY, FOREIGN, UNIQUE AND CHECK CONSTRAINTS SQL> CONNECT system/manager;Connected.SQL> CREATE TABLE STUD (NAME VARCHAR2(20), AGE NUMBER(2), ID NUMBER(6) PRIMARY KEY);
Table created.
SQL> DESC STUD Name Null? Type------------ ----- -----NAME VARCHAR2(20) AGE NUMBER(2) ID NOT NULL NUMBER(6)
SQL> INSERT INTO STUD VALUES('&NAME',&AGE,&ID);Enter value for name: PUNEETEnter value for age: 19 Enter value for id: 1 old 1 : INSERT INTO STUD VALUES('&NAME';"&AGE,&ID) new 1: INSERT INTO STUD VALUES(' PUNEET ',19,1)
1 row created.
SQL>/ Enter value for name: PUNEET Enter value for age: 21 Enter value for id: 1 old 1: INSERT INTO STUD VALUES('&NAME',&AGE,&ID) new 1: INSERT INTO STUD VALUES('PUNEET ',21,1) INSERT INTO STUD VALUES(' PUNEET ',21,1) * ERROR at line 1:ORA-OOOO1: unique constraint (PUNEET.SYS_C00400) violated
SQL> / Enter value for name: PUNEETEnter value for age: 20 Enter value for id: NULL old 1: INSERT INTO STUD VALUES('&NAME',&AGE,&ID) new 1: INSERT INTO STUD VALUES(' PUNEET ',20,NULL) INSERT INTO STUD V ALUES(' PUNEET ',20,NULL)
ERROR at line 1:ORA-01400: mandatory (NOT NULL) column is missing or NULL during insert
9
SQL> CREATE TABLE STUD1(NAME VARCHAR2(20),ID NUMBER(6), AGE NUMBER(2),PRIMARY KEY(ID,NAME));
Table created.
SQL> DESC STUDl; Name Null? Type------- ------ ------NAME NOT NULL VARCHAR2(20) ID NOT NULL NUMBER(6) AGE NUMBER(2)
SQL> INSERT INTO STUDl VALUES('&NAME',&ID,&AGE);Enter value for name: MAN,ISH Enter value for id: 1 Enter value for age: 19 old 1: INSERT INTO STUDl VALUES('&NAME',&ID,&AGE) new 1: INSERT INTO STUDl VALUES(' GULSHAN ',1,19)
1 row created.
SQL> / Enter value for name: RAHUL Enter value for id: 1 Enter value for age: 20 old 1: INSERT INTO STUDl VALUES('&NAME',&ID,&AGE) new 1: INSERT INTO STUDIV ALUES('RAHUL',1,20)
1 row created.
SQL> / Enter value for name: RAHUL Enter value for id: 1 Enter value for age: 20 old 1: INSERT INTO STUDl VALUES('&NAME',&ID,&AGE) new 1: INSERT INTO STUDl VALUES('RAHUL',1,20) INSERT INTO STUDl VALUES('RAHUL',1,20)*
ERROR at line 1:ORA-OOOO1: unique constraint (PUNEET.SYS_C00401) violated
SQL> CREATE TABLE STUD2(NAME V ARCHAR2(20),AGE NUMBER(2), ID NUMBER(2) UNIQUE);
Table created.
SQL>DESC STUD2; Name Null? Type------ ------------NAME VARCHAR2(20) AGE NUMBER(2) ID NUMBER(2)
10
SQL> INSERT INTO STUD2 VALUES('&NAME',&AGE,&ID);Enter value for name: MANISH Enter value for age: 19 Enter value for id: 1 old 1: INSERT INTO STUD2 VALUES('&NAME',&AGE,&ID) new 1: INSERT INTO STUD2 VALUES(' PUNEET ',19,1)
1 row created.
SQL> / Enter value for name: PUNEET Enter value for age: 20 Enter value for id: 1 old 1: INSERT INTO STUD2 YALUES('&NAME',&AGE,&ID) new 1: INSERT INTO STUD2 VALUES(' PUNEET ',20,1) INSERT INTO STUD2 V ALUES(' PUNEET ',20,1)
* ERROR at line 1:ORA-00001: unique constraint (PUNEET.SYS_C00402) violated
SQL> / Enter value for name: PUNEET Enter value for age: 21 Enter value for id: NULL old 1: INSERT INTO STUD2 VALUES('&NAME',&AGE,&ID) new 1: INSERT INTO STUD2 VALUES(' PUNEET ',21,NULL)
1 row created.
SQL> CREATE TABLE STUD3(NAME V ARCHAR2(20),ID NUMBER(6) NOT NULL, AGE NUMBER(2));
Table created.
SQL> DESC STUD3; Name Null? Type----- -----------NAME VARCHAR2(20) ID NOT NULL NUMBER(6) AGE NUMBER(2) SQL> INSERT INTO STUD3 V ALUES('&NAME',&ID,&AGE);Enter value for name: GULSHAN Enter value for id: NULL Enter value for age: 19 old 1: INSERT INTO STUD3 VALUES('&NAME',&ID,&AGE) new 1: INSERT INTO STUD3 VALUES(' PUNEET ',NULL, 19) INSERT INTO STUD3 VALUES(' PUNEET ',NULL,19)
*ERROR at line 1:ORA-01400: mandatory (NOT NULL) column is missing or NULL during insert
11
SQL> ALTER TABLE STUD3 ADD PRIMARY KEY(NAME);
Table altered.
SQL> CREATE TABLESTUD4(NAME VARCHAR2(20) REFERENCES STUD3, AGE NUMBER(2),ID NUMBER(6));
Table created.
SQL> ALTER TABLE STUD3 DROP PRIMARY KEY;
ALTER TABLE STUD3 DROP PRIMARY KEY
*
ERROR at line 1:ORA-02273: this unique/primary key is referenced by some foreign keys
SQL> ALTER TABLE STUD3 DROP PRIMARY KEY;ALTER TABLE STUD3 DROP. PRIMARY KEY
*
ERROR at line 1:ORA-02273: this unique/primary key is referenced by some foreign keys
SQL> CREATE TABLE STUD6(NAME V ARCHAR2(20) CHECK(NAME LIKE 'C%'),AGE NUMBER(2),ID NUMBER(6»;
Table created.
SQL> INSERT INTO STUD6 V ALUES('&NAME',&AGE,&ID);Enter value for name: PUNEET Enter value for age: 19 Enter value for id: 1 old 1: INSERT INTO STUD6 V ALUES('&NAME',&AGE,&ID) new 1: INSERT INTO STUD6 VALUES(' PUNEET ',19,1) INSERT INTO STUD6 VALUES(' PUNEET ',19,1)*
ERROR at line 1:ORA-02290: check constraint (PUNEET.SYS_C00407) violated
12
PROGRAM : 5AIM: TO MODIFY THE STRUCTURE OF TABLE BY
ADDIMODIFY COLUMN AND ADD/DROP PRIMARY, FOREIGN KEY
SQL> -- TO MODIFY THE STRUCTURE OF TABLE BY ADDIMODIFY COLUMN AND ADD/DROP PRIMARY, FOREIGN KEY
SQL> CONNECT SYSTEM/MANAGER;Connected.SQL> CREATE TABLE GULSHAN2(NAME V ARCHAR2(20), AGE NUMBER(2));
Table created.
SQL> DESC MANISH29;Name Null? Type---------- ----------NAME VARCHAR2(20) AGE NUMBER(2)
SQL> ALTER TABLE GULSHAN2 ADD (ROLL_NO VARCHAR2(6));
Table altered.
SQL> DESC MANISH29;Name Null? Type-------- --------------NAME VARCHAR2(20) AGE NUMBER(2) ROLL NO VARCHAR2(6)
SQL> ALTER TABLE GULSHAN2 MODIFY(ROLL_NO NUMBER(6));
Table altered.
SQL> DESC MANISH29;Name Nui1? Type--------- -------------NAME VARCHAR2(20) AGE NUMBER(2) ROLL NO NUMBE~J6)
PROGRAM : 6AIM: TO PERFORM RENAMING COLUMNS, RANGE
SEARCHING, PATTERN MATCHING, USE OF DEFAULT AND LOGICAL OPERATORS ON A TABLE
SQL> -- TO PERFORM RENAMING COLUMNS, RANGE SEARCHING, PATTERN MATCHING, USE OF DEFAULT AND LOGICAL OPERATORS ON A TABLE SQL> CONNECT SYSTEM/MANAGER;Connected.SQL> DESC STUD;Name Null? Type
NAME VARCHAR2(20) AGE NUMBER(2) ID NOT NULL NUMBER(6)
SQL> SELECT * FROM STUD;
NAME AGE ID---------- ------ ----MANISH 19 1
SQL> INSERT INTO STUD VALUES('&NAME',&AGE,&ID);Enter value for name: RAJ Enter value for age: 19 Enter value for id: 2 old 1: INSERT INTO STUD V LUES('&NAME',&AGE,&ID) new 1: INSERT INTO STUD VALUES ('RAJ', 19,2)
1 row created.
SQL> / Enter value for name: GAURAV Enter value for age: 19 Enter value for id: 3 old 1: INSERT INTO STUD V ALUES('&NAME',&AGE,&ID) new 1: INSERT INTO STUD VALUES('GAURAV',19,3)
1 row created.
SQL>/ Enter value for name: RAHUL Enter value for age: 19 Enter value for id: 4 old 1: INSERT INTO STUD VALUES('&NAME',&AGE,&ID) new 1: INSERT INTO STUD VALUES('RAHUL',19,4)1 row created.
SQL> /Enter value for name: RAGHAV
16
Enter value for age: 20' Enter value for id: 5 old 1: INSERT INTO STUD VALUES('&NAME',&AGE,&ID) new 1: INSERT INTO STUD V ALUES('RAGHAV',20,5)
I row created.
SQL>/ Enter value for name: VISHAL Enter value for age: 21 Enter value for id: 6 old 1: INSERT INTO STUD V ALUES('&NAME',&AGE,&ID) new 1: INSERT INTO STUD V ALUES('VISHAL',21 ,6)
NAME AGE ID--------- ------- ----MANISH 19 1 VISHAL 21 6
SQL> SELECT * FROM STUD WHERE AGE=20 AND ID=l;
no rows selected
SQL> SELECT * FROM STUD WHERE AGE=20 AND ID=5;
NAME AGE ID ----- ------- ---RAGHAV 20 5
SQL> SELECT * FROM STUD WHERE AGE NOT=21;SELECT * FROM STUD WHERE AGE NOT=21 *
ERROR at line 1:ORA-00920: invalid relational operator
SQL> SELECT * FROM STUD WHERE NOT AGE=19;
NAME AGE ID------------ ------ ------RAGHAV 20 5 VISHAL 21 6
SQL> SELECT * FROM STUD WHERE ID BETWEEN 3 AND 6;
NAME AGE ID --------- ------- ----GAURAV 19 3 RAHUL 19 4 RAGHAV 20 5 VISHAL 21 6
SQL> SELECT * FROM STUD WHERE ID NOT BETWEEN 3 AND 6;
NAME AGE ID--------- -------- ------MANISH 19 1 RAJ 19 2
18
SQL> SELECT * FROM STUD WHERE NAME LIKE '_A%';
NAME AGE ID------------ ----- ------MANISH 19 1 RAJ 19 2 GAURAV 19 3 RAHUL 19 4 RAGHAV 20 5
SQL> SELECT * FROM STUD WHERE NAME NOT LIKE'_A%';
NAME AGE ID----------- ------- ------VISHAL 21 6
PROGRAM : 7AIM: ILLUSTRATE THE USE OF ORACLE FUNCTIONS
SQL> -- ILLUSTRATE THE USE OF ORACLE FUNCTIONS SQL> CONNECT SYSTEM/MANAGER;Connected.SQL> DESC STUD; Name Null? Type---------- --------- -----NAME VARCHAR2(20) AGE NUMBER(2) ID NOT NULL NUMBER(6)
SQL> CREATE USER ORANGE IDENTIFIED BY BLUES; User created.
SQL> GRANT CONNECT, RESOURCE TO ORANGE;
Grant succeeded.
SQL> CONNECT SCOTT/TIGER;Connected.SQL> SELECT * FROM STUDl;
NAME ID AGE----------- ----- -----MANISH 1 19 RAHUL 1 20
SQL> CONNECT SYSTEM/MANAGER;Connected.SQL> REVOKE ALL ON STUD 1 FROM SCOTT;
Revoke succeeded.
24
INTRODUCTION TO DATA: Data is defined as the representation of facts, concepts of
instructions in a formal manner suitable for communication or processing by human's or by automatic means. Data refers to which is actually stored in database. Data are collection of unorganized facts but able to be organized in to useful information. Data can be of following different types: -
1. INTEGRATED DATA:A collection of data is integrated if data is logically consistent i.e. the
duplicate data items are agree with one another. A database can be considered as the unification of several distinct data files, with any redundancy among those files are eliminated. Data is said to be integrated data.
2. SHARED DATA:By shared we mean that individual pieces of data in the database can
be shared among different users, in the sense that each of those users can have access to same piece of data, possibly for different purposes. Different users can effectively even access the same piece of data at the same time.
INTRODUCTION TO DATABASE:-
A database can be defined as a collection of nonredundant data which can be shared by different application systems. A database implies separation of physical storage from use of the data by an application program to achieve program/data independence. Using a database system, the user or programmer or application specialist need not know the details of how the data are stored and such details are usually "transparent" to the user.
ADVANTAGES OF DATABASE
· Reduction in data redundancy· Maintenance of data integrity and quality
· Avoidance of inconsistency
· Reduced cost of software development
· Security restrictions
View of the Database
.The database can present different views of itself to users, programmers and the database administrator.
·The internal data rep~esentation (internal view) is normally not seen by the user or application programmer.
.Theconceptual view or conceptual schema is the primary means by which the DBA builds and manages the database. The DBMS can present multiple views of the conceptual schema (external views) to programmers and users, depending on the application.
25
INTRODUCTION TO DATABASE MANAGEMENT SYSTEM
A database management system (DBMS) consists of collection of interrelated data and a set of programs to access that data. Th~ collection of data, usually referred to as the database contains information about one particular enterprise The primary goal of a DBMS is to provide an environment that is both convenient and efficient to use in retrieving and storing database information.
DBMS COMPONENTS
Data: -Data stored in a database include numerical data which may be integers (whole numbers only) or floating point numbers (decimal), and non-numerical data such as characters (alphabetic and numeric characters), date or logical (true or false). More advanced systems may include more complicated data entities such as pictures and images as data types. .
Standard operations: Standard operations are provided by most DBMSs. These operations provide the user with basic capabilities for data manipulation. Examples of these standard operations are sorting, deleting and selecting records.
Data definition language (DDL): DOL is the language used to describe the contents of the database. It is used to describe, for example, attribute names (field names), data types, location in the database, etc.
Data manipulation and query language: Normally a query language is supported by a DBMS to form commands for input, edit, analysis, output, reformatting, etc. Some degree of standardization has been achieved with SQL (Structured Query Language).
Programming tools: Besides commands and queries, the database should be accessible directly from application programs through function calls (subroutine calls) in conventional programming languages.
File structures: Every DBMS has its own internal structures used to organize the data although some common data models are used by most DBMSs.
ADVANTAGES OF DBMS: -
1. Controlling redundancy: Each user group maintains its own files for handling its data
processing applications. Much of data is stored twice: once in the files of each user group. Additional user group may further duplicate some or all of data multiple times leads to several problems. Storage space is waisted when details stored repeated, and this problem may be serious for large databases. So DBMS helps in controlling redundancy.
2. Restricting unauthorized access: -
When multiple users share a database, it is likely that some users will not be authorized to access all information in the database. Eg:-financial
26
database is often considered, and hence only authorized person can access data.In addition some users may be permitted only to retrieve data, whereas others are allowed both to retrieve and t? update. Typically, users or some users group are given account numbers protected by some passwords, which they can be used to access the database. A DBMS should provide a security and authorization subsystem.
3. Providing persistent storage for programs objects and data structures: Database can be used to provide persistent storagefor programs object and data structures. This is one of the main 'reasons for the emergence of the object oriented database systems.Programming language typically have complex data structure such as record types in Pascal or class definitions in C++. The values of program vafiables are discarded once a program terminates, unless the programmer explicitly stores them in permanents files, which often involves converting these complex structures into a format suitable for file storage. When the need arise to read this a data once more, the programmer must convert from the file format to the program variable structure. DBMS software automatically any necessary converSIOns.
4. Permitting Interfacing and actions using rules: Some database system provides capabilities for defining deduction rules for interferencing new information from the stored database facts. Such systems are called deductive database systems.
5. Providing multiple user interfaces: Because many types of users with varying levels of technical knowledge use a database, aDBMS should provide a variety of user interfaces. These includes query languages for casual users, programming language interfaces for application programmers, forms and commands code for parametric users, and menu driven interfaces are commonly known as graphical user interfaces.
6. Representing complex relationships among data: A database may include numerous varieties of data that are inter elated in many ways. A DBMS must have the capabilities to represents a variety of complex relationships among data as well as to retrieve and update rdated data easily and effectively.
7. Enforcing integrity constraints: Most database applications have certain integrity constraints that must hold for the data. A DBMS should provide capabilities for defining and enforcing these constraints. The simplest type of integrity constraints involves specifying a data type for each data item. Some constraints can be specified to the DBMS and automatically enforced. Others constraints may have to be checked by update programs or at the time of data entry.
8. Providing' backup and recovery:A DBMS must provide facilities for recovering from hardware or software failures. The backup and recovery system of the DBMS is responsible for recovery. eg: - if the computer system fails in the middle of a complex update program, the recovery subsystem is responsible for making sure
27
that the database is restored to the state it was in before the program started executing.
INTRODUCTION TO DATABASE SYSTEM:
A database system involves four major components: data, hardware, software, and users.
DATA: Data is defined as the representation of facts, concepts of instructions in a formal manner suitable for communication or processing by human's or by automatic means. Data refers to which is actually stored in database. Data are collection of unorganized facts but able to be organized in to useful information. Data can be of following different types: 1. INTEGRATED DATA: A collection of data is integrated if data is logically consistent i.e. the duplicate data items are agree with one another. A database can be considered as the unification of several distinct data files, with any redundancy among those files are eliminated. Data is said to be integrated data.2. SHARED DATA:By shared we mean that individual pieces of data in the database can be shared among different users, in the sense that each of those users can have access to same piece of data, possibly for different purposes. Different users can effectively even access the same piece of data at the same time.
HARDWARE: The hardware components of the system consist of:1. The secondary storage volunies most magnetic disks that are used to hold the stored data, together with the I/O devices (disc drives etc.) device controllers I/O channels.2. The hardware processor and associated main memory that are used to support the execution of the database system software.
SOFTWARE: Between the physical database itself i.e. the data as physically stored and the users of the system is a layer of software, known variously as the database manager or database server or most commonly, the database management system. All request for access to database are handled by the DBMS like for adding or removing files or tables, retrieving data from· and updating data is such files or tables, these all facilities are provided by the DBMS. One general function provided by the DBMS is the shielding of the database users from hardware level details much as programming language system shield application programmers from hardware level details.