I created a table visually in DB2 Control Center. Is there a way or command in DB2 to show the SQL source for the create-table, after it's created?
There is a DB2-tool called db2look (command line) that can generate the DDL scripts for the creation of all the database objects (tables, views, constraints, ...). Have a look at the command options to let it create the DDLs of just 1 table and its constraints.
db2look -d YourDatabaseName -e -t YourTableName -o YourTableName.sql
Related
I've used mysql shell for creating database (create database testdb), why can't i make a database in sql*plus command line. I also want to see the lists of database but i can't find queries anywhere. Please guide
SQL> create database testdb;
create database testdb
*
ERROR at line 1:
ORA-01501: CREATE DATABASE failed
ORA-01100: database already mounted
SQL>
I am running a test.sql file from command prompt by connecting to the database named DB. The test.sql file contains DDL, DML statments in it.
The Schemas named A and B are used in the test.sql file.
First connection to the schema connect&&schema A was made. The DDL, DML statements for schema A got executed.
Next connect&&schema B was made in that same test.sql file. So the script at run time asks for the value of schema name B. After the connection to schema B then the DDL, DML statements for schema B will be executed.
Please suggest if I can connect automatically to schema B without entering the value of schema B name at run time when running the test.sql file.
So what I want to do here is to run a script while connected to a database I already had using pgAdmin3. The script contains a create role, tablespace, database and a create schema and several tables under that schema.
The problem here is that when I run the script it creates the new role, tablespace and database correctly. It also creates the schema and the tables correctly but with a problem, the schema is created under the database, from which I ran the script, instead of the newly created database. The script is more or less like this.
CREATE ROLE "new_role" ... ;
CREATE TABLESPACE "new_space"
OWNER "new_role"
LOCATION '/home/...';
CREATE DATABASE "new_db"
WITH OWNER = "new_role"
TABLESPACE = "new_space";
CREATE SCHEMA "schema" AUTHORIZATION "new_role" ;
CREATE TABLE IF NOT EXISTS "schema"."new_table"(
...
) TABLESPACE "new_space";...
...
I already saw a solution with a \connect foo; but that is not what I wanted, I wanted it to somehow connect within the script without running things separately and running \connect foo in the terminal.
Can anyone tell me if there is anyway to do this and help me come out with a solution to this problem?
Use psql and split it up into two scripts . You can save the scripts in .sql files, and then run psql to connect to the DB you want to run each script against all on the same command line (with && in between each command). The two psql commands could be combined into one bash script so it's only one command that you need to run.
Something like this, if the script were named foo.sql:
psql -X -h <host> -U <user> -p <port> -f foo.sql <db_name>
The first script could have the create role, create tablespace and create database commands, connecting to the postgres db or a template DB, and the second script could have the rest of the commands.
You could also use createdb from the bash script instead of CREATE DATABASE.
Using pgAdminIV:
1- right click on default database "postgres"
2- select create database, give a name f.e. "newdatabase"
3- click on "newdatabase" (to establish connection)
4- open the query tool
5- import, write or paste your code
6- run your code f.e.: CREATE SCHEMA newschema;
It works for me...
Hi there if anyone can help me, I have a .sh script that executes 4 .sql scripts, each executing against a schema. Currently the schema name is hardcoded but i want to make it configurable.
Given the following below how will i pass the arguments from the shell script to the .files?
an e.g call to a .sql is done in my shell script is done so like the following
ECHO “DELETING SCHEME….”
psql -f $SCRIPT_DIR/delete_data.sql my_db postgres
ECHO “DATABASE SCHEMA DELETED..”
delete_data.sql
drop schema my_schema cascade;
create schema my_schema;
You could replace the my_schema part with a placeholder, like %SCHEMA%:
drop schema %SCHEMA% cascade;
create schema %SCHEMA%;
We then run a substitution using sed, and pipe the results into psql (reading from stdin is equivalent to reading from file):
sed "s/%SCHEMA%/$schemaName/" $SCRIPT_DIR/delete_data.sql | psql powa_aim_db postgres
You can do this using a heredoc for your SQL:
my_schema="$1"
ECHO “DELETING SCHEME….”
psql <<SQL
drop schema $my_schema cascade
create schema $my_schema
SQL
ECHO “DATABASE SCHEMA DELETED..”
Then call your script with the schema name as the first argument:
$ ./my_script my_schema_name
I'm searching for a simple way to delete all data from a database and keep the structure (table, relationship, etc...).
I using postgreSQL but I think, if there a command to do that, it's not specific to postgres.
Thanks,
Damien
Dump the schema using pg_dump. drop the database, recreate it and load the schema.
Dump you database schema (the -s tag) to a file:
pg_dump -s -f db.dump DB-NAME
Delete the database:
dropdb DB-NAME
Recreate it:
createdb DB-NAME
Restore the schema only:
pg_restore db.dump > psql DB-NAME
This should work on PostgreSQL; Other DBMS might have their own tools for that. I do no know of any generic tool to do it.
EDIT:
Following comments, you might want to skip the dropdb command, and simply create another database with the dumped schema. If all went through well, you can drop the old database:
pg_dump -s -f db.dump DB-NAME
createdb DB-NEW-NAME
pg_restore db.dump > psql DB-NEW-NAME
At this point, you have the full database at DB-NAME, and an empty schema at DB-NEW-NAME. after you're sure everything is OK, use dropdb DB-NAME.
You can do something like this:
export PGUSER=your_pg_user
export PGHOST=database.host
export PGPORT=port
export PGDATABASE=your_database
psql -qAtX -c "select 'TRUNCATE table ' || quote_ident(table_schema) || '.' || quote_ident(table_name) || ' CASCADE;' from information_schema.tables where table_type = 'BASE TABLE' and not table_schema ~ '^(information_schema|pg_.*)$'" | psql -qAtX
It will do what's necessary.
Of course these exports are not necessary, but they will make it simpler to run 2 copies of psql without having to givem them all standard -U, -d, and so on, switches.
One thing though - using TRUNCATE to do so, while faster than DELETE, has it's drowbacks - for example - it is not being replicated by Slony and any other replication system that works on triggers. Unless you are working on PostgreSQL 8.4, and your replication knows how to use triggers on TRUNCATE.
I'm not a Postgres guy, but one option would be to iterate through the tables and issue a Truncate command against each one. You'll have to take otable relationships into account, though - you will not be able to delete reference data before data that refers to it, for example.
In pgAdmin you can do:
Right-click database -> backup, select "Schema only"
Drop the database
Create a new database and name it like the former
Right-click the new database -> restore -> select the backup, select "Schema only"
your can delete all records of your database without restriction of foreign keys by following three steps
Take script of your Database
Right Click on your database (your DB Name)
click on task and then "Generate script"
Specify location
Delete your database base
recreate a database with the same name and run you generated script
This way you can empty all of your database