0% found this document useful (0 votes)
6 views4 pages

SQL Lab

The document provides a comprehensive guide on SQL commands for creating and managing tables, including syntax for creating tables, inserting records, updating columns, retrieving records, and altering table structures. It includes examples for each command, such as creating an 'employee' table, inserting a record, updating a column, and deleting rows. Additionally, it explains how to modify table structures and manage columns effectively.

Uploaded by

avanthidocs
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)
6 views4 pages

SQL Lab

The document provides a comprehensive guide on SQL commands for creating and managing tables, including syntax for creating tables, inserting records, updating columns, retrieving records, and altering table structures. It includes examples for each command, such as creating an 'employee' table, inserting a record, updating a column, and deleting rows. Additionally, it explains how to modify table structures and manage columns effectively.

Uploaded by

avanthidocs
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

SQL Lab

Creating a Table
A table can be created using a CREATE command
Syntax:
CREATE TABLE TABLE_NAME (COLUMN_NAMES DATATYPES [ ...]);
Example:
create table employee(name varchar(20), email varchar(100), eid number(5));
In the above example, employee is called table name
Name, email and dob are column names
Varchar, eid are datatypes

Inserting or creating a record in the table


A record can be created by using the INSERT command.
Syntax:
INSERT INTO TABLE_NAME (col1, col2,,....) VALUES (value1, value2,....);
(or)
INSERT INTO TABLE_NAME VALUES (value1, value2, value3, .... );
Example:
Insert into employee values(‘rakesh’,’rk245@[Link]’,12);
Note: In the above example, characters must be enclosed in single quotations

Updating a column
A column can be updated by using UPDATE command.

Syntax:
UPDATE table_name SET [column_name1= value1,...column_nameN = valueN] [
WHERE CONDITION]
Example: update supplier set iprice=iprice+200 where isup=’keyboard’;
Note: In the above example, keyboard price is increased by 200 in supplier table

Retrieving the records from the table


The records from the table can be retrieved using SELECT command
Syntax
SELECT expressions
FROM TABLE NAME
WHERE conditions;

Example:
Select sname from supplier where city=’delhi’;
Note 1: In the above example, all the supplier names who belong to delhi is
displayed.
Note 2: To display all the columns of the table ‘*’ is used
Select * from supplier;
Note 3: To display the columns in ascending or descending order
Select sid,sname,iprice from supplier where city=’delhi’ order by iprice
asc/desc;
Changing the structure of table
The structure of the table can be changed using ALTER command
Adding new columns in Table

Syntax:
ALTER TABLE table_name ADD column_name column-definition;

Example:
Alter table supplier add contact number(10);

To ADD a multiple column from a table


ALTER TABLE table_name ADD column_name1, column_name2;

Modifying Column using ALTER


Syntax:
ALTER TABLE table_name MODIFY (column definitions....);

Example:
Alter table supplier modify contact number (12);
Drop column using ALTER
Syntax:
ALTER TABLE table_name DROP COLUMN column_name;
Example
Alter table supplier drop contact
Delete a row
A row can be deleted using DELETE Command
Syntax:
DELETE FROM table_name [WHERE condition];

You might also like