Day 1

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 14

SQL> -- creating product

SQL> create table product ( pid number(3) , pname varchar(20) not null ,
2 price number not null , quantity number(3) not null,
3 primary key(pid) );

Table created.

SQL> desc product;


Name Null? Type
----------------------------------------- -------- ----------------------------
PID NOT NULL NUMBER(3)
PNAME NOT NULL VARCHAR2(20)
PRICE NOT NULL NUMBER
QUANTITY NOT NULL NUMBER(3)

SQL> ---to know about constraints


SQL> set lines 3000
SQL> select *
2 from user_constraints;

OWNER CONSTRAINT_NAME C TABLE_NAME


SEARCH_CONDITION
R_OWNER
------------------------------ ------------------------------ -
------------------------------ -----
SCOTT SYS_C007633 C PRODUCT
"PNAME" IS NOT NULL

SCOTT SYS_C007634 C PRODUCT


"PRICE" IS NOT NULL

SCOTT SYS_C007635 C PRODUCT


"QUANTITY" IS NOT NULL

SCOTT SYS_C007020 R DUM


SCOTT
SCOTT FK_DEPTNO R EMP
SCOTT
SCOTT PK_DEPT P DEPT

SCOTT PK_EMP P EMP

SCOTT SYS_C007019 U EMPLL

SCOTT SYS_C007636 P PRODUCT

9 rows selected.

SQL> select *
2 from user_constraints
3 where table_name = 'PRODUCT';

OWNER CONSTRAINT_NAME C TABLE_NAME


SEARCH_CONDITION
R_OWNER
------------------------------ ------------------------------ -
------------------------------ -----
SCOTT SYS_C007633 C PRODUCT
"PNAME" IS NOT NULL

SCOTT SYS_C007634 C PRODUCT


"PRICE" IS NOT NULL

SCOTT SYS_C007635 C PRODUCT


"QUANTITY" IS NOT NULL

SCOTT SYS_C007636 P PRODUCT

SQL> --- CREATING CUSTOMER TABLE


SQL> CREATE TABLE CUSTOMER ( CID NUMBER(2) PRIMARY KEY,
2 CNAME VARCHAR(20) NOT NULL,
3 CITY VARCHAR(10) NOT NULL
4 PINCODE NUMBER(6) NOT NULL);
PINCODE NUMBER(6) NOT NULL)
*
ERROR at line 4:
ORA-00907: missing right parenthesis

SQL> CREATE TABLE CUSTOMER ( CID NUMBER(2) PRIMARY KEY,


2 CNAME VARCHAR(20) NOT NULL,
3 CITY VARCHAR(10) NOT NULL,
4 PINCODE NUMBER(6) NOT NULL);

Table created.

SQL> SET LINES 100


SQL> DESC CUSTOMER;
Name Null? Type
----------------------------------------------------- --------
------------------------------------
CID NOT NULL NUMBER(2)
CNAME NOT NULL VARCHAR2(20)
CITY NOT NULL VARCHAR2(10)
PINCODE NOT NULL NUMBER(6)

SQL> -- ORDERS TABLE


SQL> CREATE TABLE ORDERS ( PRIMARY KEY(ORID) ,
2 ORID NUMBER(2) ,
3 CUST_ID NUMBER(2) REFERENCES CUSTOMER(CID),
4 PROD_ID NUMBER(3) ,
5 CONSTRAINT FK_ORDERS FOREIGN KEY(PROD_ID) PRODUCT (PID),
6 ORDERED_DATE DATE CONSTRAINT NN_ORDERS NOT NULL,
7 DELIVER_DATE DATE NOT NULL);
CONSTRAINT FK_ORDERS FOREIGN KEY(PROD_ID) PRODUCT (PID),
*
ERROR at line 5:
ORA-00905: missing keyword

SQL> ED
Wrote file afiedt.buf

1 CREATE TABLE ORDERS ( PRIMARY KEY(ORID) ,


2 ORID NUMBER(2) ,
3 CUST_ID NUMBER(2) REFERENCES CUSTOMER(CID),
4 PROD_ID NUMBER(3) ,
5 CONSTRAINT FK_ORDERS FOREIGN KEY(PROD_ID) REFERENCES
PRODUCT (PID),
6 ORDERED_DATE DATE CONSTRAINT NN_ORDERS NOT NULL,
7* DELIVER_DATE DATE NOT NULL)
SQL> /

Table created.

SQL> SET LINES 3000


