MySQL数据表的CURD

发布时间 2023-12-07 23:55:24作者: TechNomad

一、数据表的CURD

1.create数据

创建一个员工表,新建employee表并向表中添加一些记录:

创建数据表:

create table employee(id int, name varchar(20), sex int, birthday date, salary double, entry_date date, resume text);

向数据表中插入数据:

insert into employee values(1,'张三',1,'1983-04-27',15000,'2012-06-24','一个大牛');
insert into employee(id,name,sex,birthday,salary,entry_date,resume) values(2,'李四',1,'1984-02-22',10000,'2012-07-24','一个中牛');
insert into employee(id,name,sex,birthday,salary,entry_date,resume) values(3,'王五',0,'1985-08-28',7000,'2012-08-24','一个小虾');

结果如下:

2.update数据

将所有员工薪水都增加500元:

update employee set salary=salary+500;

将王五的员工薪水修改为10000元,resume改为也是一个中牛:

update employee set salary=10000, resume='也是一个中牛' where name='王五';

结果如下:

3.Retrieve数据

查询员工的年收入:

select id, name as "名字", salary "月薪", salary*12 年薪  from employee where id <=1;

4.delete数据

删除表中姓名为王五的记录:

delete from employee where name='王五';	//注意from不能省略

删除表中所有记录:

delete from employee;
truncate table employee; //无条件 效率高

二、综合示例

创建一个学生表:

create table student(id int, name varchar(20), chinese int, english int, math int);

 向数据表中插入数据:

insert into student(id,name,chinese,english,math) values(1, '范建',80,85,90);
insert into student(id,name,chinese,english,math) values(2,'罗况',90,95,95);
insert into student(id,name,chinese,english,math) values(3,'杜子腾',80,96,96);
insert into student(id,name,chinese,english,math) values(4,'范冰',81,97,85);
insert into student(id,name,chinese,english,math) values(5,'申晶冰',85,84,90);
insert into student(id,name,chinese,english,math) values(6,'郝丽海',92,85,87);
insert into student(id,name,chinese,english,math) values(7,'郭迪辉',75,81,80);
insert into student(id,name,chinese,english,math) values(8,'拎壶冲',77,80,79);
insert into student(id,name,chinese,english,math) values(9,'任我行',95,85,85);
insert into student(id,name,chinese,english,math) values(10,'史泰香',94,85,84);

执行结果如下: