数据库实验二

数据库实验二 表数据的插入,修改和删除

目的与要求

(1)学会使用SQL语句对数据表进行插入,删除和修改数据;
(2)了解更新操作时要注意数据完整性;
(3)了解SQL语句对数据操作的灵活控制功能。

实验内容

1,实验题目

数据库实验二

实验步骤

一,插入

(1)使用SQL语句插入数据,向表employees插入数据,启动MySQL客户端,在命令行下输入:
insert into employees values(‘000001’,‘王林’,‘大专’,‘1986-01-23’,‘1’,8,‘中山路32-1-508’,‘83355668’,‘2’);
insert into employees values(‘010008’,‘伍容华’,‘本科’,‘1988-03-28’,‘1’,3,‘北京东路100-2’,‘83321321’,‘1’);
以上两行数据并没有明确要求列出列名,所以各列值及类型要和表中的列一一对应。
(2)
insert into employees(employeeid,name,education,birthday,sex,workyear,address,phonenumber,departmentid)values(‘020018’,‘王向荣’,‘硕士’,‘1982-12-09’,‘1’,2,‘四牌楼10-0-108’,‘83792361’,‘1’);
(上面直接粘贴没问题的,只是不知道为什么排版变成这样)
以上命令在插入数据的过程中明确了列名和值的对应关系。
(3)也可以同时插入多行数据,如下面所示,多行值之间以逗号分割。
insert into employees(employeeid,name,education,birthday,sex,workyear,address,phonenumber,departmentid)values(‘102201’,‘刘明’,‘本科’,‘1978-07-30’,‘1’,3,‘虎距路100-2’,‘83606608’,‘5’),
(‘020010’,‘李丽’,‘大专’,‘1972-10-18’,‘1’,3,‘中山东路102-2’,‘83413301’,‘1’);

二,删除

delete from employees where Employeeid=‘000001’;

三,修改

insert into employees values(‘000002’,‘容华’,‘本科’,‘1988-03-28’,‘0’,3,‘北京东路100-2’,‘83321321’,‘1’);
update employees set address=‘北京西路100-2’,phonenumber=‘83321320’,departmentid=‘2’ where employeeid=‘000002’;

四,替换已经存在的行

insert into Departments values(‘1’,‘广告部’,‘负责推广产品’);
replace into Departments values(‘1’,‘广告2部’,‘负责推广产品’);
总结:向表中“替换插入”一条数据,如果原表中没有id=1这条数据就作为新数据插入(相当于insert into);如果原表中有id=1这条数据就做替换(相当于update),对于没有指定的字段以默认值插入。

五,删除表中所有的行(谨慎操作)

truncate table salary;

注意:

运行代码时如果出现乱码,试用:
show variables like ‘char%’;
或者
show variables like ‘collation_%’;
来查看MySQL的数据使用编码。
数据库实验二

补充(实验一):

如何查看键入信息:

1)show databases;

数据库实验二

2)select database();

数据库实验二

3)show tables;

数据库实验二

4)show global variables like ‘port’;

数据库实验二

5)select * from employees;

数据库实验二