How to create table in oracle? - sql

** I've created table in oracle but getting following error:
ORA-00903: invalid table name
**
CREATE TABLE user
(id INT NOTNULL AUTO_INCREMENT,
name VARCHAR(45) NOTNULL,
email VARCHAR(45) NOTNULL,
password VARCHAR(45) NOTNULL,
PRIMARY KEY(id));

A few things to correct here
USER is reserved
NOTNULL
primary definition
autoincrment
varchar2 is preferred over varchar
but you'll end up with something like
SQL> CREATE TABLE users
2 (id INT generated as identity NOT NULL ,
3 name VARCHAR2(45) NOT NULL,
4 email VARCHAR2(45) NOT NULL,
5 password VARCHAR2(45) NOT NULL,
6 constraint users_pk PRIMARY KEY(id)
7 );
Table created.
Also checkout quicksql.oracle.com which lets you type in metadata and it generates the table definitions for you. A great way to get started

Related

Error in create table T-SQL "Incorrect syntax near ','."

When create table have error "Incorrect syntax near ','."
I cant see this error in code. Please point out this error.
CREATE TABLE books(
id INT NOT NULL IDENTITY PRIMARY KEY,
author VARCHAR(150) NOT null,
date DATETIME NOT null,
city VARCHAR(50) NOT null,
publishing VARCHAR(50) NOT null,
udc INT NOT null,
quantity INT NOT null,
inventory_numbers INT NOT NULL PRIMARY KEY)
CREATE TABLE systematic_catalog(
id INT NOT NULL IDENTITY PRIMARY KEY,
udc_id INT FOREIGN KEY REFERENCES books(udc),
knowledge_area VARCHAR)
CREATE TABLE issued_books(
date_issued DATETIME,
inventory_numbers_id INT FOREIGN KEY REFERENCES books(inventory_numbers))
CREATE TABLE readers(
id INT NOT NULL IDENTITY PRIMARY KEY,
last_name VARCHAR CONSTRAINT,
first_name VARCHAR CONSTRAINT,
middle_name VARCHAR,
phone_number INT(11),
address VARCHAR,
ticket_number INT CONSTRAINT,
date_registration DATETIME,
date_reregistratiom DATETIME,
issued_books_id FOREIGN KEY REFERENCES issued_books(inventory_numbers_id))
You cannot add multiple primary keys to the table, either composite ey (combination of two fields) or Unique constraint can be added instead.
CREATE TABLE books(
id INT NOT NULL IDENTITY PRIMARY KEY,
author VARCHAR(150) NOT null,
date DATETIME NOT null,
city VARCHAR(50) NOT null,
publishing VARCHAR(50) NOT null,
udc INT NOT null,
quantity INT NOT null,
inventory_numbers INT NOT NULL PRIMARY KEY)
this is an error, you cannot ass multiple primary keys. Instead you can add Unique constraint
Correction would be,
CREATE TABLE books(
id INT NOT NULL IDENTITY PRIMARY KEY,
author VARCHAR(150) NOT null,
date DATETIME NOT null,
city VARCHAR(50) NOT null,
publishing VARCHAR(50) NOT null,
udc INT NOT null Unique, --this is used to create the second table
quantity INT NOT null,
inventory_numbers INT NOT NULL Unique )
CREATE TABLE systematic_catalog(
id INT NOT NULL IDENTITY PRIMARY KEY,
udc_id INT FOREIGN KEY REFERENCES books(udc), --Referenced column needs to be unique
knowledge_area VARCHAR)
CREATE TABLE issued_books(
date_issued DATETIME,
inventory_numbers_id INT FOREIGN KEY REFERENCES books(inventory_numbers))
CREATE TABLE readers(
id INT NOT NULL IDENTITY PRIMARY KEY,
last_name VARCHAR (255),
first_name VARCHAR (255),
middle_name VARCHAR(255),
phone_number INT,
[address] VARCHAR(255),
ticket_number INT ,
date_registration DATETIME,
date_reregistratiom DATETIME)
You cannot use a foreign key of another table as a foreign key for some other table. You need to read more on table, Primary key constraints and foreign key referencing.
This cannot be done. you are trying to refer to the foreign key of issued_books table as a foreign key in readers table, which is wrong.
issued_books_id INT FOREIGN KEY REFERENCES issued_books(inventory_numbers_id))
I hope this explanation will give you a better understanding of errors. There were synatx errors as well as wrong use of the SQL table creation steps. I have added the corrected code for you. Feel free to contact any time.

