Data Domain MySQL的错误1064

时间:2013-11-14 05:42:13

标签: mysql

创建数据域以设置薪水和奖金的范围。

以下是表格:

CREATE TABLE Departments (
Dept_ID INT (6) NOT NULL,
Dept_NAME VARCHAR (6) NOT NULL,
DeptHead_ID INT (6) NULL,
DeptAA VARCHAR (6), 
ParentDept_ID INT (4) NULL,
Location VARCHAR (4) NOT NULL,
DeptType VARCHAR (6),
Primary key (Dept_ID)
);

CREATE TABLE Employee (
Emp_ID INT (6) NOT NULL,
Name VARCHAR (6) NOT NULL,
Dept_ID INT (6) NOT NULL,   
Tax_ID INT (4) NOT NULL,
Country VARCHAR (4) NOT NULL,
Hire_Date DATE,
Birth_Date DATE,
Salary INT (6),
Bonus INT (6),
AddressInfo VARCHAR (6),  
Salary INT (6),
(salary > 0)
and (mod(salary, 10) = 0) 
and (bonus =< Salary)
PRIMARY KEY(Emp_ID)
FOREIGN KEY(Dept_ID) REFERENCES Departments(Dept_ID)
);
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds 
to your MySQL server version for the right syntax to 
use near '(salary > 0) and 
(mod(salary, 10) =0)
and (bonus =< Salary),
PRIMARY KEY(Emp_ID' at line 13

数据域和错误的约束:

ALTER TABLE Employee
ADD CONSTRAINT ck_Employee_Salary_dmn
CHECK ((salary > 0)
and 
(mod(salary, 10) = 0) 
and 
(bonus =< Salary)
);

ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that   corresponds to your MySQL server version for the right syntax to use near '=< Salary))' at   line 7

如果我能提供更多信息,请告诉我。

1 个答案:

答案 0 :(得分:1)

您不能在CREATE TABLE语句中限制值。所以这些行无效:

(salary > 0)
and (mod(salary, 10) = 0) 
and (bonus =< Salary)

通常,您可以将其留给应用程序来验证进入它的数据。还有一些你可以使用的MySQL构造(查找TRIGGERs),但我倾向于回避这些,因为它们不可移植。无论如何,试试这个:

CREATE TABLE Employee (
Emp_ID INT (6) NOT NULL,
Name VARCHAR (6) NOT NULL,
Dept_ID INT (6) NOT NULL,   
Tax_ID INT (4) NOT NULL,
Country VARCHAR (4) NOT NULL,
Hire_Date DATE,
Birth_Date DATE,
Salary INT (6),
Bonus INT (6),
AddressInfo VARCHAR (6),  
PRIMARY KEY(Emp_ID),
FOREIGN KEY(Dept_ID) REFERENCES Departments(Dept_ID)
);

(你还有两次Salary栏......)