ORA-00904: : invalid identifier - very simple table - sql

Complete noob, only just figured what a sequence is.
CREATE SEQUENCE volunteer_id_seq
START WITH 1;
CREATE TABLE volunteer (
volunteer_id VARCHAR(20) PRIMARY KEY,
title VARCHAR2(5),
);
Returns 4 0.00 CREATE TABLE volunteer_t ( volunteer_id VARCHAR(20) PRIMA ORA-00904: : invalid identifier -

As Gordon Linoff states in his comment, you have a comma after you declare the second column in your table. Instead, it should look like this:
CREATE TABLE volunteer (
volunteer_id VARCHAR(20) PRIMARY KEY,
title VARCHAR2(5)
);

Related

SQL keeps throwing "missing left parenthesis"

I don't understand what I am doing here but I keep getting a "missing left parenthesis" error in the following SQL code. Can anyone please tell me what I am doing wrong? Newbie here.
CREATE TABLE AGENT_INFO
(AgentID SMALLINT PRIMARY KEY, AgentName VARCHAR, PhoneNumber VARCHAR);
The VARCHAR data type needs a size (and should probably be VARCHAR2 in Oracle and SMALLINT is an alias for NUMBER(38)):
CREATE TABLE AGENT_INFO(
AgentID SMALLINT PRIMARY KEY,
AgentName VARCHAR2(10),
PhoneNumber VARCHAR2(10)
);
If you want a string column that can accept text without specifying a maximum size then use CLOB:
CREATE TABLE AGENT_INFO(
AgentID NUMBER(38) PRIMARY KEY,
AgentName CLOB,
PhoneNumber CLOB
);
db<>fiddle here
varchar datatype requires size:
SQL> CREATE TABLE AGENT_INFO
2 (AgentID SMALLINT PRIMARY KEY,
3 AgentName VARCHAR2(20),
4 PhoneNumber VARCHAR2(20));
Table created.
SQL>
(Note that Oracle recommends varchar2 instead of varchar.)

SERIAL fields / Auto increment

The task indicated this:
Create a table named automagic with the following fields:
An id field is an auto-incrementing serial field.
A name field that allows up to 32 characters but no more This field is required.
A height field is a floating-point number that is required.
And my code was:
CREATE TABLE automatic (
id SERIAL PRIMARY KEY NOT NULL,
name CHAR (32),
height numeric);
However, the self-rater answered this Expecting an INSERT without a height to fail, it did not fail.
After that, I tried this
CREATE TABLE automatic (
id int not null auto_increment primary key,
name varchar(32) not null,
height float not null);
And got
ERROR: syntax error at or near "auto_increment"
LINE 1: CREATE TABLE automagic (id int not null auto_increment prima...\
SQL state: 42601
Character: 41
You can try something like this:
CREATE TABLE automagic (
id int NOT NULL,
name varchar(32) NOT NULL,
height float NOT NULL
);
CREATE SEQUENCE automagic_id_seq
START WITH 1
INCREMENT BY 1
NO MINVALUE
NO MAXVALUE
CACHE 1;
ALTER SEQUENCE automagic_id_seq OWNED BY automagic.id;
ALTER TABLE ONLY automagic ALTER COLUMN id SET DEFAULT nextval('automagic_id_seq'::regclass);
EDIT:
As pointed out by #a_horse_with_no_name, this is better:
CREATE TABLE automagic (
id int GENERATED ALWAYS AS IDENTITY,
name varchar(32) NOT NULL,
height float NOT NULL
);
maybe try this:
CREATE TABLE automagic (
id SERIAL,
name VARCHAR(32) NOT NULL,
height FLOAT NOT NULL)
Use primary key.
CREATE TABLE automagic (
id SERIAL primary key,
name VARCHAR(32) NOT NULL,
height FLOAT NOT NULL)

SQL auto increment pgadmin 4

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.

Creating a table and getting error as ora 00907 missing right parenthesis

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.

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);