SQL> SELECT *
2 FROM USER_CONSTRAINTS
3 WHERE TABLE_NAME = 'ORDERS';

OWNER CONSTRAINT_NAME C TABLE_NAME


SEARCH_CONDITION
R_OWNER
------------------------------ ------------------------------ -
------------------------------ -----
SCOTT NN_ORDERS C ORDERS
"ORDERED_DATE" IS NOT NULL

SCOTT SYS_C007642 C ORDERS


"DELIVER_DATE" IS NOT NULL

SCOTT SYS_C007643 P ORDERS

SCOTT SYS_C007644 R ORDERS


SCOTT
SCOTT FK_ORDERS R ORDERS
SCOTT

SQL>
SQL> INSERT ALL
2 INTO PRODUCT VALUES ( 1 ,'IPHONE11' ,39999,10)
3 INTO PRODUCT VALUES ( 2 ,'IPHONE12' ,49999, 4)
4 INTO PRODUCT VALUES ( 3 ,'IPHONE13' ,59999, 6)
5 INTO PRODUCT VALUES ( 4 ,'IPHONE14' ,69999, 2)
6 INTO PRODUCT VALUES ( 5 ,'IPHONE15' ,79999, 7)
7 INTO CUSTOMER VALUES (1 , 'ANIL' , 'HYD' ,500082)
8 INTO CUSTOMER VALUES (2 , 'ABHI' , 'CHN' ,600001)
9 INTO CUSTOMER VALUES (3 , 'AKHIL' , 'BNG' ,500001)
10 INTO CUSTOMER VALUES (4, 'RANI' , 'HYD' , 500072)
11 SELECT *
12 FROM DUAL;

9 rows created.

SQL> SELECT *
2 FROM PRODUCT;

PID PNAME PRICE QUANTITY


---------- -------------------- ---------- ----------
1 IPHONE11 39999 10
2 IPHONE12 49999 4
3 IPHONE13 59999 6
4 IPHONE14 69999 2
5 IPHONE15 79999 7
SQL> SELECT *
2 FROM CUSTOMER;

CID CNAME CITY PINCODE


---------- -------------------- ---------- ----------
1 ANIL HYD 500082
2 ABHI CHN 600001
3 AKHIL BNG 500001
4 RANI HYD 500072

SQL> COMMIT;

Commit complete.

SQL> /*
SQL> COPY / DUPLICATE THE TABLE BY USING CREATE
SQL> SYNTAX:
SQL> CREATE TABLE TABLE_NAME
SQL> AS
SQL> SELECT STATEMENT;
SQL>
SQL> ---- WITH RECORDS
SQL> EXAMPLE :
SQL> CREATE TABLE PRODUCT_C
SQL> AS
SQL> SELECT *
SQL> FROM PRODUCT;
SQL> -- WITHOUT RECORDS
SQL> EXAMPLE :
SQL> CREATE TABLE PRODUCT_D
SQL> AS
SQL> SELECT *
SQL> FROM PRODUCT
SQL> WHERE 1=0;
SQL> */
SQL> CREATE TABLE PRODUCT_C
2 AS
3 SELECT *
4 FROM PRODUCT;

Table created.

SQL> SELECT *
2 FROM CAT;

TABLE_NAME TABLE_TYPE
------------------------------ -----------
DEPT TABLE
EMP TABLE
BONUS TABLE
SALGRADE TABLE
STUDENT TABLE
EMPLL TABLE
E TABLE
DUM TABLE
PRODUCT TABLE
CUSTOMER TABLE
ORDERS TABLE
TABLE_NAME TABLE_TYPE
------------------------------ -----------
PRODUCT_C TABLE

12 rows selected.

SQL> SELECT *
2 FROM PRODUCT_C;

PID PNAME PRICE QUANTITY


---------- -------------------- ---------- ----------
1 IPHONE11 39999 10
2 IPHONE12 49999 4
3 IPHONE13 59999 6
4 IPHONE14 69999 2
5 IPHONE15 79999 7

SQL> SET LINES 3000


SQL> SELECT *
2 FROM USER
3
SQL> SELECT *
2 FROM USER_CONSTRAINTS
3 WHERE TABLE_NAME = 'PRODUCT_C';

OWNER CONSTRAINT_NAME C TABLE_NAME


SEARCH_CONDITION
R_OWNER
------------------------------ ------------------------------ -
------------------------------ -----
SCOTT SYS_C007646 C PRODUCT_C
"PNAME" IS NOT NULL

