0% found this document useful (0 votes)
15 views11 pages

Dbms 4

The document shows SQL commands used to create tables, insert data, and perform queries on the tables. It creates tables for storing product inventory and expiration data, inserts sample data, and performs queries and updates on the tables. Errors encountered during the process are also displayed.

Uploaded by

Rishitha Reddy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views11 pages

Dbms 4

The document shows SQL commands used to create tables, insert data, and perform queries on the tables. It creates tables for storing product inventory and expiration data, inserts sample data, and performs queries and updates on the tables. Errors encountered during the process are also displayed.

Uploaded by

Rishitha Reddy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 11

SQL> CREATE TABLE retail_store (

2 Item_No INT PRIMARY KEY,


3 Item_Name VARCHAR2(100) NOT NULL,
4 Price NUMBER(10, 2) NOT NULL CHECK (Price <> 0),
5 Stock INT NOT NULL CHECK (Stock <> 0),
6 ExpCode INT,
7 CONSTRAINT FK_retail_store_exp FOREIGN KEY (ExpCode) REFERENCES exp
(ExpCode)
8 );

Table created.

SQL> CREATE TABLE exp (


2 ExpCode INT PRIMARY KEY,
3 Manuf_Date DATE NOT NULL,
4 Exp_Date DATE NOT NULL
5 );
CREATE TABLE exp (
*
ERROR at line 1:
ORA-00955: name is already used by an existing object

SQL> drop table exp;


drop table exp
*
ERROR at line 1:
ORA-02449: unique/primary keys in table referenced by foreign keys

SQL> select * from tab;

TNAME TABTYPE CLUSTERID


------------------------------ ------- ----------
BANK TABLE
BOOKS TABLE
DEPT TABLE
EMP TABLE
EMPLOYEE TABLE
EXP TABLE
RETAIL_STORE TABLE

7 rows selected.

SQL> INSERT INTO exp (ExpCode, Manuf_Date, Exp_Date)


2 VALUES (12, TO_DATE('01-Jan-2016', 'dd-Mon-yyyy'), TO_DATE('01-Dec-2016', 'dd-
Mon-yyyy'));
INSERT INTO exp (ExpCode, Manuf_Date, Exp_Date)
*
ERROR at line 1:
ORA-00001: unique constraint (DSA.SYS_C007207) violated

SQL>
SQL> INSERT INTO exp (ExpCode, Manuf_Date, Exp_Date)
2 VALUES (21, TO_DATE('03-Mar-2016', 'dd-Mon-yyyy'), TO_DATE('24-Nov-2016', 'dd-
Mon-yyyy'));
INSERT INTO exp (ExpCode, Manuf_Date, Exp_Date)
*
ERROR at line 1:
ORA-00001: unique constraint (DSA.SYS_C007207) violated

SQL>
SQL> INSERT INTO exp (ExpCode, Manuf_Date, Exp_Date)
2 VALUES (32, TO_DATE('14-Feb-2016', 'dd-Mon-yyyy'), TO_DATE('30-Dec-2016', 'dd-
Mon-yyyy'));
INSERT INTO exp (ExpCode, Manuf_Date, Exp_Date)
*
ERROR at line 1:
ORA-00001: unique constraint (DSA.SYS_C007207) violated

SQL> select * from exp;

EXPCODE MANUF_DAT EXP_DATE


---------- --------- ---------
12 01-JAN-16 01-DEC-16
21 03-MAR-16 24-NOV-16
32 14-FEB-16 30-DEC-16

SQL> INSERT INTO retail_store (Item_No, Item_Name, Price, Stock, ExpCode)


2 VALUES (1, 'Spoon', 50.5, 6754, 12);

1 row created.

SQL>
SQL> INSERT INTO retail_store (Item_No, Item_Name, Price, Stock, ExpCode)
2 VALUES (2, 'Plate', 65.5, 8965, 21);

1 row created.

SQL>
SQL> INSERT INTO retail_store (Item_No, Item_Name, Price, Stock, ExpCode)
2 VALUES (3, 'Glass', 36.0, 8965, 12);

1 row created.

SQL>
SQL> INSERT INTO retail_store (Item_No, Item_Name, Price, Stock, ExpCode)
2 VALUES (4, 'Mattress', 8655, 190, 21);

1 row created.

SQL>
SQL> INSERT INTO retail_store (Item_No, Item_Name, Price, Stock, ExpCode)
2 VALUES (5, 'Chair', 918, 77, 21);

1 row created.

SQL>
SQL> INSERT INTO retail_store (Item_No, Item_Name, Price, Stock, ExpCode)
2 VALUES (6, 'Bowl', 98.5, 453, 21);

1 row created.

SQL>
SQL> INSERT INTO retail_store (Item_No, Item_Name, Price, Stock, ExpCode)
2 VALUES (7, 'Cot', 25000, 12, 32);

1 row created.

SQL>
SQL> INSERT INTO retail_store (Item_No, Item_Name, Price, Stock, ExpCode)
2 VALUES (8, 'Sofa', 67000, 32, 12);

1 row created.

SQL>
SQL> INSERT INTO retail_store (Item_No, Item_Name, Price, Stock, ExpCode)
2 VALUES (9, 'Cupboard', 45000, 11, 32);

1 row created.

SQL>
SQL> INSERT INTO retail_store (Item_No, Item_Name, Price, Stock, ExpCode)
2 VALUES (10, 'Fan', 1000, 679, 12);

1 row created.

SQL> select Price,Item_Name,Item_No from retail_store;

PRICE
----------
ITEM_NAME
--------------------------------------------------------------------------------
ITEM_NO
----------
50.5
Spoon
1

65.5
Plate
2

PRICE
----------
ITEM_NAME
--------------------------------------------------------------------------------
ITEM_NO
----------

36
Glass
3

8655
Mattress

PRICE
----------
ITEM_NAME
--------------------------------------------------------------------------------
ITEM_NO
----------
4
918
Chair
5

98.5

PRICE
----------
ITEM_NAME
--------------------------------------------------------------------------------
ITEM_NO
----------
Bowl
6

25000
Cot
7

PRICE
----------
ITEM_NAME
--------------------------------------------------------------------------------
ITEM_NO
----------
67000
Sofa
8

45000
Cupboard
9

PRICE
----------
ITEM_NAME
--------------------------------------------------------------------------------
ITEM_NO
----------

1000
Fan
10

10 rows selected.

SQL> update retail_store


2 if stock<80 then
3 set stock=stock+10;
if stock<80 then
*
ERROR at line 2:
ORA-00971: missing SET keyword

SQL> show errors;


No errors.
SQL> update retail_store
2 set stock=stock+10 if stock<80;
set stock=stock+10 if stock<80
*
ERROR at line 2:
ORA-00933: SQL command not properly ended

SQL> update retail_store


2 if stock<80 then set stock=stock+10 ;
if stock<80 then set stock=stock+10
*
ERROR at line 2:
ORA-00971: missing SET keyword

SQL> update retail_store


2 if set stock<80 then stock=stock+10 ;
if set stock<80 then stock=stock+10
*
ERROR at line 2:
ORA-00927: missing equal sign

SQL> update retail_store


2 set stock=stock+10
3 where stock<80;

4 rows updated.

SQL> select Item_Name from retail_store JOIN exp ON (ExpCode=ExpCode)


2 where Exp_Date>'25-Nov-2016';
select Item_Name from retail_store JOIN exp ON (ExpCode=ExpCode)
*
ERROR at line 1:
ORA-00918: column ambiguously defined

SQL> SELECT Item_No, Item_Name, Price, Stock


2 FROM retail_store
3 WHERE ExpCode IN (
4 SELECT ExpCode
5 FROM exp
6 WHERE Exp_Date > TO_DATE('25-Nov-2016', 'dd-Mon-yyyy')
7 );

ITEM_NO
----------
ITEM_NAME
--------------------------------------------------------------------------------
PRICE STOCK
---------- ----------
1
Spoon
50.5 6754

3
Glass
36 8965

ITEM_NO
----------
ITEM_NAME
--------------------------------------------------------------------------------
PRICE STOCK
---------- ----------

7
Cot
25000 22

8
Sofa

ITEM_NO
----------
ITEM_NAME
--------------------------------------------------------------------------------
PRICE STOCK
---------- ----------
67000 42

9
Cupboard
45000 21

10

ITEM_NO
----------
ITEM_NAME
--------------------------------------------------------------------------------
PRICE STOCK
---------- ----------
Fan
1000 679

6 rows selected.

SQL> select manuf_date,exp_date from retail_store


2 where Item_no IN (3,5,7,9)=(select expcode from exp);
where Item_no IN (3,5,7,9)=(select expcode from exp)
*
ERROR at line 2:
ORA-00933: SQL command not properly ended

SQL> select Item_Name from retail_store JOIN exp ON retail_store=ExpCode


2 where retail_store.Item_no IN (3,5,7,9);
select Item_Name from retail_store JOIN exp ON retail_store=ExpCode
*
ERROR at line 1:
ORA-00918: column ambiguously defined

SQL> select Item_Name from retail_store JOIN exp ON


retail_store.ExpCode=exp.expcode
2 where retail_store.Item_no IN (3,5,7,9);

ITEM_NAME
--------------------------------------------------------------------------------
Glass
Chair
Cot
Cupboard

SQL> select count(Item_No) from retail_store


2 group by expcode;

COUNT(ITEM_NO)
--------------
4
2
4

SQL> select Item_name from retail_store


2 order by prize;
order by prize
*
ERROR at line 2:
ORA-00904: "PRIZE": invalid identifier

SQL> select Item_name from retail_store


2 order by Prize;
order by Prize
*
ERROR at line 2:
ORA-00904: "PRIZE": invalid identifier

SQL> select * from retail_store;

ITEM_NO
----------
ITEM_NAME
--------------------------------------------------------------------------------
PRICE STOCK EXPCODE
---------- ---------- ----------
1
Spoon
50.5 6754 12

2
Plate
65.5 8965 21

ITEM_NO
----------
ITEM_NAME
--------------------------------------------------------------------------------
PRICE STOCK EXPCODE
---------- ---------- ----------

3
Glass
36 8965 12

4
Mattress

ITEM_NO
----------
ITEM_NAME
--------------------------------------------------------------------------------
PRICE STOCK EXPCODE
---------- ---------- ----------
8655 190 21

5
Chair
918 87 21

ITEM_NO
----------
ITEM_NAME
--------------------------------------------------------------------------------
PRICE STOCK EXPCODE
---------- ---------- ----------
Bowl
98.5 453 21

7
Cot
25000 22 32

ITEM_NO
----------
ITEM_NAME
--------------------------------------------------------------------------------
PRICE STOCK EXPCODE
---------- ---------- ----------
8
Sofa
67000 42 12

9
Cupboard
45000 21 32

ITEM_NO
----------
ITEM_NAME
--------------------------------------------------------------------------------
PRICE STOCK EXPCODE
---------- ---------- ----------

10
Fan
1000 679 12
10 rows selected.

SQL> select Item_name from retail_store


2 order by prize ASC;
order by prize ASC
*
ERROR at line 2:
ORA-00904: "PRIZE": invalid identifier

SQL> SELECT Item_No, Item_Name, Price, Stock


2 FROM retail_store
3 ORDER BY Price ASC;

ITEM_NO
----------
ITEM_NAME
--------------------------------------------------------------------------------
PRICE STOCK
---------- ----------
3
Glass
36 8965

1
Spoon
50.5 6754

ITEM_NO
----------
ITEM_NAME
--------------------------------------------------------------------------------
PRICE STOCK
---------- ----------

2
Plate
65.5 8965

6
Bowl

ITEM_NO
----------
ITEM_NAME
--------------------------------------------------------------------------------
PRICE STOCK
---------- ----------
98.5 453

5
Chair
918 87

10

ITEM_NO
----------
ITEM_NAME
--------------------------------------------------------------------------------
PRICE STOCK
---------- ----------
Fan
1000 679

4
Mattress
8655 190

ITEM_NO
----------
ITEM_NAME
--------------------------------------------------------------------------------
PRICE STOCK
---------- ----------
7
Cot
25000 22

9
Cupboard
45000 21

ITEM_NO
----------
ITEM_NAME
--------------------------------------------------------------------------------
PRICE STOCK
---------- ----------

8
Sofa
67000 42

10 rows selected.

SQL> select * from retail_store


2 group by expdate
3 ;
group by expdate
*
ERROR at line 2:
ORA-00904: "EXPDATE": invalid identifier

SQL> SELECT ExpCode, COUNT(*) AS Item_Count


2 FROM retail_store
3 GROUP BY ExpCode;

EXPCODE ITEM_COUNT
---------- ----------
21 4
32 2
12 4
SQL> spool off;

You might also like