Wildcard character in CHECK in Create table taken as string - sql

I created an info table that stores employee record, the SQL query for it as follows...
CREATE TABLE info1 (
empid VARCHAR(8) PRIMARY KEY CONSTRAINT empchk
CHECK (empid IN ('kh\%' ESCAPE '\''),
initials CHAR(6), fname CHAR(25) NOT NULL,
lname CHAR(25),
userstatus INTEGER NOT NULL,
designation CHAR(10) NOT NULL
);
Now, as you can see constraint in empid is kh% where - as far as I remember - % means that any number of the following characters (limited to 8 of course) can be anything, right?
I am using Java DB and strangely it has taken the % symbol also to be a part of the string so if I enter khce0001, it says empchk violation, it only takes in kh%
What should I do? Why is this happening?

The mistake in this SQL query is that I have used IN instead of LIKE (which I believe does wildcard checking), so
I dropped the constraint with...
ALTER TABLE info DROP CONSTRAINT empchk;
and altered the table with...
ALTER TABLE info ADD CONSTRAINT empchk CHECK (empid LIKE ('kh%'));
and hence the correct SQL Query should have been...
CREATE TABLE info1 (
empid VARCHAR(8) PRIMARY KEY CONSTRAINT empchk
CHECK (empid LIKE ('kh%')),
initials CHAR(6),
fname CHAR(25) NOT NULL,
lname CHAR(25),
userstatus INTEGER NOT NULL,
designation CHAR(20) NOT NULL
);

Related

Incorrect syntax in SQL Server query

CREATE TABLE identity (
empid VARCHAR(255) PRIMARY KEY,
entry VARCHAR(255)
);
-- Table: vectors
CREATE TABLE vectors (
f_id INTEGER PRIMARY KEY IDENTITY(1,1),
label STRING NOT NULL,
empid STRING REFERENCES identity (empid)
NOT NULL,
vector BLOB NOT NULL
);
I tried to run the above query but it gives me error
Incorrect syntax near expected '.', ID or QUOTED_ID.
I don't understand why it is giving me this error, is it because IDENTITY is a keyword in SQL Server. Kindly help!
Try this:
CREATE TABLE [identity] (
empid VARCHAR(255) PRIMARY KEY,
entry VARCHAR(255)
);
-- Table: vectors
CREATE TABLE vectors (
f_id INTEGER PRIMARY KEY IDENTITY(1,1),
label VARCHAR(255) NOT NULL,
empid VARCHAR(255) REFERENCES [identity] (empid)
NOT NULL,
vector VARCHAR(255) NOT NULL
);
Also, if you are working with SQL Server Management Studio, you can see that some words are colored in blue. It will be better to avoid them using in your code as names of tables, variables and other objects. For example, identity is such word.

Trying to create a Composite Primary Key in MSSQL with only a part of 2 columns

I'm new to SQL and am following a course, however it does not cover the "create table" part.
It only covers statements etc.
What I would like to have is, my primary key (cust_id) to be generated with the "first_name" and the first 3 letters of "last_name".
I.E. I want "John Smith" to become "custid"; JOHNSMI.
I have below code which works (without composite).
CREATE TABLE NL_client (
custid INT PRIMARY KEY IDENTITY (10000, 1),
userid VARCHAR (50) NOT NULL,
first_name VARCHAR (50) NOT NULL,
last_name VARCHAR (50) NOT NULL,
birthday DATE
);
And I found below code (last line added)
CREATE TABLE SAMPLE_TABLE (
custid INT,
userid VARCHAR (50) NOT NULL,
first_name VARCHAR (50) NOT NULL,
last_name VARCHAR (50) NOT NULL,
PRIMARY KEY (first_name, last_name),
);
However, when trying to execte the second query as displayed above it does not create a Primary key. Or when I execure the below queries;
custid INT PRIMARY KEY (first_name, last_name),
Either above in the query or at the end, it does not make a primary key.
Furthermore, I have no idea, nor was I able to find (perhaps I searched wrongly, surely I'm not the first with this "problem") how to select only the first 3 letters of "last_name" to be used as a part of the "custid".
Perhaps this is not possible and I should use "custerid" as an INT Primary key and use "userid" as a composite.
But it would surely help me in the future to be able to use the Primary Key as a reference in Python.
Many thanks in advance for your help and let me learn to understand why it doesn't work!

I cannot create a simple table

I try to Write the SQL code to create the table named ‘EMP_1’. This table is a subset of the EMPLOYEE table, and the structure of the table is summarized as shown below.
This is the information:
Attribute Name Data Type Remarks
EMP_NUM CHAR(3) PK
EMP_LNAME VARCHAR(15) Not Null
EMP_FNAME VARCHAR(15) Not Null
EMP_INITIAL CHAR(1)
EMP_HIREDATE DATE
JOB_CODE CHAR(3) FK (from JOB table)
My code:
CREATE TABLE EMP_1
(
EMP_NUM CHAR(3) PRIMARY KEY,
EMP_LNAME VARCHAR(15) Not Null,
EMP_FNAME VARCHAR(15) Not Null,
EMP_INITIAL CHAR(1) ,
EMP_HIREDATE DATETIME,
JOB_CODE CHAR(3) FOREIGN KEY (JOB_CODE) REFERENCES JOB(JOB_CODE)
);
I keep getting CONSTRAINT error
I think you might be missing a comma before the constraint. This worked when I tried it:
CREATE TABLE EMP_1
(
EMP_NUM CHAR(3) PRIMARY KEY,
EMP_LNAME VARCHAR(15) Not Null,
EMP_FNAME VARCHAR(15) Not Null,
EMP_INITIAL CHAR(1),
EMP_HIREDATE DATETIME,
JOB_CODE CHAR(3),
CONSTRAINT FK_JOBS FOREIGN KEY (JOB_CODE) REFERENCES JOB(JOB_CODE)
);
Make sure your PRIMARY KEY and FOREIGN KEY have the same DATA TYPE.
I've not scrpited a FK for a while, but my recollection is that you can EITHER use:
JOB_CODE CHAR(3) FOREIGN KEY REFERENCES JOB(JOB_CODE)
which will create an FK named JOB_CODE,
OR:
split the line so you create the field and add the constraint separately as per JPW's response.
Your original code tries to combine the 2 approaches which will throw the error you describe.

create table in Oracle BD but gives error

CREATE TABLE employees (
id INT NOT NULL auto_increment PRIMARY KEY (ID),
first_name VARCHAR(20) DEFAULT NULL,
last_name VARCHAR(20) DEFAULT NULL,
salary INT DEFAULT NULL);
I think this is correct query to create table in Oracle database.. but it gives the following error:
ORA-00907: missing right parenthesis
How to correct the statement?
You can validate your SQL using formatting tools such as http://www.dpriver.com/pp/sqlformat.htm
auto_increment seems like a proprietary MySQL extension, so it's not valid for Oracle.
also, "id int not null auto_increment primary key (id)" does not need the last "(id)"
Using Oracle, you shoud try something like this
CREATE SEQUENCE seq;
CREATE TABLE employees
(
id INTEGER NOT NULL PRIMARY KEY,
first_name VARCHAR2(20) DEFAULT NULL,
last_name VARCHAR2(20) DEFAULT NULL,
salary INTEGER DEFAULT NULL
);
INSERT INTO employees
VALUES (seq.NEXTVAL,
'name',
'last name',
1);
Sometimes, SQL is fancy, because even having a standard (ANSI), most DBMS vendors add their proprietary extensions to the SQL creating their own languages, so it's rare the situation where you can port one SQL from one DB into another without any changes.
Also, it's a pretty useless error message. It could at least say which position. (also, there's no missing parenthesis, but an unexpected token)
EDITED : New feature 12c
CREATE TABLE employees(
id NUMBER GENERATED ALWAYS AS IDENTITY,
first_name VARCHAR2(30)
etc.
);
Why would you do default null?
The VARCHAR datatype is synonymous with the VARCHAR2 datatype. To avoid possible changes in behavior, always use the VARCHAR2 datatype to store variable-length character strings.
Replace
id INT NOT NULL auto_increment PRIMARY KEY (ID),
with
id INT NOT NULL auto_increment PRIMARY KEY,
this is more efficient
CREATE TABLE EMPLOYEES_T(
ID NUMBER,
FIRST_NAME VARCHAR2(20) DEFAULT NULL,
LAST_NAME VARCHAR2(20) DEFAULT NULL,
SALARY INTEGER DEFAULT NULL,
CONSTRAINT PK_EMPLOYEES_T PRIMARY KEY(ID)
);

sql 3NF Normalization

is this in 3NF ?
create table movies(
id numeric primary key not null default autoincrement,
name varchar(50) not null,
release-date Date,
price numeric,
rating numeric,
length numeric,
description varchar(1500)
);
create table movies(
id numeric primary key,
name varchar(20)
);
create table genre(
name varchar(20) primary key
);
create table directors(
id numeric primary key not null default autoincrement,
first-name varchar(32) not null,
last-name varchar(32) not null,
gender varchar(8),
dob Date,
biography varchar(1000)
);
create table movie-Star(
id numeric primary key not null default autoincrement,
first-name varchar(20) not null,
last-name varchar(20) not null,
gender varchar(8),
dob Date,
hometown varchar(20)
);
create table movies-cast(
movie-id numeric references movies(id),
actor-id numeric references movie-Star(id),
role varchar(32),
primary key (movie-id, actor-id)
);
Create table Studio(
studio-id numeric references directors(id)
Directer-name varchar(20) not null
name varchar(20) primary key
);
create table directors(
id numeric primary key not null default autoincrement,
first-name varchar(32) not null,
last-name varchar(32) not null,
gender varchar(8),
dob Date,
biography varchar(1000)
);
It looks pretty well structured. I don't see any normalization problems. However:
Movies and Directors tables are created twice.
Genre table is not used for anything (presumably should be in movies).
Same with Studios.
Current arrangement allows only one director per studio. This should probably be A) one studio per director (add studio_id column to directors) or more likely B) many-to-many relationship between studio and director (add new studio_directors table).
Current arrangement does not associate Director with Movie.
You might consider combining Director and Movie-Start into one table called Talent. You have data duplication in which a star is also a director. This is the biggest normalization issue with your design.
if all can not be duplicated again then it is included 3NF.
make sure there are not duplicate data again
It has e PK, so it's in 1NF. It's PK is not composite, so it's in 2NF. All the columns are dependent on nothing but the key, so it's in 3NF. There no keys other than PK, so it's in BCNF.