How do I create a postrgresql database using SQL's DDL on pgadmin4? - sql

I'm learning DDL to create and define an SQL database with Postgresql 10.
I have the something like the following SQL code in an .sql file, and I want to input it in psql or PgAdmin 4, just to test the syntax and see the database structure:
CREATE DATABASE database;
CREATE TYPE t_name AS
( first VARCHAR(30),
last VARCHAR(60)
);
CREATE TABLE telephone_m
( tnumber VARCHAR(15) NOT NULL UNIQUE
);
CREATE TABLE people
( curp CHAR(18) NOT NULL PRIMARY KEY,
pname t_name NOT NULL,
birth_date DATE NOT NULL,
telephone_m VARCHAR(15) REFERENCES telephone_m
);
CREATE TABLE clients
( curp CHAR(18) NOT NULL PRIMARY KEY,
cid SERIAL NOT NULL REFERENCES cards,
clocation VARCHAR(29)
) INHERITS (people);
CREATE TABLE cards
( cid BIGSERIAL NOT NULL PRIMARY KEY,
curp CHAR(18) NOT NULL REFERENCES clients,
trips SMALLINT,
distance NUMERIC,
points NUMERIC
);
CREATE TABLE drivers
( curp CHAR(18) NOT NULL PRIMARY KEY,
rfc CHAR(22) NOT NULL UNIQUE,
adress t_adress NOT NULL
) INHERITS (people);
I've tried in PgAdmin 4 making right-click on a new database -> CREATE Script, it opens Query Editor, I copy paste my code and execute it, but it returns:
ERROR: CREATE DATABASE cannot be executed from a function or multi-command string
SQL state: 25001
I've also tried using Query Tool directly from the PgAdmin tools menu with the same results.

