Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2012 Forums
 Transact-SQL (2012)
 Can you help me with this create table command?

Author  Topic 

kofijr
Starting Member

2 Posts

Posted - 2015-01-14 : 14:15:14
I am new to sql and trying to create a check command that ensures only letter A'B','c','D','E','F','G can be inserted into the table, is this even applicable?

Heres my rough syntax

Create Table GRADE (
Grade VARCHAR2(1) CONSTRAINT pk_Grade PRIMARY KEY
CONSTRAINT check_grade
CHECK (substr(Grade = 'A','B','c','D','E','F','G')),

Salary_Scale VARCHAR2(2) CONSTRAINT check_SScale
CHECK (substr(Salary_Scale = 'S1' or 'S2')),
)
/

what is wrong with this SQL syntax?

Thank you.

gbritton
Master Smack Fu Yak Hacker

2780 Posts

Posted - 2015-01-14 : 15:44:13
There's lots wrong with the syntax!

1. THere is no varchar2 type
2. there's no substr function in sql, though there is a substring function. Secondly, the substring function takes three parameters(string, start, end) and an expression like Grade = 'A','B','c','D','E','F','G' is not allowed (I'm not even sure what it would mean!)

However, you can try this:


Create Table GRADE (
Grade VARCHAR(1) CONSTRAINT pk_Grade PRIMARY KEY
CONSTRAINT check_grade
CHECK (grade in ('A','B','c','D','E','F','G')),

Salary_Scale VARCHAR(2) CONSTRAINT check_SScale
CHECK (Salary_Scale in ('S1', 'S2')),
)
Go to Top of Page

kofijr
Starting Member

2 Posts

Posted - 2015-01-14 : 22:39:37
Thanks for the syntax, but damn still not working.
Go to Top of Page

gbritton
Master Smack Fu Yak Hacker

2780 Posts

Posted - 2015-01-15 : 00:52:32
What's not working
Go to Top of Page

ScottPletcher
Aged Yak Warrior

550 Posts

Posted - 2015-01-15 : 11:20:55
[code]
Create Table GRADE (
Grade nchar(1) CONSTRAINT pk_Grade PRIMARY KEY
CONSTRAINT check_grade
CHECK (Grade in ('A','B','c','D','E','F','G')),
Salary_Scale nchar(2) CONSTRAINT check_SScale
CHECK (Salary_Scale in ('S1', 'S2'))
)

[/code]
Go to Top of Page
   

- Advertisement -