I have a table that looks like this:
CREATE TABLE customer (
customer_id integer primary key,
email varchar not null,
...
);
On insert, I want to check wether the email has this specific format:
'a#b.c'
Additionally I want to check that a, b and c contain only latin letters (ASCII 65-90) and a, b can also contain numbers (ASCII 48-57).
I thought of creating a trigger and going through all characters of the varchar. Is there a possibility to do that?
Related
I have a dataset named "supplier_dim" in an excel sheet, and one of the columns "SUPPLIER NAME" has names in Russian supplier_name_data_input
So when I tried creating a table to integrate data into:
create table supplier_DIM
(
ID_supplier int primary key identity (1,1),
supplier_name nvarchar(50),
supplier_code varchar(50)
)
Then I inserted data into this table:
insert into supplier_DIM (supplier_name, supplier_code)
values ('Шпаркасе Лизинг ДОО', 'DC000325')
I get this result when I select all columns:
How can I fix the question mark value problem?
'Шпаркасе Лизинг ДОО' is a varchar literal, and will not preserve the characters in a database without a special collation. Instead use an nvarchar literal, eg
supplier_DIM (supplier_name,supplier_code) values (N'Шпаркасе Лизинг ДОО','DC000325')
I'm begginer on Sql.
I created a table with a field of phone:
CREATE TABLE contact (
phone NUMBER(9),
)
Now I have to set a CHECK to delimite the lenght of digits to the number var, to allow you to only enter a 9-digit number and no more or less.
I try:
ALTER TABLE contact ADD CONSTRAINT CK_phone_right check (length(phone) < 9)
But it doesn't work, because it converts number format to string.
How to do it to keep number type and check it to 9 digits?
Don't store a phone as a number. Use a string instead. If it has to be exactly 9 digits, then:
CREATE TABLE contact (
phone CHAR(9),
constraint chk_contact_phone check ( regexp_like(phone, '^[0-9]{9}$') )
)
A phone number is not really a number. A number is something you can do arithmetic on. Also, it is possible for a phone number to start with 0.
You can put phone number as varchar2 or char type and check the phone number by regular expression:
CREATE TABLE contact (
phone CHAR(9),
constraint valid_phone_number
CHECK (REGEXP_LIKE(phone, 'd{9}$'))
);
What do I need to add to make sure this table is created with the 'name' field as non null but with a value of ""?
CREATE TABLE stuff (id serial primary key, name varchar(64) <-- what goes here??
Standard SQL applies:
CREATE TABLE stuff (
id serial primary key,
name varchar(64) not null default ''
);
It's possible you attempted to use double quotes to specify the text literal like this "", which will explode. Postgres uses single quotes to delimit text literals, like this ''.
See SQLFiddle.
I am building a database and have set field A, Release #, as the primary field. I am running into a problem where duplicate keys are found which gives error on import.
Specifically, release # "49221" is the value of a certain release from a certain website. On a different website, the release # is "0000000049221"
Release# 49221 is a completely different release than is "0000000049221". Is there a way for me to import both of them into the same table while still retaining field "release #" as the primary key?
This usually happens when implicit conversions are given, check the type of your primary key.
For example:
CREATE TABLE TEST
(
COLUMN_1 INT,
COLUMN_2 VARCHAR(8)
)
INSERT INTO TEST
VALUES (4566,'00004566')
SELECT * FROM TEST WHERE COLUMN_1 = COLUMN_2
The second column , it converts to int, and the zeros at the left dissapear, Because zeros at the left in a number has not value (as you know).
Change the second column type to type varchar or char.
You would need to define the primary key as a CHAR or VARCHAR instead of an integer value.
I am having a hard time making my PL/SQL code work. I have a constraint that makes sure 5 digits have been input. The constraint works for any number that does not use a 0 first. Example 0, 1, 2, 33, 401 work but 01, 02, 004 do not work. How can I make it so that I can input numbers such as 00009?
CREATE TABLE table1
(
id NUMBER(5)
CONSTRAINT idconstraint
CHECK (REGEXP_LIKE(id, '[0-9][0-9][0-9][0-9][0-9]'))
);
INSERT INTO table1
VALUES ('00009');
You seem to have the wrong constraint or the wrong data type. If you want leading zeros, use varchar2() or char(). Because you seem to want a fixed length string, try this:
CREATE TABLE table1 (
id CHAR(5)
CONSTRAINT idconstraint
CHECK (REGEXP_LIKE(id, '[0-9][0-9][0-9][0-9][0-9]'))
);
Your problem is that the field is declared as a number. The input string is converted to a number, then back to a string. It loses the leading 0's in the process.