当前位置:网站首页>MySQL-DML

MySQL-DML

2022-06-21 11:47:00 Jue Niu thunder plough hot blade

/* DML It refers to the data operation language , The full English name is Data Manipulation Language, Used to update the data records of tables in the database .  keyword : 1. Insert insert 2. Delete delete 3. to update update */

#  Insert the way 1
insert into student(sid, name, gender, age, birthday, address, score)
values (1008, ' Little plum ', 'male', 18, '2004-12-9', ' New York ', 74),
       (1009, ' Fruit sister ', 'female', 22, '2000-12-9', ' Washington ', 89);

#  Insert data into the specified column 
insert into student(sid)
values (1022);

insert into student(sid, name)
values (1024, ' pomegranate ');

#  Insert the way 2
#  Insert all columns 
insert into student
values (1078, ' Kobe ', 'male', 37, '1975-12-9', ' Los Angeles ', 85),
       (1099, ' harden ', 'male', 22, '2000-12-9', ' Houston ', 78);

#  modify ( to update )
# 1. Change the address of all students to Philadelphia 
update student
set address=' Philadelphia ';

# 2. take sid by 1008 The address of the student of is changed to Kashgar 
update student
set address=' Kashgar '
where sid = 1008;

# 2. take sid Greater than 1008 The student's address is changed to Russia 
update student
set address=' Russia '
where sid > 1008;

# 3. take sid by 1009 The address of the students is changed to Heilongjiang , The grade is revised to 99
update student
set address=' heilongjiang ',
    score=99
where sid = 1009;

#  Delete data 
# 1. Delete sid by 1008 Student data for 
delete
from student
where sid = 1008;
# 2. Clear table data 
delete
from student;

# truncate table student;
# table Omission 
truncate student;
#  Be careful :delete and truncate The principle is different ,delete Only delete content ,
#  and truncate Be similar to drop table, It can be understood as deleting the whole table , Then create the table ;


#  practice 
/* 1. Create an employee table emp, The fields are as follows : id( staff id), name( Employee name ), gender( Gender of employees ), salary( Employee pay ), */
create table if not exists emp
(
    id     int comment ' staff id',
    name   varchar(20) comment ' Employee name ',
    gender varchar(10) comment ' Gender of employees ',
    salary double comment ' Employee pay '
);

/* 2. insert data  1,' Zhang San ',' male ',2000 2,' Li Si ',' male ',3000 3,' Wang Xiaowu ',' Woman ',2000 */
insert into emp
values (1, ' Zhang San ', ' male ', 2000),
       (2, ' Li Si ', ' male ', 3000),
       (3, ' Wang Xiaowu ', ' Woman ', 2000);

#  Modify table data 
# 1. Change the salary of all employees to 5000
update emp
set salary=5000;

# 2. Name is ‘ Zhang San ’ The salary of the employee is revised to 3000
update emp
set salary=3000
where name = ' Zhang San ';

# 3. take id by 3 The salary of the employee is revised to 6000,gender It is amended as follows ‘ male ’
update emp
set salary=6000,
    gender=' male '
where id = 3;

# 4. take id by 3 The employee's salary is increased on the original basis 2000
update emp
set salary=salary + 2000
where id = 3;
原网站

版权声明
本文为[Jue Niu thunder plough hot blade]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/172/202206211146381987.html