Error on CHECK constraint in HANA SQL - sql

I'm trying to create a table in the SQL Console of the catalog editor on a HANA SPS12 system with this statement:
create table testEmployee (
empid varchar(4) primary key,
salary number(6),
CHECK (salary >= 50000 and salary <= 200000)
)
It works if I change the CHECK into two separate statements but when I use the statement with the and I get this error:
12:25:20 PM (SQL Editor) Could not execute 'create table testEmployee ( empid varchar(4) primary key, salary number(6), CHECK ...'Error: (dberror) 288 - cannot use duplicate table name: TESTEMPLOYEE: line 2 col 13 (at pos 13)
Any suggestions?
Thanks,
Ross

This seems to be a wrong error message (aka bug) to me.
The problem does not occur when stating the check condition like this:
create table testEmployee (
empid varchar(4) primary key,
salary number(6),
CHECK (salary between 50000 and 200000)
);
In my tests it seemed to me that
A) one can create multiple check constraints by running multiple ALTER TABLE ADD CHECK commands, like so:
create table checker (
empid varchar(4) primary key,
salary number(6)
);
alter table checker add CHECK ( salary + empid > 100);
alter table checker add CHECK ( mod(salary/empid, 2)= 1);
alter table checker add CHECK ( salary between 1000 and 50000);
B) every single CHECK CONSTRAINT can appear on reference each column once. So, SALARY BETWEEN 5000 and 200000 works, while the logical equivalent SALARY >= 5000 and SALARY <= 200000 results in the error message.
UPDATE: the problem with the wrong error message does not occur on HANA 2SP02 and newer.

Related

Add constraint to SQL Server with condition