Creation of new table fails with error message "Foreign key constraint is incorrectly formed"

I have 2 SQL statement as follows:
CREATE TABLE IF NOT EXISTS countries
(
country_id INT PRIMARY KEY NOT NULL AUTO_INCREMENT,
country varchar(45) NOT NULL
);
CREATE TABLE IF NOT EXISTS patients
(
p_id INT PRIMARY KEY NOT NULL AUTO_INCREMENT,
p_fname varchar(50) NOT NULL,
p_mname varchar(10) NULL,
p_lname varchar(50) NOT NULL,
age INT NOT NULL,
sex SET ('Male','Female'),
phone_num_mobile varchar(10) NULL,
phone_num_res varchar(7) NULL,
phone_num_office varchar(7) NULL,
email varchar(75) NULL,
addr_house varchar(10) NULL,
addr_street1 varchar(45) NOT NULL,
addr_street2 varchar(45) NULL,
addr_street3 varchar(45) NULL,
addr_city varchar(20) NOT NULL,
addr_country varchar(45) NOT NULL,
occupation varchar(20) NOT NULL,
married BOOLEAN NOT NULL,
FOREIGN KEY(addr_country) REFERENCES countries(country)
);
The first one executes successfully and the second one, which assigns a foreign key to the previous table, fails to execute with the message 'Foreign key constraint incorrectly formed'. I have also tried altering the second query's foreign key field(addr_country) to have the same name as that of the 'countries' table(country) but it were top no avail.
Can anyone please explain me what's going on and suggest me with a solution to this problem?
Thanks in advance.
A foreign key in MariaDB can only reference either a primary key in another table or a unique key. In this case, the countries table already has a primary key, so you can create a unique key on the country column.
Try putting a unique constraint on the country column in the countries table:
CREATE TABLE IF NOT EXISTS countries
(
country_id INT PRIMARY KEY NOT NULL AUTO_INCREMENT,
country varchar(45) NOT NULL
UNIQUE KEY(country)
);
Late edit: As the commentors mentioned, you might want to just reference the country_id primary key instead of the country name. Since no two countries in the world have the same name, either might work in practice.

conflicting NULL/NOT NULL declarations for column "id_bank_card" of table "bill"

I Got a table BILL which is associated to another table BANK_CARD as follows:
create table BILL (
id_bill BIGSERIAL not null,
id_bank_card BIGSERIAL null,
id_registred_user BIGSERIAL not null,
reference_number INT4 null,
purchase_date DATE null,
bill_status VARCHAR(50) null,
payment_method VARCHAR(50) null,
constraint PK_BILL primary key (id_bill)
);
create table BANK_CARD (
id_bank_card BIGSERIAL not null,
id_registred_user BIGSERIAL not null,
card_type VARCHAR(50) null,
card_number INT4 null,
expiring_date DATE null,
cipher INT4 null,
constraint PK_BANK_CARD primary key (id_bank_card)
);
The table BILL has a 0..1 association with the table BANK_CARD, and BANK_CARD has a 1..n association with table BILL.
But when i execute my sql script i get the following error:
conflicting NULL/NOT NULL declarations for column "id_bank_card" of table "bill"
Because the relationship BILL and BANK_CARD is 0..1 the foreign key id_bank_card can be null in the table bill, so i don't understand why i get this error.
Any help please?
Thanks.
You are confusing data type definitions for primary and foreign keys. A bigserial is a generator of values of the bigint type. The foreign keys should use that data type. See table definitions below. Also, use of the NULL clause is redundant as that is the default behaviour. Primary keys can not be NULL so NOT NULL there is also redundant.
create table bank_card (
id_bank_card bigserial,
id_registred_user bigint references <user table>,
card_type VARCHAR(50),
card_number INT4,
expiring_date DATE,
cipher INT4,
constraint PK_BANK_CARD primary key (id_bank_card)
);
create table bill (
id_bill BIGSERIAL,
id_bank_card bigint references bank_card,
id_registred_user bigint references <user table>,
reference_number INT4,
purchase_date DATE,
bill_status VARCHAR(50),
payment_method VARCHAR(50),
constraint pk_bill primary key (id_bill)
);
If you are using Java+PostgreSQL:
this can happen if you accidentially insert null strings into your statement.
String N = System.getProperty("line.seperator");
Returned null, screwing up my sql statement.
FIXES: (Choose One)
String N = System.lineSeparator();
String N = "\n";
Exact string as it appeared in my code:
String CT =(""
//:+"---------10--------20--------30-------39
//:+"0123456789012345678901234567890123456789
+"CREATE TABLE t_1( "+N//:0
+"id SERIAL PRIMARY KEY "+N//:30
+" ,c_1 INT "+N//:60
+" ,c_2 VARCHAR (255) "+N//:90
+" ,c_3 CHARACTER( 8 ) "+N//:120
+" ,c_4 BOOLEAN "+N//:150
+" ,c_5 BYTEA "+N//:180
+" ,c_6 DATE "+N//:210
+"); " //:240
);;
Exact Error Message when N==null instead of "\n":
org.postgresql.util.PSQLException:
ERROR: conflicting NULL/NOT NULL declarations
for column "nullid" of table "t_1"
Stack: Java+Heroku+Tomcat9+PostGreSQL 9.5.13

