Ex - 1 and 2
Ex - 1 and 2
DATE:
PROCEDURE:
Data Definition Commands:
DDL consists of SQL commands used to define and manage the structure of a database. It deals
with the descriptions of the database schema and is used to create, modify, and delete database
objects such as tables, indexes, and views.
i) CREATE TABLE
The CREATE TABLE command is used to create a new table in the database.
Rules:
1. Reserved Words: Oracle reserved keywords cannot be used as table names or column
names.
2. Naming Restrictions: Table names can contain underscores, numerals, and letters, but
cannot contain spaces.
3. Maximum Length: The maximum length for a table name is 30 characters.
4. Unique Table Names: Each table in a database must have a unique name.
5. Column Names: Each column in the table must have a unique name.
6. Data Types: Proper data types must be specified for each column, along with the appropriate
size or precision (where applicable).
Explanation: The CREATE TABLE command is used to create a new table in the database with
specified column names, data types, and optional constraints.
Explanation: The DESC (short for "DESCRIBE") command displays the structure of a table,
including column names, data types, and whether the column can accept NULL values.
You can create a new table based on the structure and data of an existing table using the
following syntax:
Explanation: The CREATE TABLE ... AS SELECT command creates a new table by selecting
specific columns from an existing table based on a condition (optional). This can be used to copy
data and structure from the existing table.
Syntax:
SQL>Create table tablename (column_name1 data_ type constraints, column_name2
data_ type constraints …);
2. DROP TABLE
The DROP TABLE command is used to delete a table and all of its data from the database
permanently. Once a table is dropped, it cannot be recovered unless there is a backup.
Syntax:
SQL> DROP TABLE table_name;
3. ALTER COMMAND
The ALTER TABLE command is used to modify the structure of an existing table. It can
perform the following actions:
i) ADD COMMAND
Syntax:
SQL> ALTER TABLE table_name ADD column_name data_type(size);
Explanation: The ADD command adds a new column to the table with the specified data type
and size.
To modify an existing column’s definition (such as its data type or size), use the MODIFY
command.
Syntax:
SQL> ALTER TABLE table_name MODIFY column_name data_type(size);
Explanation: The MODIFY command changes the definition of an existing column, allowing
you to update its data type, size, or other attributes.
4. TRUNCATE TABLE
The TRUNCATE TABLE command removes all rows from a table but retains the table structure
for future use. It is faster than DELETE, and unlike DELETE, it cannot be rolled back.
Syntax:
SQL> TRUNCATE TABLE table_name;
Explanation: The TRUNCATE command deletes all rows from the table while keeping the
table structure intact. It frees up the space used by the data.
5. COMMENT
1. Single-Line Comments: Comments that start and end on the same line.
2. Multi-Line Comments: Comments that span multiple lines.
3. Inline Comments: Comments placed within a SQL statement.
Single-Line Comment
A comment that starts with -- and extends to the end of the line.
Syntax:
-- This is a single-line comment
-- Another comment
Multi-Line Comment
A comment that spans multiple lines, starting with /* and ending with */.
Syntax:
/* This is a multi-line comment
spanning multiple lines */
Inline Comment
An extension of the multi-line comment that can be placed within a SQL statement.
Syntax:
SQL> SELECT * FROM /* comment here */ table_name;
6. RENAME
The RENAME command is used to change the name of an existing database object, such as a
table. It allows database users to give more relevant names to tables or other objects.
Syntax:
SQL> RENAME old_table_name TO new_table_name;
Explanation: The RENAME command changes the name of a table or other database object.
Program:
SQL> connect
Enter user-name: system
Enter password: admin
Connected.
SQL> CREATE TABLE emp (id NUMBER(10), name VARCHAR(10));
Table created.
Table dropped.
ID NAME DEPT
---------- ---------- ----------
1 aaa cse
2 aaa cse
3 aaa ece
4 aaa cse
5 aaa cse
no rows selected
Result:
Thus, the SQL Data Definition Language (DDL) commands have been successfully
executed to create, modify, and manage the database objects.
EX.NO:2
DATE:
AIM:
To create a database and perform operations using Data Manipulation Commands (DML) for
inserting, deleting, updating, and retrieving data, along with using Transaction Control
statements.
DESCRIPTION:
DML statements access and manipulate data in existing tables. DML commands are the
most frequently used SQL commands and is used to query and manipulate the existing
database objects. Some of the commands are Insert, Select, Update, Delete.
Examples of DML:
1) Insert Command: This is used to add one or more rows to a table. The values are
separated by commas and the data types char and date are enclosed in apostrophes. The
values must be entered in the same order as they are defined.
4) Delete command: After inserting row in a table we can also delete them if required.
The delete command consists of a from clause followed by an optional where clause.
Create table:
INSERT COMMAND
Example: SQL> INSERT INTO persons (pid, firstname, lastname, address, city)
VALUES (1, 'Nelson', 'Raj', 'No25, Annai Street', 'Chennai');
1 row created.
Insert more than a record into persons table using a single insert command.
SELECT COMMAND
It is used to retrieve information from the table. It is generally referred to as querying the
table. We can either display all columns in a table or only specify column from the table.
Syntax: SQL> Select * from tablename; // This query selects all rows from the table.
To select specific rows from a table we include ‘where’ clause in the select command. It
can appear only after the ‘from’ clause.
Example: SQL> SELECT firstname, lastname FROM persons WHERE pid > 2;
Example: SQL> INSERT INTO persons1 (SELECT pid, firstname, lastname FROM persons
WHERE city = 'Chennai');
Example: SQL>Select * from persons where pid between 100 and 500;
Table updated.
DELETE COMMAND
1 row deleted.
RESULT:
Thus, the database has been created, and the data has been inserted, deleted, modified, updated,
and altered. Records have also been viewed based on specific conditions.