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];