Lab 1 DBMS
Lab 1 DBMS
Example: Now we want to create a table called "Persons" that contains five columns:
PersonID, LastName, FirstName, Address, and City.
CREATE TABLE Persons
(
PersonID int, LastName varchar(255), FirstName varchar(255), Address varchar(255), City
varchar(255)
);
INSERT INTO statement is used to insert new records in a table. It is possible to write the
INSERT INTO statement in two forms.
a) The first form does not specify the column names where the data will be inserted, only
their values:
INSERT INTO table_name VALUES (value1,value2,value3,...);
b) The second form specifies both the column names and the values to be inserted:
INSERT INTO table_name (column1,column2,column3,...) VALUES
(value1,value2,value3,...);
SELECT command to retrieve one or more rows, or partial rows, of data from an existing
table or view, and to perform grouping functions on the data.
Syntax:
a) To view all row and all column columns:
Select * from table_name;
b) To view all row and selected column columns;
Select column_name1,column_name2 from table_name;
c) To view selected row and all column columns;
Select * from table_name where condition;
d) To view selected row and all column columns;
Select column_name1,column_name2 from table_name where condition;
Examples: To select all rows of the alerts.status table where the Severity is equal to 4, enter:
select * from alerts.status where Severity = 4;
There may be a requirement where the existing data in a MySQL table needs to be modified.
You can do so by using the SQL UPDATE command. This will modify any field value of
any MySQL table.
Syntax
The following code block has a generic SQL syntax of the UPDATE command to modify the
data in the MySQL table −
UPDATE table_name SET field1 = new-value1, field2 = new-value2
[WHERE Clause]
Syntax
The basic syntax of the DELETE query with the WHERE clause is as follows −
DELETE FROM table_name
WHERE [condition];
You can combine N number of conditions using AND or OR operators.
Example
Consider the CUSTOMERS table having the following records −
+ + + + + +
| ID | NAME | AGE | ADDRESS | SALARY |
+ + + + + +
| 1 | Ramesh | 32 | Ahmedabad | 2000.00 |
| 2 | Khilan | 25 | Delhi | 1500.00 |
| 3 | kaushik | 23 | Kota | 2000.00 |
| 4 | Chaitali | 25 | Mumbai | 6500.00 |
| 5 | Hardik | 27 | Bhopal | 8500.00 |
| 6 | Komal | 22 | MP | 4500.00 |
| 7 | Muffy | 24 | Indore | 10000.00 |
+ + + + + +
The following code has a query, which will DELETE a customer, whose ID is 6.
SQL> DELETE FROM CUSTOMERS
WHERE ID = 6;
Now, the CUSTOMERS table would have the following records.
+ + + + + +
| ID | NAME | AGE | ADDRESS | SALARY |
+ + + + + +
| 1 | Ramesh | 32 | Ahmedabad | 2000.00 |
| 2 | Khilan | 25 | Delhi | 1500.00 |
| 3 | kaushik | 23 | Kota | 2000.00 |
| 4 | Chaitali | 25 | Mumbai | 6500.00 |
| 5 | Hardik | 27 | Bhopal | 8500.00 |
| 7 | Muffy | 24 | Indore | 10000.00 |
+ + + + + +
If you want to DELETE all the records from the CUSTOMERS table, you do not need to use
the WHERE clause and the DELETE query would be as follows −
4 Viva 1. What are the different DDL commands in SQL? Give a description of their purpose.
questions 2. What are the different DML commands in SQL?
3.When would you use view in SQL?
5 External https://www.youtube.com/watch?v=Tet3Z7Yb2gg
Link (if any)