Creating a table syntax errors in sql - sql

I'm trying to create a SQL table for a school assignment. I'm required to name the constraints, and I am receiving a syntax error when creating my table
SELECT * FROM demo;
CREATE TABLE PRODUCT
(
ProductID NUMBER,
ProductDescription VARCHAR(200),
CONSTRAINT ProductPK PRIMARY KEY (ProductID)
);
CREATE TABLE ITEM
(
ItemNum NUMBER
CONSTRAINT Inull NOT NULL
CONSTRAINT ItemPK PRIMARY KEY(ItemNum),
ItemDesc VARCHAR(200)
);
The syntax error is as follows:
near "(": syntax error
It is likely something simple, but this is my first time writing SQL.

You have a few errors .. One, you are missing commas after ItemNum NUMBER and CONSTRAINT Inull NOT NULL
Additionally there are other ways to define NOT NULL, you technically don't need to use CONSTRAINT in traditional SQL.
And depending on the flavor of SQL you are using .. I like to use INT or INTEGER instead of NUMBER -- It's basically the same as NUMBER() with a few exceptions, but if you are using whole numbers I would use INT or INTEGER
SELECT * FROM demo;
create TABLE PRODUCT
(
ProductID INT,
ProductDescription VARCHAR(200),
CONSTRAINT ProductPK PRIMARY KEY (ProductID)
);
CREATE TABLE ITEM
(
ItemNum INT NOT NULL,
CONSTRAINT ItemPK PRIMARY KEY(ItemNum),
ItemDesc VARCHAR(200)
);
UPDATE
And as #Schwern suggests .. Your query can be further simplified, as there really is no need for CONSTRAINTS here .. I would also recommend Auto Incrementing your IDs .. Thus removing the need for NOT NULL on the ID. This also negates the need to supply an ID with the INSERT query ..
SELECT * FROM demo;
create TABLE PRODUCT
(
ProductID INT PRIMARY KEY AUTO_INCREMENT,
ProductDescription VARCHAR(200)
);
CREATE TABLE ITEM
(
ItemNum INT PRIMARY KEY AUTO_INCREMENT,
ItemDesc VARCHAR(200)
);

Related

How to fix "ERROR: FK name length exceeds maximum allowed length(30)" in SQL Developer Data Modeller

I'm trying to turn the Logical Model of my Database into a DDL script, but I don't know how to fix this error in the DDL script or Data Modeller:-- ERROR: FK name length exceeds maximum allowed length(30)
It seems to be based on my junction-table's Primary Key, which is made up from two Foreign Keys from the two neighboring tables.
I have tried changing the names of the Primary Keys in the neighboring tables, but when I try to generate a NEW DDL script with Data Modeler, it still generates the old script.
Here's the sections of code that create the 3 tables and link them together:
CREATE TABLE items (
item_no NUMBER (8) NOT NULL,
"year" DATE,
price NUMBER (20,2)
);
ALTER TABLE items ADD CONSTRAINT items_pk PRIMARY KEY ( item_no );
CREATE TABLE purchase_order (
order_no NUMBER(8) NOT NULL,
quantity INTEGER,
item_description VARCHAR2(200),
unit_price NUMBER(20,2),
total NUMBER(20,2),
order_date DATE,
sales_person_code VARCHAR2(5) NOT NULL,
supplier_id NUMBER(3) NOT NULL
);
ALTER TABLE purchase_order ADD CONSTRAINT purchase_order_pk PRIMARY KEY ( order_no );
CREATE TABLE purchase_order_items (
purchase_order_order_no NUMBER(8) NOT NULL,
items_item_no NUMBER(8) NOT NULL
);
ALTER TABLE purchase_order_items ADD CONSTRAINT purchase_order_items_pk PRIMARY KEY ( items_item_no,
purchase_order_order_no );
ALTER TABLE purchase_order_items
ADD CONSTRAINT purchase_order_items_items_fk FOREIGN KEY ( items_item_no )
REFERENCES items ( item_no );
-- ERROR: FK name length exceeds maximum allowed length(30)
ALTER TABLE purchase_order_items
ADD CONSTRAINT purchase_order_items_purchase_order_fk FOREIGN KEY ( purchase_order_order_no )
REFERENCES purchase_order ( order_no );
ALTER TABLE purchase_order
ADD CONSTRAINT purchase_order_sales_person_fk FOREIGN KEY ( sales_person_code )
REFERENCES sales_person ( code );
ALTER TABLE purchase_order
ADD CONSTRAINT purchase_order_supplier_fk FOREIGN KEY ( supplier_id )
REFERENCES supplier ( id );
So I'm not sure exactly what FK name length is too long and what I need to change in the script to fix this error.
Oracle limits identifiers to 30 characters, so
purchase_order_items_purchase_order_fk needs to be shorted. Perhaps to something like poi_purchase_porder_fk.

create table syntax error in Microsoft Access?

