SQL constraint E-Mail - sql

i have an assignment that i have to do in SQL Server Manager.
I have a database, and a table named User. Under user there are a column named e-mail.
The assignment is to create a new constraint to that table that affect the column e-mail.
There has to be only one '#' and minimum one '.'
It is not allowed to be any special characters such as ( !, ", #, ¤, %, etc.) <- this do not include the '#' and '.'
I've tried some different things but cant seem to make it work. Also it should be noticed that I am a beginner.
Thanks for your help

Alternatively you can create your table with constraint, like this which will check if all the given conditions satisfies.
Please change table data with your's
If table not Exist
CREATE TABLE Persons (
ID int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Email varchar(255),
CHECK (len(Email) - len(replace(Email,'#',''))=1 AND len(Email) -
len(replace(Email,'.',''))=1 AND CHARINDEX('!',Email)!>0 AND
CHARINDEX('#',Email)!>0 AND CHARINDEX('%',Email)!>0 AND
CHARINDEX('¤',Email)!>0 AND CHARINDEX('"',Email)!>0)
);
Some Rejected Inputs
some#some.!%#¤com
some#some.!%#com
some#some.%#com
some#some.#com
some#some#com
!some#some#com
etc...
Some Excepted Inputs
some#some.com
some222#some.com
harry_porter#some.com
332#some.com
etc....
If you have already a table then
GO
ALTER TABLE [dbo].[Yourtablename] WITH CHECK ADD Constraint EmailConstraint CHECK (((len([Email])-
len(replace([Email],'#','')))=(1) AND (len([Email])-
len(replace([Email],'.','')))=(1) AND charindex('!',[Email])<=(0) AND
charindex('#',[Email])<=(0) AND charindex('%',[Email])<=(0) AND
charindex('¤',[Email])<=(0) AND charindex('"',[Email])<=(0)))
GO

Related

SQL DROP TABLE FUNCTION

Hello i've created a table if in a VM provided by my university and inserted the following:
create table first_table (record_id int primary key,
first_name varchar(20) not null,
last_name varchar(20) not null);
However, instead of the first_name and last_name I actually inserted my name and am looking to drop the table to recreate it.
" I typed in DROP TABLE [IF EXISTS] first_table "
and then it simply doesn't do anything. Any idea why.
In the Postgresql documentation, when you see something inside square brackets such as [IF EXISTS] it means that "IF EXISTS" is optional. You shouldn't type the square brackets if you put that in.
In this case, though, you know that the table exists so just leave the "IF EXISTS" part off:
drop table first_table;
Given that the problem is that you typed your first and last names instead of the desired column name (first_name and last_name) you can simply rename the columns using
alter table first_table
rename leo as first_name
alter table first_table
rename whatever_your_last_name_is as last_name
Try this:
DROP TABLE first_table;
In PSQL you should be able to do so using the command
DROP TABLE first_table;

Why does the zipcode fail the check constraint?

I feel like I am probably missing something really simple, but I really can't figure out what I'm doing wrong. I'm trying to use a check constraint to make sure zipcodes are 5 digit numbers, but the check restraint keeps failing. Here is the table creating with the constraint:
Create Table Students (
StudentID Int Primary Key Identity(1,1)
StudentNumber nVarchar(100) Unique Not Null,
...
StudentZipCode nChar(10) Not Null
)
Go
Alter Table Students Add Constraint chZipCode
CHECK (StudentZipCode LIKE '[0-9][0-9][0-9][0-9][0-9]' OR StudentZipCode
Like '[0-9][0-9][0-9][0-9][0-9]-[0-9][0-9][0-9][0-9]')
Go
Codes like 12345-6789 work, but when I try to insert the values like '12345' or '01234' it gives me this error:
The INSERT statement conflicted with the CHECK constraint "chZipCode". The conflict occurred in database ..., table "dbo.Students", column 'StudentZipCode'.
It fails because you defined the zip code as a char() instead of a varchar(). Hence, it has a bunch of spaces padding it out.
So, define it as:
Create Table Students (
StudentID Int Primary Key Identity(1,1),
StudentNumber nVarchar(100) Unique Not Null,
StudentZipCode nVarChar(10) Not Null,
CHECK (StudentZipCode LIKE '[0-9][0-9][0-9][0-9][0-9]' OR
StudentZipCode LIKE '[0-9][0-9][0-9][0-9][0-9]-[0-9][0-9][0-9][0-9]')
);
Then '12345' works, because it matches the first of the LIKE patterns.
'012344' does not work, because no pattern has six digits in a row.
Here is a SQL Fiddle.

How to do same thing on multiple conflicts in PostgreSQL?

For example if I have a table:
create table test(
username varchar(50) PRIMARY KEY,
customer varchar(12),
nickname varchar(12)
);
create unique index unique_customer_nickname on test(customer,nickname);
Therefore username is unique and (customer,nickname) together are unique.
Then If I want to write an upsert statement like this:
Insert into test(username,customer,nickname) values('utest','ctest','ntest')
on conflict(username) or on conflict(customer,nickname) DO UPDATE ....
but this gives me syntax error.
I also tried on (conflict(username) or conflict(customer,nickname))
but this also returns a syntax error.
so what I want is for different conflicts to perform the same thing.

Newly added table not working

I am working on DB2 9.7 database. I was using SquirrelSQL for GUI purpose. However ever since I applied an alter command to one of my tables , I started facing problems with the table, and any further select queries asked for "reorg" of the table. to overcome this, I renamed the old table and created new table. However the create query didn't execute properly in Squirrel ,and so downloaded DBViewer for my STS(Spring Source Tool Suite.)I executed the create table query from DBViewer, but the issue now is neither am I able to access the newly created table from my JAVA code , nor from Squirrel.
I am completely clueless as to what could be the problem. Has anyone got any idea?
Following is the Structure of my table:
CREATE TABLE DB2ADMIN.CERT
(
CERT_ID CHAR(36) NOT NULL,
CERT_CD CHAR(1) NOT NULL,
CERT_NBR CHAR(10) NOT NULL,
CERT_REQ_USR_CD_1 CHAR(10),
CERT_REQ_USR_CD_2 CHAR(10),
CERT_REQ_USR_CD_3 CHAR(10),
CERT_REQ_USR_CD_4 CHAR(10),
CERT_REQ_USR_TXT_1 VARCHAR(255),
CERT_REQ_USR_TXT_2 VARCHAR(255),
CERT_REQ_USR_TXT_3 VARCHAR(255),
CERT_REQ_USR_TXT_4 VARCHAR(255),
CERT_REQ_USR_TXT_5 VARCHAR(255),
CERT_REQ_USR_TXT_6 VARCHAR(255),
CERT_REQ_USR_TXT_7 VARCHAR(255),
CERT_REQ_USR_TXT_8 VARCHAR(255),
CERT_REQ_USR_TXT_9 VARCHAR(255),
CERT_REQ_USR_DT DATE,
LAST_MDF_USER_ID CHAR(25) NOT NULL,
LAST_MDF_ACY_TS TIMESTAMP(26,6) NOT NULL,
CONSTRAINT SQL111017085116710 PRIMARY KEY (CERT_ID)
);
In my alter query I changed the datatype of CERT_NBR form INTEGER to CHAR
using the command;
ALTER TABLE CERT ALTER COLUMN CERT_NBR SET DATA TYPE INTEGER
any further select queries asked for "reorg" of the table.
This is a common occurrence after altering tables in DB2. It should be trivially solved by calling:
reorg table table-name;
This is a command-line command, rather than an sql statement, but you can call it via SQL with the admin_cmd procedure:
call sysproc.admin_cmd('reorg table table-name');
I'm not sure why you are unable to access the new table. The error message should help you resolve this. Some possibilities:
You created it with a different username than the one you are trying to access it with.
You never committed after the create table statement.
Something went wrong and the table ended up in an inoperable state.

Stop double entry of data in database

I am using VS 2008 and SQL Server 2005. And the problem is that when I insert a new record which is string data. It continues on entering the same data which is already exiting in the table, again and again. But I want that where my insert query is running. I place the check there that it does not allow similar data in the table.
My scenario:
I have to decide on these two string columns: 'source' and 'destination'
If similar source and destination occur in any record we must stop we the entry on record.
Share the solution.
The easiest way to do it is by putting a 'UNIQUE constraint' on your database. Then, each time an SQL UPDATE or an SQL INSERT is executed, the database server would check the validity of the new SQL action and cancel it if it violates your data integrity constraing.
For example (copying from this SQL tutorial):
CREATE TABLE Persons
(
P_Id int NOT NULL UNIQUE,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Address varchar(255),
City varchar(255)
)
If you want to add a UNIQUE constraint on two columns, you could use such a statement:
CREATE TABLE Example
(Col1 int NOT NULL,
Col2 int NOT NULL,
UNIQUE (Col1, Col2)
)
Hope I helped!