SQL

Saturday, 9 January 2016

SQL 1: Create Table, Insert row and Drop Table

First I will create 3 tables and some exercise will follow these 3 tables. I believe the best way to learn is by practising. Many people know very well theoretically but when it comes for real problems they fail. I will create


  • S for Suppliers Table 
  • P for Parts Table
  • SP fro Shipments Table

Create Table S:

SNO                  SNAME              STATUS               CITY
---                       ----------               ----------             -----------    
S1                      SMITH                    20                    LONDON
S2                      JONES                    10                    PARIS
S3                      BLAKE                   30                    PARIS
S4                      CLARK                   20                    LONDON
S5                      ADAMS                  30                    ATHENS






Now we have a table S in the Database but it will not look like above until we put row data on it.So we need to insert the values as well.

Right now so far we have all the data in the table S. To see all the data on S, we have to do a select command:


Create Table P:

PNO      PNAME      COLOR       WEIGHT CITY
---          ----------          -----           ----------   ----------
P1            NUT            RED           12      LONDON
P2            BOLT          GREEN   17      PARIS
P3            SCREW       BLUE           17      ROME
P4            SCREW       RED           14      LONDON
P5            CAN            BLUE           12      PARIS
P6            COG            RED           19      LONDON









Now to Inser data on P we have to use same command as we did previous table:



Create Table J:


JNO        JNAME       CITY
---------- ----------       ----------
J1         SORTER       PARIS
J2         DISPLAY     ROME
J3         OCR             ATHENS
J4         CONSOLE   ATHENS
J5         RAID            LONDON
J6         EDS              OSLO
J7         TAPE            LONDON

7 rows selected.







Create Table SP:



SNo      PNo     Qty
S1         P1        200
S2         P3        400
S2         P5        100
S3         P3        200
S3         P4        500
S4         P6        300
S5         P2        200
S5         P5        500
S5         P6        200
S5         P1        100
S5         P3        200
S5         P4        800


The following command will generate a table with empty row :


The table is empty now So to insert row or data we have to use Insert statement.


Above query will create one by one row in the SP table each time. But If we like to put several row together, we may follow this procedure:


Note: If you are using SQLPlus the above statement will not work.But the following will work:


SNo     PNo   JNo   Qty



Create Table SPJ:
SNO        PNO        JNO               QTY
---------- ---------- ----------           ----------
S1             P1            J1                200
S1             P1            J4                700
S2             P3            J1                400
S2             P3            J2                200
S2             P3            J3                200
S2             P3            J4                500
S2             P3            J5                600
S2             P3            J6                400
S2             P3            J7                800
S2             P5            J2                100
S3             P3            J1                200
S3             P4            J2                500
S4             P6            J3                300
S4             P6            J7                300
S5             P2            J2                200
S5             P2            J4                100
S5             P5            J5                500
S5             P5            J7                100
S5             P6            J2                200
S5             P1            J4                100
S5             P3            J4                200
S5             P4            J4                800
S5             P5            J4                400
S5             P6            J4                500





Insert record to SPJ:


To Drop a Table you have to use simply drop statement. DBMS will not give you a warning sign whether the table empty or full but it will simply drop it so be cautious to drop a table.



Think We have a database which has some table on it. The following command will show all table as a list.