Here is my code, i'm trying to make multiple tables:
Create Table Order_t
(
Id AutoIncrement Not Null,
OrderDate DateTime Not Null,
CustId Int Not Null,
Primary Key(Id),
Foreign Key(CustId) References Customer_t(Id)
(;
Create Table PersonRole_t
(
PersonRoleID Autoincrement Not Null,
Person_ID int Not Null,
Primary Key(PersonRoleID, Person_ID),
Foreign Key(Person_ID) References Person_T(Person_ID)
(;
Create Table Product_t
(
Id Text(10) Not Null,
Name Text(30) Not Null,
Description Text(30),
Finish Text(30),
UnitPrice Currency Not Null,
Primary Key(Id)
) ;
Whenever I run it in Microsoft Access, I get an error in the CREATE TABLE statement (it highlights the PersonRole_T table definition). Not sure what to do, rather new to SQL.
Use CLOSE parenthesis at the end of CREATE Table statement instead of OPEN parenthesis
Create Table Order_t
(
Id AutoIncrement Not Null,
OrderDate DateTime Not Null,
CustId Int Not Null,
Primary Key(Id),
Foreign Key(CustId) References Customer_t(Id)
); -- Not (;
Create Table PersonRole_t
(
PersonRoleID Autoincrement Not Null,
Person_ID int Not Null,
Primary Key(PersonRoleID, Person_ID),
Foreign Key(Person_ID) References Person_T(Person_ID)
); -- Not (;
The table Person_T with a key of Person_ID needs to exist before the References Person_T(Person_ID) statement can execute. Based on your naming convention, I would guess the statement should rather be References Person_T(Id).
Consider changing your naming convention so that a data element does not change its name depending its location realtive to tables/views. Also consider whether the _t suffix is worth the bother.
#Prdp's point about the close parens is valid too.

Error while creating TABLE in database

I am using oracle 10g ex to learn so here is my code
CREATE TABLE MINE
(
NAME VARCHAR(10),
ID INT(3) PRIMARY KEY
);
and my error is
ORA-00907: missing right parenthesis.
but I don't where I missed the right parenthesis. There is any other chance or something I have to know to solve this issue.
INT does not need a size - it is an alias for NUMBER(38).
CREATE TABLE MINE
(
NAME VARCHAR(10),
ID INT PRIMARY KEY
);
However, what you probably want is to use VARCHAR2 and NUMBER types:
CREATE TABLE MINE
(
NAME VARCHAR2(10),
ID NUMBER(3,0) PRIMARY KEY
);
And now is the time to get into good habits - you probably also want to name your constraints:
CREATE TABLE MINE
(
NAME VARCHAR2(10),
ID NUMBER(3,0) CONSTRAINT mine__id__pk PRIMARY KEY
);
The int datatype doesn't take a size argument:
CREATE TABLE MINE
(
NAME VARCHAR(10),
ID INT PRIMARY KEY -- Here!
);
create table Mine(name varchar(10),Id number(20) PRIMARY KEY);

product set a column unique without being the primary key

I wanna create the following table:
create table product (id bigint not null, product_type varchar(50), product_name varchar(100), available_from TIMESTAMP, available_to TIMESTAMP, primary key (id));
My table's key is the 'id'.
when inserting in the table, I wanna that the product_type be unique.
How to do that without setting the product_type the key for my table
ALTER TABLE <your table name>
ADD CONSTRAINT unique_product_type UNIQUE(product_type);
I can't see a table name in your create table SQL.
The function NEWID() generates a new unique id. Maybe you can make a trigger which insets this in every insert or make the default value to be NEWID(). Well the second option seems a bit better :P
CREATE TABLE Test123
(
ID NVARCHAR(200) DEFAULT (NEWID()),
name NVARCHAR(100)
)
INSERT INTO Test123 (name) VALUES ('test')
SELECT * FROM Test123
DROP TABLE Test123
Add a unique constraint, something like this (not tested):
http://www.w3schools.com/sql/sql_unique.asp
CREATE TABLE Product
(
id bigint NOT NULL,
product_type varchar(50),
product_name varchar(100),
available_from TIMESTAMP,
available_to TIMESTAMP,
primary key (id),
UNIQUE (product_type)
)

SQL Server : create error how to write database name with the table name

CREATE DATABASE agom COLLATE Arabic_CI_AS
CREATE TABLE Branches
(
ID INT PRIMARY KEY NOT NULL IDENTITY,
NAME VARCHAR(255) NOT NULL
)
CREATE TABLE agom.Brands
(
ID INT PRIMARY KEY NOT NULL IDENTITY,
NAME VARCHAR(255) NOT NULL
)
CREATE TABLE agom.Work_Order
(
NUMBER INT NOT NULL,
BRANCHID INT NOT NULL,
BRANDID INT NOT NULL,
WDATE DATE NOT NULL,
REPAIRSTATUS VARCHAR(255),
REPAIRCOST VARCHAR(255),
REMARK VARCHAR(500),
PRIMARY KEY (NUMBER,BRANCHID,BRANDID)
)
CREATE TABLE agom.Profiles
(
ID INT PRIMARY KEY NOT NULL IDENTITY,
USERNAME VARCHAR(25) NOT NULL,
PASS VARCHAR(25) NOT NULL
)
ALTER TABLE agom.Work_Order
ADD CONSTRAINT branchfk
FOREIGN KEY (BRANCHID) REFERENCES Branches(ID)
ALTER TABLE agom.Work_Order
ADD CONSTRAINT brandfk
FOREIGN KEY (BRANDID) REFERENCES Brands(ID)
I get an error that cannot create table
I try to write database name with the table name db.tablename but it's not working
I need to create the database then create the tables and its constraints but I don't know where is the error.I am a sql noob
It's never Database.Table.
It's either Table, or Schema.Table, or Database.Schema.Table, or Server.Database.Schema.Table.
You probably just want to insert USE agom right after create database, and then only refer to tables by name.
Alternatively, you can refer to your tables as agom.dbo.Work_Order, dbo being the default database schema.
See Using Identifiers As Object Names for general reference.