DB-2 Update & Delete Records

You might also like

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 21

DATABASE PROGRAMMING

Dr.Amgad Monir
Contents
• UPDATE
• DELETE
• BASIC QUERIES
Create table with contents
CREATE TABLE student (
student_id INT PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(40) NOT NULL,
major VARCHAR(40) DEFAULT 'undecided'
);

INSERT into student(name) values('jack');


insert into student(name,major) values("mahmoud","programmer");
insert into student values(3,"Ahmed","Engineer");
insert into student values(4,"Jack","Biology");
Update
Update contents from Biology to BIO

UPDATE student
SET major = 'Bio'
WHERE major = 'Biology';

select * from student;


Update
For all records -> Set major to engineer

UPDATE student
SET major = 'Engineer'

select * from student;


Update
For student_id=1 ----> Set major to Systm Analyst

UPDATE student
SET major = 'System Analyst'
WHERE student_id = 1;

select * from student;


Update
Major = ‘Engineer’ OR Major= ‘System Analyst’ --> Major=‘Practical Science’

UPDATE student
SET major = 'Practical Science'
WHERE major="Engineer" OR major="System Analyst";

select * from student;


Update Multi Values
Student_id=4 --> name=‘Khaled’ & major=‘Programmer’

UPDATE student
SET name="khaled", major="programmer"
WHERE student_id=4;

select * from student;


DELETE
DELETE the record with student_id=2

DELETE FROM student


WHERE student_id=2;

select * from student;


DELETE
DELETE the record with major=Practical Science

DELETE FROM student


WHERE major='Practical Science';

select * from student;


DELETE
DELETE Everything from Student

DELETE FROM student;

select * from student;


Create table with contents
CREATE TABLE student (
student_id INT PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(40) NOT NULL,
major VARCHAR(40) DEFAULT 'undecided'
);

INSERT into student(name) values('jack');


insert into student(name,major) values("mahmoud","programmer");
insert into student values(3,"Ahmed","Engineer");
insert into student values(4,"Jack","Biology");
Basic Queries
SELECT name of all Students

select name select name, major


from student; from student;
ORDER BY

select name, major select name, major select *


from student from student from student
ORDER BY major; ORDER BY major DESC; ORDER BY student_id DESC;
ORDER BY

select *
from student
ORDER BY major,name;
LIMIT

select *
select * from student
from student ORDER BY student_id DESC
LIMIT 2; LIMIT 2;
Where

select name,major
select * select name,major
from student
from student from student
where major="programmer" OR
where major="programmer"; where major="programmer";
major="Biology";
Where

SELECT All non programmers SELECT All student_id < 3


select * select *
from student from student
where major <> "programmer"; where student_id <3;
Where
SELECT All student_id < 3 who are not jack

select *
from student
where student_id <3 and name <> "jack";
Where

select *
from student
where name in ("Jack","Ahmed") ;
Where

select *
from student
where name in("Jack","ahmed") and student_id > 3;

You might also like