Sql-Lab 3 DML PDF

Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

3.

DML Queries
Instructor: Bidur Devkota
14th Sept 2020

Data Manipulation Language (DDL)


 includes SQL commands that manipulate the data stored in the datqabase.
 DML works to add / alter / delete the data
 E.g. INSERT, UPDATE and DELETE statements.

1
1. INSERT

Syntax 1:INSERT into [table_name] values (val1, val2, val3, …)

INSERT INTO students VALUES


(1,'Tom','061444333','first','CSIT');

INSERT INTO students VALUES


(NULL,'Pratyush','061444333','first','CSIT');

Syntax 2:INSERT into [table_name] (col1, col2, col3, …) values


(val1, val2, val3, …)
INSERT INTO students (roll,name,phone,sem,department) VALUES
(2,'Mary', '01445566','2nd','CSIT');

Multi column insert:

INSERT INTO students (roll,name,phone,sem,department) VALUES


(2,'Manoj', '01665566','2nd','BE-Software'),(4,'Sandesh',
'01445777','2nd','BE-Civil'),(3,'Bijay', '01445566','third','CSIT');

2. SELECT- retrieve the records.

Syntax:
SELECT [column_name(s)] from [table_name] where [condition]
Example:
select name,roll from students;

select * from students;

select name,roll,sem from students WHERE name = 'Tom' ;


select name,roll,sem from students WHERE name != 'Tom' ;

SELECT sem FROM students;


SELECT DISTINCT sem FROM students;

select * from students WHERE name = "Tom" OR sem!="first";

SELECT * FROM students WHERE roll BETWEEN 2 AND 5;

SELECT * FROM students WHERE roll IN (1,4);

2
SELECT * FROM students WHERE department LIKE 'c%';
SELECT * FROM students LIMIT 2;

SELECT * FROM students WHERE roll IS NULL;

select * from students where sem LIKE 'firs_';

Try the combinations of differnt operators:

3. DELETE

Remove table contents.

Syntax 1:
DELETE from [table_name] where [condition to be specified]

DELETE FROM students WHERE roll=3;

Syntax 2: delete all table

DELETE from [table_name]

DELETE FROM students;

TRUNCATE - to delete all data in a table.

Syntax:

TRUNCATE [TABLE] table_name;

TRUNCATE table students;

4. UPDATE
Syntax:UPDATE [table_name] SET [col1 = val1, col2 = val2,...]
where [condition to be specified]
UPDATE students SET name = 'Ravi' where roll =1;

UPDATE students SET name = 'Binod', sem = 'IV' where roll = 3;

3
Task:

1. Complete the above examples.


Instead of the table name student create new table with name `your-
name_roll`.
For e.g. if your name is Ram and your roll number is 100 then you have to create
table named `ram-100`.

Submission:
1. Title: 3. DML Queries.
2. Theory: Explain about the related queries( syntax, Descriptions)
3. Observations: Queries you run and their screenshot.
4. Conclusion

Note:
1. Submit a file with name <3_DML_Queries_ROLL_NAME.pdf>
For example student ‘Ram’ with roll number ‘100’ should send a file named:
3_DML_Queries_100_Ram.pdf
2. Copying will be marked ZERO.
3. Send as email attachment to [email protected]
4. Email Subject: 3. DML Queries

You might also like