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

Lab 1 DBMS

Uploaded by

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

Lab 1 DBMS

Uploaded by

Akshay Dwivedi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

KIET GROUP OF INSTITUTIONS, GHAZIABAD

Department of Computer Science and Information Technology

Database Management System Lab (KCS-551)


Session 2022-23 (Odd Semester)
Lab Experiments
Experiment Number 1
1 Aim / Write Sql statements for the following statements
Objective /
problem 1. How to get MySQL prompt?
statement 2. How to Create Database?
3. How to use database?
4. How to create table in a database?
5. How to insert 1 record and multiple records in a table?
6. How to show records of a table?
7. How to show a particular record of a table?
8. How to update existing values in a table?
9. How to add new column in a table?

sample SQL query


input
expected Result of SQL query in the form of tables
output
2 Theory CREATE TABLE statement is used to create a table in a database. Tables are organized into
rows and columns; and each table must have a name.
SQL CREATE TABLE Syntax
CREATE TABLE table_name (
column_name1 data_type(size), column_name2 data_type(size), column_name3
data_type(size),.............. );
The column_name parameters specify the names of the columns of the table. The data_type
parameter specifies what type of data the column can hold (e.g. varchar, integer, decimal,
date, etc.). The size parameter specifies the maximum length of the column of the table.

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,...);

Example: Assume we wish to insert a new row in the "Customers" table:


INSERT INTO Customers (CustomerName, ContactName, Address, City, PostalCode,
Country) VALUES ('Cardinal','Tom B. Erichsen','Skagen 21','Stavanger','4006','Norway');
Insert Data Only in Specified Columns:
INSERT INTO Customers (CustomerName, City, Country) VALUES ('Cardinal', 'Stavanger',
'Norway');

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]

 You can update one or more field altogether.


 You can specify any condition using the WHERE clause.
 You can update the values in a single table at a time.
The SQL DELETE Query is used to delete the existing records from a table.
You can use the WHERE clause with a DELETE query to delete the selected rows,
otherwise all the records would be deleted.

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 −

SQL> DELETE FROM CUSTOMERS;

3 Procedure SQL CREATE TABLE Syntax


CREATE TABLE table_name (
column_name1 data_type(size), column_name2 data_type(size), column_name3
data_type(size),.............. );

INSERT INTO table_name VALUES (value1,value2,value3,. );

UPDATE table_name SET field1 = new-value1, field2 = new-value2


[WHERE Clause]

DELETE FROM table_name


WHERE [condition];

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)

You might also like