SCOTT SYS_C007647 C PRODUCT_C


"PRICE" IS NOT NULL

SCOTT SYS_C007648 C PRODUCT_C


"QUANTITY" IS NOT NULL

SQL> SELECT *
2 FROM USER_CONSTRAINTS
3 WHERE TABLE_NAME = 'PRODUCT';

OWNER CONSTRAINT_NAME C TABLE_NAME


SEARCH_CONDITION
R_OWNER
------------------------------ ------------------------------ -
------------------------------ -----
SCOTT SYS_C007633 C PRODUCT
"PNAME" IS NOT NULL

SCOTT SYS_C007634 C PRODUCT


"PRICE" IS NOT NULL

SCOTT SYS_C007635 C PRODUCT


"QUANTITY" IS NOT NULL
SCOTT SYS_C007636 P PRODUCT

SQL> CREATE TABLE PRODUCT_D


2 AS
3 SELECT *
4 FROM PRODUCT
5 WHERE 1=0;

Table created.

SQL> SET PAGES 100


SQL> SELECT *
2 FROM TAB;

TNAME TABTYPE CLUSTERID


------------------------------ ------- ----------
DEPT TABLE
EMP TABLE
BONUS TABLE
SALGRADE TABLE
STUDENT TABLE
EMPLL TABLE
E TABLE
DUM TABLE
PRODUCT TABLE
CUSTOMER TABLE
ORDERS TABLE
PRODUCT_C TABLE
PRODUCT_D TABLE

13 rows selected.

SQL> DESC PRODUCT_D;


Name

-----------------------------------------------------------------------------------
----------------
PID

PNAME

PRICE

QUANTITY

SQL> SET LINES 100


SQL> DESC PRODUCT_D;
Name Null? Type
----------------------------------------------------- --------
------------------------------------
PID NUMBER(3)
PNAME NOT NULL VARCHAR2(20)
PRICE NOT NULL NUMBER
QUANTITY NOT NULL NUMBER(3)

SQL> SELECT *
2 FROM PRODUCT_D;

no rows selected

SQL> --- DROP THE TABLE


SQL> DROP TABLE PRODUCT_D;

Table dropped.

SQL> SELECT *
2 FROM TAB;

TNAME TABTYPE CLUSTERID


------------------------------ ------- ----------
DEPT TABLE
EMP TABLE
BONUS TABLE
SALGRADE TABLE
STUDENT TABLE
EMPLL TABLE
E TABLE
DUM TABLE
PRODUCT TABLE
CUSTOMER TABLE
ORDERS TABLE
PRODUCT_C TABLE
BIN$9CFUpiKwSgixwVco37xJ8g==$0 TABLE

13 rows selected.

SQL> SHOW RECYCLEBIN;


ORIGINAL NAME RECYCLEBIN NAME OBJECT TYPE DROP TIME
---------------- ------------------------------ ------------ -------------------
PRODUCT_D BIN$9CFUpiKwSgixwVco37xJ8g==$0 TABLE 2024-06-19:15:36:16
SQL> -- RESTORING THE TABLE
SQL> FLASHBACK TABLE PRODUCT_D TO BEFORE DROP RENAME TO P;

Flashback complete.

SQL> SELECT *
2 FROM TAB;

TNAME TABTYPE CLUSTERID


------------------------------ ------- ----------
DEPT TABLE
EMP TABLE
BONUS TABLE
SALGRADE TABLE
STUDENT TABLE
EMPLL TABLE
E TABLE
DUM TABLE
PRODUCT TABLE
CUSTOMER TABLE
ORDERS TABLE
PRODUCT_C TABLE
P TABLE

13 rows selected.
SQL> DROP TABLE P;

Table dropped.

SQL> FLASHBACK TABLE P TO BEFORE DROP;

Flashback complete.

SQL> SELECT *
2 FROM TAB;

TNAME TABTYPE CLUSTERID


------------------------------ ------- ----------
DEPT TABLE
EMP TABLE
BONUS TABLE
SALGRADE TABLE
STUDENT TABLE
EMPLL TABLE
E TABLE
DUM TABLE
PRODUCT TABLE
CUSTOMER TABLE
ORDERS TABLE
PRODUCT_C TABLE
P TABLE

13 rows selected.

SQL> DROP TABLE P;

Table dropped.

SQL> CREATE TABLE P


