外键约束是指在外键关联主键上强制加上一个约束,如果违反该约束,则不允许该条数据的修改。
select * from dog id in(3,5);查询包括3和5的
模糊查询:
--查询所有包含‘电’字的记录。
select * from product where where productName Like'%电%'
"-"通配符表示任何单个字符
"%"通配符表示包含零个或多个任意字符。
--查询厂家为null的商品
select * from product where factory is null
--按商品单价排序显示
select * from product order by price;--升序
select * from product order by price desc;--降序
select * from product order by price desc,id desc;
表的创建插入代码、事务回滚:
CREATE TABLE Employee(
EmployeeID INT PRIMARY KEY AUTO_INCREMENT,
DeptID INT,
EmployeeName VARCHAR(50),
Gender CHAR(2),
Birthday DATETIME,
IDCard CHAR(18),
Degree VARCHAR(12),
GraduateSchool VARCHAR(50),
GraduateDate DATETIME,
Specialty VARCHAR (50),
Telephone VARCHAR(200),
Salary SMALLINT,
Allowance DECIMAL(18,2),
Skill VARCHAR(2000)
)DEFAULT CHARSET=utf8,ENGINE=INNODB;
CREATE TABLE Department(
DeptID INT PRIMARY KEY,
Deptname VARCHAR(50),
Describ VARCHAR(500)
)DEFAULT CHARSET=utf8;
ALTER TABLE Employee ADD CONSTRAINT fk_emp FOREIGN KEY(DeptID) REFERENCES Department(DeptID);
INSERT INTO Department VALUES(1000,'aa','人事管理');
INSERT INTO Department VALUES(1001,'bb','市场部'),(1002,'cc','销售部');
SELECT * FROM Department;
INSERT INTO Employee VALUES('1',1001,'张三','男','1992-8-2','513030185908132785','大专','电子高专','2013-6-6','软件','7740916','10000','18','打球');
SELECT * FROM Employee;
INSERT INTO Employee VALUES(,1002,'张三','男','1992-8-2',3030185908132785,'大专','电子高专','2013-6-6','软件','7740916','10000','18','打球');
INSERT INTO Employee(DeptID,EmployeeName,Gender,Birthday,IDCard,Degree,GraduateSchool,GraduateDate,Specialty,Telephone,Salary,Allowance,Skill) VALUES(1002,'张三','男','1992-8-2','513030185908132785','大专','电子高专','2013-6-6','软件','7740916','10000','18','打球');
CREATE TABLE dog(
id INT PRIMARY KEY AUTO_INCREMENT,
dogname VARCHAR(20),
dogage INT
)CHARSET=utf8;
INSERT INTO dog(dogname,dogage)VALUES('王朝',4),('马哈',5);
SELECT * FROM dog;
-- 开启事务
START TRANSACTION;
-- 提交事务,提交事务后不能回滚。
COMMIT;
DELETE FROM dog;
-- 事务回滚,回到开启事务之前的状态。
ROLLBACK;
-- concat连接字符串
UPDATE dog SET dogname=CONCAT(dogname,'你好');