SQL - create database and tables in one script
Put a GO
command between queries.
IF db_id('dbname') IS NULL
CREATE DATABASE dbname
GO
CREATE TABLE dbname.dbo.TABLE1 (
);
CREATE TABLE dbname.dbo.TABLEN (
);
As for putting the table statements in the IF, you wouldn't be able to because of the GO command. You could create additional IF statements afterwards, to check for each tables pre-existence.
The syntax for a block if is:
IF condition
BEGIN
....
....
END
Create SQL script that create database and tables
In SQL Server Management Studio you can right click on the database you want to replicate, and select "Script Database as" to have the tool create the appropriate SQL file to replicate that database on another server. You can repeat this process for each table you want to create, and then merge the files into a single SQL file. Don't forget to add a using statement after you create your Database but prior to any table creation.
In more recent versions of SQL Server you can get this in one file in SSMS.
- Right click a database.
- Tasks
- Generate Scripts
This will launch a wizard where you can script the entire database or just portions. There does not appear to be a T-SQL way of doing this.
Create database and table in a single .sql script
This worked for me:
CREATE DATABASE mydb;
GO
USE mydb;
GO
CREATE TABLE mydb.dbo.Customers
(
ID INT ,
FirstName VARCHAR(255) ,
LastName VARCHAR(255),
);
Create multiple tables using single .sql script file
You need to separate the create table scripts with /
or end the command with ;
, Try like this,
CREATE TABLE ACCOUNT_DETAILS_TB ( CUSTOMER_ID VARCHAR2(20) NOT NULL , ACCOUNT_ID VARCHAR2(20) NOT NULL )
/
CREATE TABLE ADDRESS_DETAILS_TB ( ACCOUNT_ID VARCHAR2(20) NOT NULL , ADDRESS_ID VARCHAR2(20) NOT NULL )
/
OR
CREATE TABLE ACCOUNT_DETAILS_TB ( CUSTOMER_ID VARCHAR2(20) NOT NULL , ACCOUNT_ID VARCHAR2(20) NOT NULL );
CREATE TABLE ADDRESS_DETAILS_TB ( ACCOUNT_ID VARCHAR2(20) NOT NULL , ADDRESS_ID VARCHAR2(20) NOT NULL );
Create database, tables etc in one script postgresql
Replace the T-SQL batch terminator GO
with the PostgreSQL batch terminator ;
.GO
is not supported in postgreSQL
- Microsoft SQL Server to PostgreSQL Migration by Ian Harding
you need to connect on the database using \
. eg,
CREATE DATABASE testdatabase;
\c testdatabase
CREATE TABLE testtable (testcolumn int);
- PostgreSQL for MySQL users
Is it possible to create a single sql script to both create a database and to populate it with tables?
With a bash script like this:
#!/bin/bash
psqluser="U" # username
psqlpass="U" # password
psqldb="U" # db
sudo printf "CREATE USER $psqluser WITH PASSWORD '$psqlpass';\nCREATE DATABASE $psqldb WITH OWNER $psqluser;\nGRANT ALL PRIVILEGES ON database $psqldb TO $psqluser;" > createdb.sql
sudo -u postgres psql -f createdb.sql
echo "Populating inserting.sql"
sudo -u postgres psql -d $psqldb -f inserting.sql
where inserting.sql is something like:
CREATE TABLE T (
id serial,
description text
);
ALTER TABLE T OWNER TO U;
INSERT INTO T(description) VALUES ('description 1');
INSERT INTO T(description) VALUES ('description 2');
ALTER TABLE ONLY T ADD CONSTRAINT id_pkey PRIMARY KEY (id);
Related Topics
Attaching an Mdf File Without Ldf File
How to Determine If Null Is Contained in an Array in Postgres
Is Array All Nulls in Postgresql
Update Multiple Rows Using Select Statement
Database Restore Failing with Move
Sql Server: Best Way to Concatenate Multiple Columns
Querying Multiple Tables in Big Query
Sql Query Not Between Two Dates
How to Split a String into Variables in Sql
Sql Server Creating a Temp Table for This Query
What Is The Correct Case Select Statement in Access 2010
Get All The Rows Referencing (Via Foreign Keys) a Particular Row in a Table
How to Get Id of Newly Inserted Record Using Excel Vba
Select The Rows That Just Inserted
Extract Email Address from String Using Tsql
How to Deal with Spark Udf Input/Output of Primitive Nullable Type
How to Use Oracle Outer Join with a Filter Where Clause
Sql 2008 Vs 2012 Error: Incorrect Syntax Near The Keyword 'Compute'