0

创建用于设置工资和奖金范围的数据域。

这是表格:

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

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

4

1 回答 1

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)
);

(你也有两次薪水列在那里......)

于 2013-11-14T05:51:15.107 回答