Adding constraint to check if the value contains specific characters- SQL Server - sql

My Check constraint conflicts when adding values, any answer to why it is wrong will be much appreciated.
Here is the question:
Ensure that the Student’s NIC number contains 9 digits (0-9) and one
character which is “V” or “v”.
Here is the value I want to add to the table:
946785467v
Here is the constraint I used:
ALTER TABLE Student
ADD CONSTRAINT stdNIC CHECK(NIC LIKE '[0-9]{9}[V-v]');

Better to change the constraint as below.
ALTER TABLE Student ADD CONSTRAINT stdNIC CHECK(NIC LIKE '%[0-9]%[V-v]')
Example validated on db<>fiddle<>example

Related

Using a 'not in' query inside of a check constraint in SQL

I have two tables, one of student and one of staff that look as such:
create table student (
id int not null primary key
)
create table staff (
id int not null primary key
)
I want the id in each to be unique. I know this isn't how it should be in production but I'm just trying to see why my check constraint doesn't work and I'm using a simpler example to explain.
I then alter the tables to include the check as follows:
alter table student add constraint not_staff check (id not in (select id from staff))
alter table staff add constraint not_student check (id not in (select id from student))
These checks seem to be invalid.
My question is whether we're allowed to have these kinds of SQL statements inside of a check constraint. If so, why is the above constraint invalid and how would I go about fixing it.
Thanks!
You can't use queries in a check constraint in Db2. Refer to the description of the CREATE TABLE statement.
CHECK (check-condition)
Defines a check constraint. The search-condition must be true or unknown for every row of the table.
search-condition
The search-condition has the following restrictions:
...
The search-condition cannot contain any of the following (SQLSTATE 42621):
Subqueries
The easiest way to achieve your goal is not to create constraints, but create a sequence and use it in before triggers on both tables.

Unclear constraints in SQL's CREATE TABLE

In a Coursera course, there is a snippet of code:
I don't understand the parts:
CONSTRAINT AUTHOR_PK
(author_id) (after PRIMARY KEY)
Could you please explain?
Clarifications: for CONSTRAINT AUTHOR_PK, I don't understand why CONSTRAINT is there explicitly but it's not there for the other attributes of the table. I also don't know what AUTHOR_PK is used for.
For (author_id), I don't understand its presence. Since PRIMARY KEY is written on the same line as author_id, isn't it already implicit that author_id will be used as the primary key?
I'm very new to SQL. I consulted
https://www.w3schools.com/sql/sql_create_table.asp
https://www.w3schools.com/sql/sql_constraints.asp
but could not resolve these issues myself.
There are two types of constraints you can create in a CREATE TABLE statement. These are column constraints and table constraints.
Column constraints are included in the definition of a single column.
Table constraints are included as separate declarations, not part of a column definition.
This is the same table with the primary key declared as a table constraint:
CREATE TABLE Author
(author_id CHAR(2),
lastname VARCHAR(15) not null,
...,
CONSTRAINT PK_AUTHOR PRIMARY KEY (author_id)
)
What you have in your example is a constraint being declared as a column constraint. Normally, column constraints don't have to name which columns they're relevant to since they're part of the column's definition, and indeed in some dialects of SQL, the sample you've shown would be rejected, because it does name the columns explicitly.
PK_AUTHOR, in both your sample and mine, is being used to give a specific name to the constraint. This is helpful if you'll later need to remove the constraint. If you don't want to name the constraint, then CONSTRAINT PK_AUTHOR may be omitted from either sampe.
The CONSTRAINT keyword is necessary when you want to provide a specific name for the constraint, in this case AUTHOR_PK. If you don't do this, a name would be auto-generated, and these names are generally not very useful. All the NOT NULL constraints in this example would have auto-generated names.
In my experience, it's standard practice to name all constraints except NOT NULL ones.
I think you are right that (author_id) is unnecessary in this example, as it is implied by the fact that the constraint is declared for that column already. But the syntax allows it. (I wonder if it would allow specifying a different column in this position - I don't think so but haven't tried it.)
The syntax to specify columns is more useful when you want to declare a multiple-column key. In this case, the CONSTRAINT clause would be specified as if it were another column in the table definition:
...
country CHAR(2),
CONSTRAINT one_city_per_country UNIQUE (country,city)
);

How to create CHECK constraints in Informix?

I need to create validations in my fields (columns) in Informix tables. Inside SQL Server, the names are CHECK (for example: CHECK (Age>=18))
How to create in Informix, or, what's the similar syntax in Informix?
If you want add check constraint you could do it in two ways:
1) The next example adds a new unit_price column to the items table and includes a check constraint to ensure that the entered value is greater than 0:
ALTER TABLE items
ADD (unit_price MONEY (6,2) CHECK (unit_price > 0));
2) To create a constraint that checks values in more than one column, use the ADD CONSTRAINT clause. The following example builds a constraint on the column that was added in the previous example. The check constraint now spans two columns in the table.
ALTER TABLE items ADD CONSTRAINT CHECK (unit_price < total_price);
Have a look at the doc

Trouble Altering Table SQL

Hey guys just wondering what i'm doing wrong in this script.
ALTER TABLE SSV_TOURS (
ADD CRUISE_ID# CHAR(5),
ADD CONSTRAINT TOURS_CRUISEID#_FK FOREIGN KEY (CRUISE_ID#) REFERENCES SSV_CRUISES(CRUISE_ID#)
);
When i do the ADD commands individually the table alters, so I'm not really sure what I'm doing wrong. Thanks in advance
In Oracle, alter table only allows one modification at a time. You can see this in the syntax diagram in the documentation: There are no back arrows.
So:
ALTER TABLE SSV_TOURS ADD CRUISE_ID# CHAR(5);
ALTER TABLE SSV_TOURS ADD CONSTRAINT TOURS_CRUISEID#_FK FOREIGN KEY (CRUISE_ID#) REFERENCES SSV_CRUISES(CRUISE_ID#);
I think you can't mix column_clause and constraint_clause in one alter table sentence. However in your case you either need to split your alter table to two (one with add column and other with add constraint) or slightly rewrite your statement as
ALTER TABLE SSV_TOURS (
ADD CRUISE_ID# CHAR(5)
CONSTRAINT TOURS_CRUISEID#_FK FOREIGN KEY (CRUISE_ID#)
REFERENCES SSV_CRUISES(CRUISE_ID#)
);

Add foreign key on column with different types

I need to add a foreign key constraint on an integer column to reference a text column in another table. Both represent year in 4 digits format, but creating the constraint like
ALTER TABLE a
ADD FOREIGN KEY (year_int)
REFERENCES b (cast(year_text as integer));
doesn't work and give a syntax error.
So, is it possible without using a view (the b table is already linked by several tables/views)? Or is there a query which can convert all linked tables/views type from text to integer?
Cheers