2 AS
3 SELECT *
4 FROM PRODUCT
5 WHERE 1=9;

Table created.

SQL> SHOW RECYCLEBIN;


ORIGINAL NAME RECYCLEBIN NAME OBJECT TYPE DROP TIME
---------------- ------------------------------ ------------ -------------------
P BIN$waT7bDNcROirJ6/zil+UVg==$0 TABLE 2024-06-19:15:41:41
SQL> FLASHBACK TABLE P TO BEFORE DROP;
FLASHBACK TABLE P TO BEFORE DROP
*
ERROR at line 1:
ORA-38312: original name is used by an existing object

SQL> FLASHBACK TABLE P TO BEFORE DROP RENAME TO P2;

Flashback complete.

SQL> DROP TABLE P;


Table dropped.

SQL> SHOW RECYCLEBIN;


ORIGINAL NAME RECYCLEBIN NAME OBJECT TYPE DROP TIME
---------------- ------------------------------ ------------ -------------------
P BIN$iR2uVRqCTXaGqMy5KCmOeQ==$0 TABLE 2024-06-19:15:45:09
SQL> SELECT *
2 FROM TAB;

TNAME TABTYPE CLUSTERID


------------------------------ ------- ----------
DEPT TABLE
EMP TABLE
BONUS TABLE
SALGRADE TABLE
STUDENT TABLE
EMPLL TABLE
E TABLE
DUM TABLE
PRODUCT TABLE
CUSTOMER TABLE
ORDERS TABLE
PRODUCT_C TABLE
P2 TABLE
BIN$iR2uVRqCTXaGqMy5KCmOeQ==$0 TABLE

14 rows selected.

SQL> PURGE TABLE P;

Table purged.

SQL> SELECT *
2 FROM TAB;

TNAME TABTYPE CLUSTERID


------------------------------ ------- ----------
DEPT TABLE
EMP TABLE
BONUS TABLE
SALGRADE TABLE
STUDENT TABLE
EMPLL TABLE
E TABLE
DUM TABLE
PRODUCT TABLE
CUSTOMER TABLE
ORDERS TABLE
PRODUCT_C TABLE
P2 TABLE

13 rows selected.

SQL> DROP TABLE P2 PURGE;

Table dropped.

SQL> SELECT *
2 FROM TAB;
TNAME TABTYPE CLUSTERID
------------------------------ ------- ----------
DEPT TABLE
EMP TABLE
BONUS TABLE
SALGRADE TABLE
STUDENT TABLE
EMPLL TABLE
E TABLE
DUM TABLE
PRODUCT TABLE
CUSTOMER TABLE
ORDERS TABLE
PRODUCT_C TABLE

12 rows selected.

SQL> -- TRUNCATE
SQL> -- USED REMOVE / DELETE ALL ROWS/ RECORDS PERMANENTLY FROM TABLE
SQL> SELECT *
2 FROM PRODUCT_C;

PID PNAME PRICE QUANTITY


---------- -------------------- ---------- ----------
1 IPHONE11 39999 10
2 IPHONE12 49999 4
3 IPHONE13 59999 6
4 IPHONE14 69999 2
5 IPHONE15 79999 7

SQL> TRUNCATE TABLE PRODUCT_C;

Table truncated.

SQL> SELECT *
2 FROM PRODUCT_C;

no rows selected

SQL> DESC PRODUCT_C;


Name Null? Type
----------------------------------------------------- --------
------------------------------------
PID NUMBER(3)
PNAME NOT NULL VARCHAR2(20)
PRICE NOT NULL NUMBER
QUANTITY NOT NULL NUMBER(3)

SQL> ---RENAME : USED TO CHANGE OF NAME OF EXISTING OBJECT


SQL> -- RENAME TABLE_NAME TO NEW_TABLE_NAME
SQL> RENAME PRODUCT_C TO P;

Table renamed.

SQL> ALTER TABLE P


2 MODIFY PRICE CHAR(5) NULL;

Table altered.
SQL> DESC P;
Name Null? Type
----------------------------------------------------- --------
------------------------------------
PID NUMBER(3)
PNAME NOT NULL VARCHAR2(20)
PRICE CHAR(5)
QUANTITY NOT NULL NUMBER(3)

SQL> ALTER TABLE P


2 MODIFY PRICE NOT NULL;

Table altered.

SQL> DESC P;
Name Null? Type
----------------------------------------------------- --------
------------------------------------
PID NUMBER(3)
PNAME NOT NULL VARCHAR2(20)
PRICE NOT NULL CHAR(5)
QUANTITY NOT NULL NUMBER(3)

