Run .sql script for a particular database - sql

I'm using Postgres's official Docker image. I have a script under /docker-entrypoint-initdb.d/ that creates two different databases for me. I want to initialize the table schemas for a particular database using a .sql file. The .sql file is very simply and looks something like this:
CREATE TABLE foo (
id INT PRIMARY KEY,
data VARCHAR
)
CREATE TABLE bar (
id INT PRIMARY KEY,
data VARCHAR
)
What I would like this to do is something like this (I'm making up this syntax):
FOR database_a AS username
CREATE TABLE foo (
id INT PRIMARY KEY,
data VARCHAR
)
CREATE TABLE bar (
id INT PRIMARY KEY,
data VARCHAR
)
Is this possible? Seems like a common feature but I can't find any information on it.

Related

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

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

SSIS Unique column?

Hello and sorry for newbie question.
I have a very simple SSIS project that imports customer names from file. It all works now fine, however there are multiple entries of same name and I dont want duplicates.
This works just fine, however it populates duplicates:
CREATE TABLE [SLSales].[dbo].[Customer] (
id BIGINT IDENTITY NOT NULL PRIMARY KEY,
name NVARCHAR(100) NOT NULL
);
However, when I try to use this:
CREATE TABLE [SLSales].[dbo].[Customer] (
id BIGINT IDENTITY NOT NULL PRIMARY KEY,
name NVARCHAR(100) NOT NULL UNIQUE
);
All records fail and I get a mysterious -1071607685 error code.
The SSIS way is to:
Load the data from Source
GROUP BY [name] in your case because you can have same name in file
Run through a Lookup (Match and NoMatch outputs)
Insert No Match
Update matches (or in your case you might want to just ignore)

Liquibase ignorable comments

Is there a way we can add comments in liquibase file which are not parsed by the program?
We are using the text format for the changes.sql and this is how it looks
--changeset Sapan.Parikh:MyUniqueAlphaNumericId5
--comment: Table created for liquibase testing purpose with non numeric id
--6:10 PM 11/10/2015
create table liqui_test11 (
id int primary key,
name varchar(255)
);
create table liqui_test9 (
id int primary key,
name varchar(255)
);
create table liqui_test10 (
id int primary key,
name varchar(255)
);
Our organization has been using similar change log for years and while migrating to Liquibase we want to be able to do two things.
Add dashes or hashes after each changeset.
And after every production build we add a comment at the end of the changes file.
For instance
--changeset Sapan.Parikh:MyUniqueAlphaNumericId5
--comment: Table created for liquibase testing purpose with non numeric id
--6:10 PM 11/10/2015
create table liqui_test11 (
id int primary key,
name varchar(255)
);
-----------------------------------------------------------------
--changeset Sapan.Parikh:MyUniqueAlphaNumericId4
--comment: Table created for liquibase testing purpose with non numeric id
--6:10 PM 11/10/2015
create table liqui_test12 (
id int primary key,
name varchar(255)
);
###------------------Build 10.0.1 Made-------------------
Now if we add just dashes- or # the luqibase task is breaking and DB upgrade does not happen. Is there a way to accommodate comments which are not parsed by liquibase engine?
You can just persist your comments and strip them right before executing liquibase
- can be done easily using sed

How to model 'toggle' table in sql

I have a table with some columns
CREATE TABLE test (
testid INT,
field1 CHAR(10),
field2 VARCHAR(50),
field3 DATETIME,
field4 MEDIUMINT
[...]
);
Now I want to be able to have a setting in my app that will allow me to to either enable or disable some of those for particular users.
CREATE TABLE user (
userid INT
);
I was thinking about:
CREATE TABLE user_test_visible (
userid INT,
field1 BOOL,
field2 BOOL,
field3 BOOL,
field4 BOOL
[...]
);
Also I was thinking about something like this :
CREATE TABLE user_test_visible (
userid INT,
field_name VARCHAR(30),
visible BOOL);
Are any of those approaches sensible?
I would suggest do something like this maybe.
CREATE TABLE test
(
fieldId INT,
field CHAR(10)
)
To have one table that contains the fields. Then if you need to add one more (change of requirements) you do not have to add a new column.
The I would skip the boolean and go with one table that has a shared primary key. Like this:
CREATE TABLE user_test_visible (
userid INT,
fieldId INT
);
The reason why I would suggest skipping the boolean is that if there is no row do show the field. That depends on what your start value is. If you want the users to see all field from the begining then you might consider having the table like this:
CREATE TABLE user_test_not_visible (
userid INT,
fieldId INT
);
Then where there is a row in this table then do not show the filed.
Edit
When use insert the field you must have some pre deployment script right? There you can also specify which columns that are visible and which is not. If you have different data types then ether have the layout like you have or you can just a sql_variant. But beaver that this type of column is not supported in for example linq-to-sql as a primary key.
That is just my idés. Hope it helps
Perhaps a more flexible approach would be to define "roles" within your application. A user would be associated with one or more roles, and each role would be associated with a set of columns. The union of those column sets would be what a user can see. This approach will require more effort to work out what columns a user can see, but it would make user management easier in the long term. It also separates user privileges from what that means in terms of database access.

How to concatenate several create table sentences so the script runs everything with one execute command?

For example, say I have this SQL Script:
create table Person
(
id int primary key,
name nvarchar(40)
)
create table Country
(
id int primary key,
name nvarchar(40)
)
How can I have those commands in a single file and be able to run it?
Add GO after each create table statement.