Consider this table
CREATE TABLE employee
(
employee_number INT PRIMARY KEY,
employee_name NVARCHAR(100),
employee_year INT,
manager_employee_number INT,
salary INT,
FOREIGN KEY(manager_employee_number)
REFERENCES employee(employee_number)
);
I want to add a constraint which every employee's employee_year is less than his manager's and we know every manager is an employee himself. So I wrote this:
ALTER TABLE employee
ADD CONSTRAINT CK_EmployeeYearLessThanManager
CHECK (employee_year <= (SELECT m.employee_year
FROM employee
WHERE m.employee_number = manager_employee_number ));
I get this error:
Subqueries are not allowed in this context. Only scalar expressions are allowed
Is there any possible way to write such a constraint?
As it can be seen from the error message, you cannot implement it as a subquery, but as a work around you can create a scalar-valued user defined function and call it from the check constraint. Here is an example:
CREATE TABLE employee
(
employee_number INT PRIMARY KEY,
employee_name NVARCHAR(100),
employee_year INT,
manager_employee_number INT,
salary INT,
FOREIGN KEY(manager_employee_number)
REFERENCES employee(employee_number)
);
GO
CREATE FUNCTION dbo.get_manager_hire_year(#manager_employee_number INT)
RETURNS INT
AS
BEGIN
DECLARE #e_year int;
SELECT #e_year = employee_year
FROM employee
WHERE employee_number = #manager_employee_number;
RETURN #e_year
END
GO
ALTER TABLE employee
ADD CONSTRAINT CK_EmployeeYearLessThanManager
CHECK (employee_year <= dbo.get_manager_hire_year(manager_employee_number))

Adding constraints to an SQL Table with altering

I have a defined type:
CREATE TYPE salary AS (salary numeric);
and a table holding data:
CREATE TABLE employees (
lastname text,
firstname text,
salary salary,
);
I want to use ALTER TABLEto add a constraint to the salary attribute:
ALTER TABLE employees ADD CONSTRAINT minimum_salary CHECK(salary > 1.47333);
this gives me the error ERROR: operator does not exist: salary > numeric. Why is this the case?
To start with: a type seems overkill to just store a scalar value. Why no just create a check constraint on the column?
create table employees (
lastname text,
firstname text,
salary numeric check(salary > 1.473333)
);
Coming back to your original question: I don't think that it is possible to put a check constraint on a type column. One thing you could do, however, is to create a domain. Domains extend the standard types and do accept check constraints.
Consider this demo:
create domain salary numeric check(value > 1.47333);
create table employees (lastname text, firstname text, salary salary);
insert into employees values('foo', 'bar', 1);
-- ERROR: value for domain salary violates check constraint "salary_check"

Error in primary key in sqlplus?

I am beginner in sql.I am using sqlplus to run the sql query .I used simple query but it shows an error like "MISSING RIGHT PARENTHESIS".My objective is to create the autoincrement primary key .Can anyone solve the error?Thanks in advance...
create table student(rollno int identity(1,1) primary key,
name varchar(20),marks int);
For Oracle, the rollno column could be defined as NUMBER(0010) and primary key.
Then you would need to add an ON INSERT trigger to populate rollno from a SEQUENCE. There are many samples of triggers and sequences on this site.
In oracle 12 you can use a identity column to automatically fill your ID
CREATE TABLE students
(
"ID" NUMBER GENERATED BY DEFAULT AS IDENTITY MINVALUE 1 MAXVALUE 9999999999
INCREMENT BY 1 START WITH 1 ,
"NAME" VARCHAR2(20),
"MARKS" NUMBER(2,0),
CONSTRAINT PK_STUDENTS PRIMARY KEY (ID) ENABLE
);
/
This creates a table without any triggers needed and automatically fills the id column (of not specified with a value) with the next number up to 99999...
If you're using oracle 11 and below, you need a trigger on insert and assign a value (custom_sequence.nextval) to the id column.
CREATE TABLE students
(
"ID" NUMBER(5,0) not null,
"NAME" VARCHAR2(20),
"MARKS" NUMBER(2,0),
CONSTRAINT PK_STUDENTS PRIMARY KEY (ID) ENABLE
);
/
CREATE SEQUENCE SEQ_STUDENTS INCREMENT BY 1 START WITH 1;
/
TRIGGER TC_students
before insert on students
for each row
begin
if (:new.id is null) then
select SEQ_students.nextval into :new.id from dual;
end if;
end;
/
And please use VARCHAR2.

Update trigger assistance

Evening, needing assistance regarding triggers.
Within my development environment I have two tables, one containing employee data (which contains various data errors that will be amended via ALTER TABLE) and the log table.
How do i go about designing a trigger that will update multiple rows contained within the log table such as 'issue_status','status_update_date' when ALTER TABLE sql is used to amend the data contained in the first data table?
-- employee table
CREATE TABLE emp(
emp_id INTEGER NOT NULL,
emp_name VARCHAR(30),
emp_postcode VARCHAR(20),
emp_registered DATE,
CONSTRAINT pk_emp PRIMARY KEY (emp_id));
-- SQL for the log table
CREATE TABLE data_log
(issue_id NUMBER(2) NOT NULL,
table_name VARCHAR2(20),
row_name VARCHAR2(20),
data_error_code NUMBER(2),
issue_desc VARCHAR2(50),
issue_date DATE,
issue_status VARCHAR2(20),
status_update_date DATE);
-- example log insert
INSERT INTO data_log( SELECT DI_SEQ.nextval, 'emp', emp.emp_id, '1', 'emp_name case size not UPPER', SYSDATE, 'un-fixed', '' FROM emp WHERE emp_name != UPPER(emp_name));
This is the example of the issue inserted into the log table. All i want to do is if I update the emp table to set 'emp_name' to Upper the trigger will register this update and change the rows 'issue_status' and 'status_update_date' to 'fixed' and the 'sysdate' of when the change was made
I've done some browsing however i'm still struggling to understand, any literature recommendations would be appreciated also.
Thanks in advance for the assistance.
Note that we don't use ALTER TABLE to update the rows of a table as you have mentioned.We use Update statement. Your trigger should be a BEFORE UPDATE TRIGGER like this.
CREATE OR REPLACE TRIGGER trig_emp_upd BEFORE
UPDATE ON emp
FOR EACH ROW
WHEN ( new.emp_name = upper(old.emp_name) )
BEGIN
UPDATE data_log
SET
issue_status = 'fixed',
status_update_date = SYSDATE
WHERE
row_name =:new.emp_id;
END;
/

Sql create table check date

On creating table i needed to add a check statement for current date should be created date cell. So for example
create table own_departments
(
id number(4) primary key,
name varchar2(30),
num_of_emps number(4) default '0',
est_date date,
check(est_date < sysdate)
);
This check gives errors.
Is anyone know how to check DATE format???
If you are using SQL Server then you will need to create a table constraint...
ALTER TABLE _departments WITH CHECK ADD CONSTRAINT CK_DateGreaterThan CHECK (([est_date]>YOUR_DATE))
GO
ALTER TABLE _departments CHECK CONSTRAINT CK_DateGreaterThan
GO
If you're using SQL Server / Oracle / MS Access, then it should be something like this:
create table own_departments
(
id number(4) primary key,
name varchar2(30),
num_of_emps number(4) default '0',
**est_date date check(est_date < sysdate)**
);