SQL> ALTER TABLE P


2 MODIFY PRICE NUMBER;

Table altered.

SQL> DESC P;
Name Null? Type
----------------------------------------------------- --------
------------------------------------
PID NUMBER(3)
PNAME NOT NULL VARCHAR2(20)
PRICE NOT NULL NUMBER
QUANTITY NOT NULL NUMBER(3)

SQL> ALTER TABLE P\


2 \
3
SQL> \
SP2-0042: unknown command "\" - rest of line ignored.
SQL> ALTER TABLE P
2 MODIFY PRICE UNIQUE;

Table altered.

SQL> SET LINES 3000


SQL> SELECT *
2 FROM USER_CONSTRAINTS
3 WHERE TABLE_NAME = 'P';

OWNER CONSTRAINT_NAME C TABLE_NAME


SEARCH_CONDITION
R_OWNER
------------------------------ ------------------------------ -
------------------------------ -----
SCOTT SYS_C007646 C P
"PNAME" IS NOT NULL

SCOTT SYS_C007655 C P
"PRICE" IS NOT NULL

SCOTT SYS_C007648 C P
"QUANTITY" IS NOT NULL

SCOTT SYS_C007656 U P

SQL> ALTER TABLE P


2 DROP UNIQUE(PRICE);

Table altered.

SQL> SELECT *
2 FROM USER_CONSTRAINTS
3 WHERE TABLE_NAME = 'P';

OWNER CONSTRAINT_NAME C TABLE_NAME


SEARCH_CONDITION
R_OWNER
------------------------------ ------------------------------ -
------------------------------ -----
SCOTT SYS_C007646 C P
"PNAME" IS NOT NULL

SCOTT SYS_C007655 C P
"PRICE" IS NOT NULL

SCOTT SYS_C007648 C P
"QUANTITY" IS NOT NULL

SQL> ALTER TABLE P


2 DROP CONSTRAINT SYS_C007655 ;

Table altered.

SQL> SELECT *
2 FROM USER_CONSTRAINTS
3 WHERE TABLE_NAME = 'P';

OWNER CONSTRAINT_NAME C TABLE_NAME


SEARCH_CONDITION
R_OWNER
------------------------------ ------------------------------ -
------------------------------ -----
SCOTT SYS_C007646 C P
"PNAME" IS NOT NULL

SCOTT SYS_C007648 C P
"QUANTITY" IS NOT NULL

SQL> ALTER TABLE P


2 MODIFY PNAME NULL;
Table altered.

SQL> SELECT *
2 FROM USER_CONSTRAINTS
3 WHERE TABLE_NAME = 'P';

OWNER CONSTRAINT_NAME C TABLE_NAME


SEARCH_CONDITION
R_OWNER
------------------------------ ------------------------------ -
------------------------------ -----
SCOTT SYS_C007648 C P
"QUANTITY" IS NOT NULL

SQL> ALTER TABLE P


2 ADD PRIMARY KEY(PID);

Table altered.

SQL> SELECT *
2 FROM USER_CONSTRAINTS
3 WHERE TABLE_NAME = 'P';

OWNER CONSTRAINT_NAME C TABLE_NAME


SEARCH_CONDITION
R_OWNER
------------------------------ ------------------------------ -
------------------------------ -----
SCOTT SYS_C007657 P P

SCOTT SYS_C007648 C P
"QUANTITY" IS NOT NULL

SQL> ALTER TABLE P


2 DROP PRIMARY KEY;

Table altered.

SQL> SELECT *
2 FROM USER_CONSTRAINTS
3 WHERE TABLE_NAME = 'P';

OWNER CONSTRAINT_NAME C TABLE_NAME


SEARCH_CONDITION
R_OWNER
------------------------------ ------------------------------ -
------------------------------ -----
SCOTT SYS_C007648 C P
"QUANTITY" IS NOT NULL

SQL>

COLUMN OWNER FORMAT A30


COLUMN CONSTRAINT_NAME FORMAT A30
COLUMN TABLE_NAME FORMAT A20
COLUMN SEARCH_CONDITION FORMAT A100
COLUMN R_OWNER FORMAT A30
COLUMN R_CONSTRAINT_NAME FORMAT A30
SELECT * FROM USER_CONSTRAINTS WHERE TABLE_NAME='PRODUCT';

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy