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);
Related
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)
);
I have been searching and playing around with the code trying to figure out why its giving me parenthesis error when I followed the format of Oracle referencing in other tables
Create table TourOperator (TOID int PRIMARY KEY, Cname varchar (20), phone long int);
Create table Airline(Aname varchar(20) PRIMARY KEY, Website varchar(255), Phone int(10), TOID int, FOREIGN KEY(TOID) REFERENCES TourOperator);
You have a few issues in your SQL, according to the Oracle syntax:
The type INT cannot be qualified with a size.
The type VARCHAR technically does exist in Oracle, but is not recommended; use VARCHAR2 instead.
The syntax of the foreign key clause is wrong.
Avoid the type LONG; it's a legacy type that you shouldn't use for new databases. I changed the phone column to VARCHAR2.
The SQL can run as:
Create table TourOperator (
TOID int PRIMARY KEY,
Cname varchar2(20),
phone varchar2(20)
);
Create table Airline(
Aname varchar2(20) PRIMARY KEY,
Website varchar2(255),
Phone int,
TOID int REFERENCES TourOperator (TOID)
);
See running example at db<>fiddle.
You need to specify the target column of the foreign key in the TourOperator table. Try this:
Create table TourOperator
(TOID int PRIMARY KEY,
Cname varchar2(20),
phone long int
);
Create table Airline
(Aname varchar2(20) PRIMARY KEY,
Website varchar2(255),
Phone int,
TOID int,
FOREIGN KEY(TOID) REFERENCES TourOperator(TOID)
);
For more examples, see here: https://www.oracletutorial.com/oracle-basics/oracle-foreign-key/
I am new in Posgresql. I have 5 tables and I am trying to INSERT properties to tables. When I tried to Insert 2nd time, I have this error in 'pgadmin'.
ERROR: insert or update on table "question" violates foreign key constraint "question_id_difficulty_fkey" DETAIL: Key (id_difficulty)=(9) is not present in table "difficulty". SQL state: 23503.
my schema is here
id SERIAL PRIMARY KEY,
name varchar
);
CREATE TABLE question (
id SERIAL PRIMARY KEY,
text varchar,
correct_answer varchar,
incorrect_answer1 varchar,
incorrect_answer2 varchar,
incorrect_answer3 varchar,
id_difficulty SERIAL REFERENCES difficulty(id),
id_category SERIAL REFERENCES category (id),
id_creator SERIAL REFERENCES game (id)
);
CREATE TABLE difficulty (
id SERIAL PRIMARY KEY,
name varchar
);
CREATE TABLE category (
id SERIAL PRIAMRY KEY,
name varchar
);
CREATE TABLE user (
id SERIAL PRIMARY KEY,
name varchar
)
You would need a corresponding entry in the difficulty table with an id of 9, so that a referencing id_difficulty column in the question table.
For example, if your difficulty table contained:
id | name
----+----------------
1 | easy
2 | reasonable
3 | difficult
4 | very difficult
5 | impossible
You could only set id_difficulty for rows in the question table to one of those id values. If you set 6, or 12 or anything other than 1 to 5, it would fail because the values are constrained by the values in the foreign key.
The id_difficulty, id_category and id_creator columns shouldn't be using serial, so these should have their defaults dropped:
ALTER TABLE question ALTER COLUMN id_difficulty DROP DEFAULT;
ALTER TABLE question ALTER COLUMN id_category DROP DEFAULT;
ALTER TABLE question ALTER COLUMN id_creator DROP DEFAULT;
Postgres now recommends using generated always as instead of serial. If you do this, then the types will align much more simply:
CREATE TABLE question (
id int generated always as identity PRIMARY KEY,
text varchar,
correct_answer varchar,
incorrect_answer1 varchar,
incorrect_answer2 varchar,
incorrect_answer3 varchar,
id_difficulty int REFERENCES difficulty(id),
id_category int REFERENCES category (id),
id_creator int REFERENCES game (id)
);
CREATE TABLE difficulty (
id int generated always as identity PRIMARY KEY,
name varchar
);
This makes what is happening much clearer. The data type for a foreign key reference needs to match the data type of the primary key. Postgres knows that serial is really int. But using generated always, it is obvious that they are the same.
In addition, generated always as is more consistent with standard SQL.
I am trying to make a simple database with an number generator but why do I get the error below?
ERROR: syntax error at or near "AUTO_INCREMENT"
LINE 2: IDNumber int NOT NULL AUTO_INCREMENT,
Code:
CREATE TABLE Finance
(
IDNumber int NOT NULL AUTO_INCREMENT,
FinName varchar(50) NOT NULL,
PRIMARY KEY(IDNumber)
);
The subject of the question mentions pgAdmin 4, so here's how to do it there.
First, add a column to your table, then click the little edit icon:
Then go to Constraints and select the Identity type:
This generates SQL similar to this:
CREATE TABLE public.my_table_name
(
id integer NOT NULL GENERATED ALWAYS AS IDENTITY,
PRIMARY KEY (id)
);
For Postgres you have to use SERIAL
CREATE TABLE Finance
(
IDNumber SERIAL PRIMARY KEY,
FinName varchar(50) NOT NULL
);
IN pgadmin-2.
step 01:
create seq:
and set info:
step 02: go to ID in table and set constraints
if it is sql the syntax is the following
CREATE TABLE Persons (
ID int IDENTITY(1,1) PRIMARY KEY,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Age int
);
You are using a MySQL syntax which won't work in SQL Server.
CREATE TABLE Finance
(
IDNumber int NOT NULL IDENTITY(1,1) PRIMARY KEY,
FinName varchar(50) NOT NULL
);
The following code should work for SQL Server.
IDENTITY(1,1) is SQL Server's way of saying "auto increment".
The starting value for IDENTITY is 1, and it will increment by 1 for each new record.
So : IDENTITY(startvalue, incrementvalue)
there are two options; one is to use the "datatype" serial or create a sequence and use this sequence as a default value for your integer as follows:
CREATE SEQUENCE your_seq;
CREATE TABLE foo(
id int default nextval('your_seq'::regclass),
other_column TEXT);
INSERT INTO foo(other_column) VALUES ('bar') RETURNING *;
It is important to note that specifying a table as follows:
CREATE TABLE tablename (
colname SERIAL);
is equivalent to:
CREATE SEQUENCE tablename_colname_seq AS integer;
CREATE TABLE tablename (
colname integer NOT NULL DEFAULT nextval('tablename_colname_seq'));
ALTER SEQUENCE tablename_colname_seq OWNED BY tablename.colname;
More information about serial can be found here.
I am trying to create a table and getting error as ora 00907 missing right parenthesis. What went wrong?
This is my query:
CREATE TABLE College (
ID int,
NAME varchar(255),
Branch var char(255) NOT NULL,
Percentage int,
Address varchar,
City varchar(255),
PRIMARY KEY (ID)
);
You have multiple errors. Presumably, you want something like this:
CREATE TABLE Colleges (
CollegeID int PRIMARY KEY,
Name varchar2(255),
Branch varchar2(255) NOT NULL,
Percentage int,
Address varchar2(255),
City varchar2(255)
);
Notes:
Your syntax problem is the space in var char.
Oracle recommends varchar2() over varchar.
You should always include a length in the definition.
You can inline the primary key definition.
I prefer that the primary key include the entity name, rather than the generic id.
Similarly, I prefer that the table be in the plural, because it contains multiple colleges.
A column called percentage with a type of int is suspicious.