Repository navigation
Expand file tree
/
Copy pathDML.sql
More file actions
101 lines (60 loc) · 2.35 KB
/
Copy pathDML.sql
File metadata and controls
101 lines (60 loc) · 2.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
Create database mysql_class;
use mysql_class;
create table student_table(
student_id INT,
student_name VARCHAR(100),
course_name VARCHAR(50)
);
alter table student_table add email varchar(100);
show tables;
describe student_table;
-- insert data
INSERT INTO student_table(student_id,student_name,course_name,email)
VALUES(1, 'John',"SQL", 'John11@gmail.com');
-- How to insert data into only few columns:
INSERT INTO student_table(student_id,student_name,email)
VALUES(1, 'John','John11@gmail.com');
-- How to insert multiple values in one Go:
INSERT INTO student_table(student_id,student_name,course_name,email)
VALUES(2, 'Jane',"SQL", 'Jane22@gmail.com'),
(3, 'Michael',"SQL", 'Michael33@gmail.com');
-- DQl(Select)
select student_id from student_table;
select student_id,student_name from student_table;
select student_id,student_name,course_name,email from student_table;
select * from student_table;
-- update
UPDATE student_table SET student_id = student_id * 2 WHERE email ='Jane22@gmail.com';
-- if using safe mode
SET SQL_SAFE_UPDATES = 0;
select * from student_table;
-- delete
DELETE FROM student_table WHERE student_id =4;
select * from student_table;
DELETE FROM student_table;
truncate table student_table;
-- Datatype Mismatch:
INSERT INTO student_table(student_id,student_name,course_name,email)
VALUES('a', 'Shailja',"SQL",'Shailja11@gmail.com');
-- DML USING CONTRAINTS
-- Creating Table without NULL:
CREATE TABLE employee (
firstname varchar(20) NOT NULL,
middlename varchar(20),
lastname varchar(20) NOT NULL,
age INT NOT NULL,
salary INT NOT NULL,
location varchar(20)
);
describe employee;
INSERT INTO employee(middlename, lastname, age, salary, location) VALUES ('Kumar','Sinha',29,10000,'bangalore');
-- Default Values:
alter table employee modify column location varchar(20) DEFAULT 'BANGLORE';
INSERT INTO employee(firstname, lastname, age, salary, location) VALUES ('satish','kumar',29,10000,'hyderabad');
select * from employee;
INSERT INTO employee(firstname, lastname, age, salary) VALUES ('rajesh','kumar',29,10000);
select * from employee;
-- Combination of NOT NOT and DEFAULT
alter table employee modify column location varchar(20) NOT NULL DEFAULT 'BANGLORE';
INSERT INTO employee(firstname, lastname, age, salary) VALUES ('Shailja','Mishra',10,10000);
-- https://www.youtube.com/watch?v=rB382wSHuTg&ab_channel=GreatLearning