The database is created just fine. But if you want to create object in the new DB, you have to connect to it. In any client, including pgAdmin4.
And you cannot run CREATE DATABASE inside of a transaction. Must be committed on it's own. Executing multiple commands at once is automatically wrapped into a single transaction in pgAdmin.
You have to execute CREATE DATABASE mydb; on its own (for instance by selecting only that line and pressing F5 while being connected to any DB, even the maintenance db "postgres". Then click on "Databases" in the object browser in the pgadmin4 main window / tab, hit F5 to refresh the view, click on the new DB, open up a new query tool with the flash icon (in a new window / tab) and execute the rest of your script there.
psql scripts manage by using the meta-command \c to connect to the new db after creating it, within the same session.
Asides:
"database" is no good name for a database.
CREATE TYPE AS (...), but just CREATE TABLE (...). No AS.
And you typically don't want to use the data type CHAR(18). See:
Any downsides of using data type "text" for storing strings?
Get sum of integers for UNIQUE ids
What is the overhead for varchar(n)?
Should I add an arbitrary length limit to VARCHAR columns?

There is the ; missing after the CREATE DATABASE database (and perhaps give the db a better name).

Related

ERROR: relation "schema.TableName_Id_seq" does not exist - when creating table in a new database

I'm having an issue where I used pgAdmin4's GUI to create a SQL table, and I want to use to generated CREATE TABLE script to create this same table in another database.
When I run the CREATE TABLE script generated by pgAdmin4 in my new database, I get the following error:
ERROR: relation "schema.TableName_Id_seq" does not exist
So, it appears that the issue is with my auto-incrementing id column that I created as type SERIAL.
The CREATE TABLE script as provided by pgAdmin4:
-- Table: myschema.TableName
-- DROP TABLE myschema."TableName";
CREATE TABLE myschema."TableName"
(
"Id" integer NOT NULL DEFAULT nextval('myschema."TableName_Id_seq"'::regclass),
/* Other columns here */
CONSTRAINT "TableName_pkey" PRIMARY KEY ("Id")
)
WITH (
OIDS = FALSE
)
TABLESPACE pg_default;
ALTER TABLE myschema."TableName"
OWNER to JoshuaSchlichting;
Why can't the CREATE TABLE script be used in another database? The relation "schema.TableName_Id_seq" didn't exist in the original database prior to be creating that table. What's happening that is different?
The DDL script provided by pgAdmin4 is not complete. When the table was created, there was an implicit creation of a sequence because of the SERIAL type being select for the Id column.
You can find this newly create sequence with pgAdmin4. To do this, go to
-> your server
-> your database
-> your schema
-> Sequences
-> Right click TableName_Id_seq
-> choose "Create script"
This reveals the script used to create this sequence. In this instance, the following was revealed:
-- SEQUENCE: myschema.TableName
-- DROP SEQUENCE myschema."TableName";
CREATE SEQUENCE myschema."TableName"
INCREMENT 1
START 1
MINVALUE 1
MAXVALUE 2147483647
CACHE 1;
The use of the CREATE SEQUENCE script can be avoided by changing the line of code used to create the Id column in the CREATE TABLE script. Example below:
original line:
"Id" integer NOT NULL DEFAULT nextval('myschema."TableName_Id_seq"'::regclass),
changed to: "Id" SERIAL NOT NULL,

Newly added table not working

I am working on DB2 9.7 database. I was using SquirrelSQL for GUI purpose. However ever since I applied an alter command to one of my tables , I started facing problems with the table, and any further select queries asked for "reorg" of the table. to overcome this, I renamed the old table and created new table. However the create query didn't execute properly in Squirrel ,and so downloaded DBViewer for my STS(Spring Source Tool Suite.)I executed the create table query from DBViewer, but the issue now is neither am I able to access the newly created table from my JAVA code , nor from Squirrel.
I am completely clueless as to what could be the problem. Has anyone got any idea?
Following is the Structure of my table:
CREATE TABLE DB2ADMIN.CERT
(
CERT_ID CHAR(36) NOT NULL,
CERT_CD CHAR(1) NOT NULL,
CERT_NBR CHAR(10) NOT NULL,
CERT_REQ_USR_CD_1 CHAR(10),
CERT_REQ_USR_CD_2 CHAR(10),
CERT_REQ_USR_CD_3 CHAR(10),
CERT_REQ_USR_CD_4 CHAR(10),
CERT_REQ_USR_TXT_1 VARCHAR(255),
CERT_REQ_USR_TXT_2 VARCHAR(255),
CERT_REQ_USR_TXT_3 VARCHAR(255),
CERT_REQ_USR_TXT_4 VARCHAR(255),
CERT_REQ_USR_TXT_5 VARCHAR(255),
CERT_REQ_USR_TXT_6 VARCHAR(255),
CERT_REQ_USR_TXT_7 VARCHAR(255),
CERT_REQ_USR_TXT_8 VARCHAR(255),
CERT_REQ_USR_TXT_9 VARCHAR(255),
CERT_REQ_USR_DT DATE,
LAST_MDF_USER_ID CHAR(25) NOT NULL,
LAST_MDF_ACY_TS TIMESTAMP(26,6) NOT NULL,
CONSTRAINT SQL111017085116710 PRIMARY KEY (CERT_ID)
);
In my alter query I changed the datatype of CERT_NBR form INTEGER to CHAR
using the command;
ALTER TABLE CERT ALTER COLUMN CERT_NBR SET DATA TYPE INTEGER
any further select queries asked for "reorg" of the table.
This is a common occurrence after altering tables in DB2. It should be trivially solved by calling:
reorg table table-name;
This is a command-line command, rather than an sql statement, but you can call it via SQL with the admin_cmd procedure:
call sysproc.admin_cmd('reorg table table-name');
I'm not sure why you are unable to access the new table. The error message should help you resolve this. Some possibilities:
You created it with a different username than the one you are trying to access it with.
You never committed after the create table statement.
Something went wrong and the table ended up in an inoperable state.

Use Oracle Apex to read csv line by line and validate data

Now, I installed Oracle database and Apex into my local server.
I also create a table
CREATE TABLE FAC_CODE_CPQLDN(
ID NUMBER(10),
CODE NVARCHAR2(100) NOT NULL,
NAME NVARCHAR2(1000),
DESCRIPTION BLOB,
PARENT_ID NUMBER(10),
CONSTRAINT FAC_CODE_CPQLDN_PK PRIMARY KEY (ID)
);
How can I use Oracle Apex to read csv file line by line, store into "list_value" to validate before inserting into database?
If your csv less than 50 columns ( I think it is your case)
Excel2Collection [Plug-in]
Else
Implement your own PL/SQL PROCEDURE PARSE_CSV

Stop double entry of data in database

I am using VS 2008 and SQL Server 2005. And the problem is that when I insert a new record which is string data. It continues on entering the same data which is already exiting in the table, again and again. But I want that where my insert query is running. I place the check there that it does not allow similar data in the table.
My scenario:
I have to decide on these two string columns: 'source' and 'destination'
If similar source and destination occur in any record we must stop we the entry on record.
Share the solution.
The easiest way to do it is by putting a 'UNIQUE constraint' on your database. Then, each time an SQL UPDATE or an SQL INSERT is executed, the database server would check the validity of the new SQL action and cancel it if it violates your data integrity constraing.
For example (copying from this SQL tutorial):
CREATE TABLE Persons
(
P_Id int NOT NULL UNIQUE,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Address varchar(255),
City varchar(255)
)
If you want to add a UNIQUE constraint on two columns, you could use such a statement:
CREATE TABLE Example
(Col1 int NOT NULL,
Col2 int NOT NULL,
UNIQUE (Col1, Col2)
)
Hope I helped!

"There is already an object named XXXX in the database" after creating New database

I have an SQL file which I am executing on an SQL Server instance which contains the schema for a database. The file creates a brand new database (as in, a database with this name does not exist on this server):
CREATE DATABASE PROJECT;
and begins to create a relation:
CREATE TABLE Courses (
CourseID INT NOT NULL PRIMARY KEY,
Name VARCHAR(64) NOT NULL UNIQUE,
Code CHAR(4) NOT NULL UNIQUE
);
...
and here is what SQL Server tells me right off the bat:
Msg 2714, Level 16, State 6, Line 3
There is already an object named 'Courses' in the database.
Any idea why SQL Server tells me that there already exists a relation by the name of Courses when clearly there isn't?
Thank you for your time.
Check the database if you are using PROJECT
CREATE DATABASE PROJECT
GO
USE PROJECT
GO
CREATE TABLE Courses
(
CourseID INT NOT NULL PRIMARY KEY,
Name VARCHAR(64) NOT NULL UNIQUE,
Code CHAR(4) NOT NULL UNIQUE
)
GO
You are likely missing USE PROJECT statement and therefore trying to create Courses table in the master database, not in the PROJECT database.
Long shot but try using:
Use project;
CREATE TABLE Courses....