How to make an employee wage restriction? - sql

I have a database with an employee table. This table has the attributes:
name, id, adress and salary.
I need to restrict the salary of employees between US $ 5000 to US $ 10,000. How can I implement this restriction?
My attempt was to define the data type of the salary attribute as:
NUMERIC (5000, 10000)
However when I went to search the sql documentation I saw that NUMERIC (p, s) means
p: precision
s: scale

Try the check constraint when you create the table:
salary NUMERIC
CONSTRAINT salary_range
CHECK(salary >= 5000 and salary <= 10000)

Use Check constraint while creating the table.
create table (
name,
id,
adress,
salary check (salary between ( 5000 and 10,000))

Related

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"

Oracle SQL Check

I'm trying to implement an Oracle SQL database, in one of my tables I must introduce a restriction which does not allow to have more than 4 people in the same group:
I've tried this:
CREATE TABLE PERSON (name VARCHAR (20) PRIMARY KEY, group VARCHAR (3), CHECK (COUNT (*) group FROM PERSON) <=4);
also this (among others):
CREATE TABLE PERSON (name VARCHAR (20) PRIMARY KEY, group VARCHAR (3), CHECK NOT EXISTS (Select COUNT(*) FROM PERSON GROUP BY group HAVING COUNT(*) > 4);
But I'm getting errors every time (ORA-00934: group function is not allowed here or ORA-02251: subquery not allowed here.
What is the correct way to do it?
You have multiple issues with this
CREATE TABLE PERSON (
name VARCHAR(20) PRIMARY KEY,
group VARCHAR(3),
CHECK (COUNT (*) group FROM PERSON) <=4);
);
Oracle explicitly prefers VARCHAR2() to VARCHAR().
GROUP is a really bad name for a column, because it is a keyword. Surely you can find something like group_name or whatever for the name.
CHECK constraints only work within a single row.
Probably the best way to handle this is:
Create a new table called groups -- or whatever. It should have a group_id as well as group_name and num_persons.
Add triggers to person to keep the counter up-to-date for inserts, deletes, and updates to person.
Add a check constraint to groups, say check (num_persons <= 4).
You need to create the table as following:
CREATE TABLE PERSON (
name VARCHAR2(20) PRIMARY KEY,
group_ VARCHAR2(3) -- added _ after column name
); -- used varchar2 as data type of column
Then create before insert trigger as following:
create trigger person_trg
before insert on person
for each row
declare
group_cnt number;
begin
select count(distinct name)
into group_cnt
from person
where group_ = :new.group_;
if group_cnt = 4 then
raise_application_error(-20001, 'more than 4 persons are not allowed in the group');
end if;
end;
/
I have used distinct person name as more than 4 distinct persons are not allowed in the group as per your requirement.
db<>fiddle demo
Cheers!!

How to format text data with Azure SQL

I just began using Azure SQL Query Editor to help me learn.
I have problems formatting TEXT data when inserting data. I know I should be formatting data with "Quotations" around TEXT data, however, I continue to run into the following error: Incorrect syntax near '“'.
Example of table and dummy data:
CREATE TABLE EMPLOYEE ( eno INTEGER(11), ename TEXT(30), zip INTEGER(5) DEFAULT 47405, hdate DATE, dept TEXT (30), salary INTEGER(15,3), constraint EMPLOYEE_PK primary key (eno));
INSERT INTO EMPLOYEE VALUES (5476343153, “Stanley Keller”, , 06-Mar-99, “Sales”, 75689);
INSERT INTO EMPLOYEE VALUES (5286469147, “Sergio Murray”, , 13-Mar-92, “Marketing”, 148769);
INSERT INTO EMPLOYEE VALUES (2454152346, “Laurie Hawkins”, , 22-Mar-98, “Marketing”, 92474);
Why is this happening to me? Thank you.
Gordon is correct - single identifiers are the norm for ANSI SQL.
In SQL Server/SQL Azure, if you want to use a different quoted identifier, there is a session option to allow it if needed.
Here is the documentation on that feature which should make your original query work (non-standard SQL):
https://learn.microsoft.com/en-us/sql/t-sql/statements/set-quoted-identifier-transact-sql?view=sql-server-2017
SET QUOTED_IDENTIFIER ON
Please try the following. It worked for me.
DROP TABLE EMPLOYEE
GO
CREATE TABLE EMPLOYEE ( eno BigInt, ename NVARCHAR(30), zip INT DEFAULT 47405, hdate DATE, dept NVARCHAR(30), salary decimal(15,3), constraint EMPLOYEE_PK primary key (eno));
GO
INSERT INTO EMPLOYEE (eno, ename, hdate, dept, salary) VALUES (5476343153, 'Stanley Keller', '06-Mar-99', 'Sales', 75689);
INSERT INTO EMPLOYEE (eno, ename, hdate, dept, salary) VALUES (5286469147, 'Sergio Murray', '13-Mar-92', 'Marketing', 148769);
INSERT INTO EMPLOYEE (eno, ename, hdate, dept, salary) VALUES (2454152346, 'Laurie Hawkins', '22-Mar-98', 'Marketing', 92474);
Please take note the data types were wrong, You were treating integer like decimal and for integers the data type is int. Also you don't have to specify commas for the fields where you want the default value to take place. On the eno field I had to specify BigInt as data type since the values on the INSERT statements are to big for the integer (int) data type.
I changed double quotes for single quotes on those INSERT statements.

Error on CHECK constraint in HANA 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.

Is it possible to populate table using trigger on another table in oracle 12c

Using oracle 12c, I have a table for employee and a table for managers, if the newly inserted employee salary >=5000 then he/she is considered manager. So I'd like to create trigger on table employee that checks if the salary of the newly inserted employee >=5000 this row should be duplicated in the manager table. IS this possible? If yes, could you simply give me the right syntax.
Some general words first: This could be considered bad database design. If you consider an employee beyond a certain salary a manager, this almost screams for a column in the same table, either physical or virtual. For example, it could look like this:
CREATE TABLE employees (
id NUMBER,
first_name VARCHAR2(10),
last_name VARCHAR2(10),
salary NUMBER(9,2),
is_manager as (case when salary >= 5000 then 1 else 0 end)
CONSTRAINT employees_pk PRIMARY KEY (id)
);
If you still want to use a trigger and a second managers table, it could work like this:
CREATE OR REPLACE TRIGGER trig_emp_insert
AFTER INSERT
ON employees
FOR EACH ROW
BEGIN
if (:new.salary >= 5000) then
insert into managers (...) values (...)
end if;
END;