插入数据
添加部门数据
示例代码:
-- 添加部门数据
-- 第一种插入数据方法
INSERT INTO department VALUES
(NULL,'行政部','北京'),
(NULL,'市场','上海'),
(NULL,'开发部','广州'),
(NULL,'产品部','广州');
SELECT * FROM department;
-- 添加员工表数据
-- 第二种插入数据方法
INSERT INTO employee (ename,age,dept_id) VALUES ('王一',38,1);
INSERT INTO employee (ename,age,dept_id) VALUES ('黄二',40,2);
INSERT INTO employee (ename,age,dept_id) VALUES ('张三',29,3);
INSERT INTO employee (ename,age,dept_id) VALUES ('李四',22,4);
INSERT INTO employee (ename,age,dept_id) VALUES ('赵五',58,1);
INSERT INTO employee (ename,age,dept_id) VALUES ('李六',33,1);
INSERT INTO employee (ename,age,dept_id) VALUES ('时七',NULL,2);
INSERT INTO employee (ename,age,dept_id) VALUES ('亦八',NULL,3);
INSERT INTO employee (ename,age,dept_id) VALUES ('黄二',40,1);
INSERT INTO employee (ename,age,dept_id) VALUES ('黄二',40,3);
INSERT INTO employee (ename,age,dept_id) VALUES ('黄二',40,4);
SELECT * FROM employee
更改数据
示例代码:
-- 更改数据
-- 将所有dept_id为2的人的年龄改为100
UPDATE employee set age = '100'
-- 将名为李四的人的名字改为罗四
UPDATE employee set ename = '李四' where ename = '罗四';
-- 将名为王一的人的年龄改为100,dept_id为4
UPDATE employee set age = 100 ,dept_id=4 where ename = '王一';
删除数据
示例代码:
-- 删除数据
-- 删除名字为李六的数据
DELETE FROM employee WHERE ename ='李六'
-- 删除表中所有数据
-- 第一种方法delete:逐条删除,效率低
DELETE FROM employee
-- 第二种方法truncate:整表删除,重新创建,效率高
TRUNCATE TABLE employee
-- 外键约束主要事项
-- 需要先删除从表数据
DELETE FROM employee WHERE dept_id =3
DELETE FROM department WHERE id=3