Creating a users table in SQL

I'm trying to create a Users table:
CREATE TABLE users
( user_id int(5) PRIMARY KEY,
username varchar(25) NOT NULL,
password varchar(30) NOT NULL
);
But I keep getting this error:
Error starting at line 1 in command:
CREATE TABLE users ( user_id int(5) PRIMARY KEY, username varchar(25) NOT NULL, password varchar(30) NOT NULL )
Error at Command Line:2 Column:13
Error report:
SQL Error: ORA-00907: missing right parenthesis
00907. 00000 - "missing right parenthesis"
*Cause:
*Action:
Any ideas?
don't try to put precision for integer type:
CREATE TABLE users
( user_id int PRIMARY KEY,
username varchar(25) NOT NULL,
password varchar(30) NOT NULL
);
Looks like it's related to your int(5) data type specification. See Oracle numerica data types.
Try something like:
CREATE TABLE users
( user_id NUMBER PRIMARY KEY,
username varchar(25) NOT NULL,
password varchar(30) NOT NULL
);
try
CREATE TABLE users
( user_id int PRIMARY KEY,
username varchar(25) NOT NULL,
password varchar(30) NOT NULL
);
The following works, but note that the PRIMARY KEY constraint is added at the end of the command
CREATE TABLE users(
user_id int NOT NULL,
username varchar(25) NOT NULL,
password varchar(30) NOT NULL,
PRIMARY KEY(user_id)
);
To create a table:
CREATE TABLE users
( user_id int(5) NOT NULL,
username varchar(25) NOT NULL,
password varchar(30) NOT NULL,
PRIMARY KEY(user_id)
);

Error with auto_increment while conneted to Postgres via psql and puTTY

I'm getting this error in puTTY. Not sure why, looks right to me ...
psql:pierre.sql:10: ERROR: syntax error at or near "AUTO_INCREMENT"
LINE 2: c_id INTEGER NOT NULL AUTO_INCREMENT PRIMARY KEY,
^
psql:pierre.sql:18: ERROR: syntax error at or near "AUTO_INCREMENT"
LINE 2: r_id INTEGER NOT NULL AUTO_INCREMENT PRIMARY KEY,
--DROP TABLE customer, reservation;
CREATE TABLE customer(
c_id INTEGER NOT NULL AUTO_INCREMENT PRIMARY KEY,
c_ref VARCHAR(30) NOT NULL,
f_name VARCHAR(30) NOT NULL,
l_name VARCHAR(30) NOT NULL,
address VARCHAR(100) NOT NULL,
email VARCHAR(100) NOT NULL,
phone VARCHAR(11) NOT NULL
);
CREATE TABLE reservation(
r_id INTEGER NOT NULL AUTO_INCREMENT PRIMARY KEY,
c_id VARCHAR(30) NOT NULL REFERENCES customer(c_id),
book_date DATE NOT NULL CHECK (book_date <= now()),
s_time DOUBLE NOT NULL,
e_time DOUBLE NOT NULL,
amount INTEGER NOT NULL
);
Any ideas why?
auto_increment looks like something you'd use with MySQL.
But, here, it seems you are using PostgreSQL.
According to the datatype serial section of the manual, postgresql's equivalent of auto_increment is serial or bigserial.
Quoting that page :
The data types serial and bigserial are not true types, but merely
a notational convenience for setting up unique identifier columns
(similar to the AUTO_INCREMENT property supported by some other databases).
In Postgres 10 or later consider an IDENTITY column:
CREATE TABLE customer(
c_id INTEGER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
...
(PRIMARY KEY also makes it NOT NULL automatically.)
Details:
Auto increment table column
In Postgres 9.6 or older consider a serial like Pascal already suggested.
Works in pg 10 or later, too, but IDENTITY is generally superior.