0% found this document useful (0 votes)
36 views3 pages

DDL Commands and Constraints in SQL

The document outlines a lab exercise focused on executing DDL commands and constraints in a database. Participants will create and manipulate tables, including adding and modifying columns, inserting data, and understanding the differences between TRUNCATE and DROP commands. Additionally, it details permissible and impermissible changes to columns in a database table.

Uploaded by

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

DDL Commands and Constraints in SQL

The document outlines a lab exercise focused on executing DDL commands and constraints in a database. Participants will create and manipulate tables, including adding and modifying columns, inserting data, and understanding the differences between TRUNCATE and DROP commands. Additionally, it details permissible and impermissible changes to columns in a database table.

Uploaded by

Kartik Mittal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Lab 2 – Execution of DDL Commands and Constraints

Objective: At the end of the assignments, participants will be able to understand basic DDL,
Create table with constraints, Alter, Truncate, Drop and Rename

1. Complete the GRADUATE CANDIDATE table instance chart. Credits is a foreign-key column referencing
the requirements table.

2. Write the syntax to create the grad_candidates table.

 Create Table GRADUATE (


Id number(6) ,
Lname varchar(20) ,
Fname varchar(30),
credits number(3) ,
graduation_Date DATE
);

3. Confirm creation of the table.

 describe graduate

4. Create a new table using grad_candidates with the following syntax: CREATE TABLE
o_grad_candidates AS (SELECT * FROM grad_candidates);

 Create Table O_GRADUATE AS (SELECT * FROM GRADUATE);

5. Create a new table using a subquery. Name the new table your first name -- e.g., akshat_table. Using
a subquery, copy grad_candidates into akshat_table.

=> Create Table akshat_table AS( SELECT *FROM GRADUATE);


6. In your o_grad_candidates table, enter a new column called “adm_date.” The datatype for the
new column should be VARCHAR2. Set the DEFAULT for this column as SYSDATE.

=> ALTER TABLE O_GRADUATE


ADD adm_date varchar2 default SYSDATE;

7. In your o_grad_candidates table, increase the length of last_name column by 10 and remove the
credits column.
=> alter table O_GRADUATE
modify Lname varchar(30);
alter table O_GRADUATE
drop column credits;

8. Create a new column in the akshat_table table called start_date. Use the TIMESTAMP WITH LOCAL
TIME ZONE as the datatype.
=> alter table akshat_table
ADD start_date Timestamp with local time zone;

9. Write syntax to change the name of credit column by grad_credit.


=>alter table akshat_table
Rename column credits to grad_credit;

10. Insert 5 tuples in akshat_table.

=> INSERT INTO AKSHAT_TABLE (STUDENT_ID, LAST_NAME, FIRST_NAME, GRAD_CREDIT, GRADUATION_DATE, START_DATE)
VALUES (101005, 'Davis', 'James', 110, TO_DATE('2025-07-30', 'YYYY-MM-DD'), TO_TIMESTAMP('2021-01-15
[Link]', 'YYYY-MM-DD HH24:MI:SS'));

=> INSERT INTO AKSHAT_TABLE (STUDENT_ID, LAST_NAME, FIRST_NAME, GRAD_CREDIT, GRADUATION_DATE, START_DATE)
VALUES (101004, 'Brown', 'Jessica', 122, TO_DATE('2023-12-20', 'YYYY-MM-DD'), TO_TIMESTAMP('2019-09-12
[Link]', 'YYYY-MM-DD HH24:MI:SS'));

=> INSERT INTO AKSHAT_TABLE (STUDENT_ID, LAST_NAME, FIRST_NAME, GRAD_CREDIT, GRADUATION_DATE, START_DATE)
VALUES (101002, 'Johnson', 'Emily', 115, TO_DATE('2024-05-15', 'YYYY-MM-DD'), TO_TIMESTAMP('2020-09-01
[Link]', 'YYYY-MM-DD HH24:MI:SS'));

=> INSERT INTO AKSHAT_TABLE (STUDENT_ID, LAST_NAME, FIRST_NAME, GRAD_CREDIT, GRADUATION_DATE, START_DATE)
VALUES (101001, 'Smith', 'John', 120, TO_DATE('2024-05-15', 'YYYY-MM-DD'), TO_TIMESTAMP('2020-08-23
[Link]', 'YYYY-MM-DD HH24:MI:SS'));

=> INSERT INTO AKSHAT_TABLE (STUDENT_ID, LAST_NAME, FIRST_NAME, GRAD_CREDIT, GRADUATION_DATE, START_DATE)
VALUES (101003, 'Williams', 'Michael', 118, TO_DATE('2024-06-10', 'YYYY-MM-DD'), TO_TIMESTAMP('2020-08-20
[Link]', 'YYYY-MM-DD HH24:MI:SS'));

11. Truncate the akshat_table table. Then do a SELECT * statement. Are the columns still there?

=> TRUNCATE TABLE AKSHAT_TABLE;


SELECT * FROM AKSHAT_TABLE
=>YES , column are still there but data is not present in it
12. What the distinction is between TRUNCATE and DROP for tables?

13. List the changes that can and cannot be made to a column.

=> Changes That CAN Be Made:

Modify Data Type (e.g., increase size or precision).


Modify Default Value or add/remove a default value.
Add a NOT NULL Constraint, if there are no NULL values.
Rename a Column.
Modify or Add Constraints (e.g., UNIQUE, PRIMARY KEY, CHECK).

=>Changes That CANNOT Be Made:

Decrease the Size of a column if data would be truncated.


Change to Incompatible Data Types (e.g., VARCHAR to NUMBER if non-numeric data exists).
Add a NOT NULL Constraint to a column with existing NULL values.
Make a Column a Primary Key if it contains duplicate values.
Remove a Column if it's part of a constraint or index, without first dropping those constraints

14. Rename o_grad_candidates to n_grad_candidates.

=> RENAME o_grad_candidates TO n_grad_candidates;

You might also like