extract oracle database erd with sqlplus - database

i want to ask about extracting oracle ER Diagram (ERD) from sqlplus. i have a condition that i must access oracle database from my vps (this oracle database at another server). i need to use vps because with vps ,it will provide a domain that will be used for oracle database server to grant access to it database. what i know to generate ER Diagram is from oracle sql Developer .but with my current vps ,it provided CLI only. Unfortunatly sql developer using GUI so i cant use it. So how can i extract ER Diagram from oracle database witohout using sql developer and this solution must not using an app with GUI.

You can generate a plain text file containing the description for each of your tables, download this file from your remote machine to your local machine and load the content in an ERD tool such as Oracle DataModeler.
A common script to obtain a table description (including your goal, Foreign key constraints) is:
set heading off;
set echo off;
Set pages 1000;
set long 50000;
SET linesize 150;
spool My_ddl.sql;
select DBMS_METADATA.GET_DDL('TABLE','<your table 1>','<schema>') from DUAL;
select DBMS_METADATA.GET_DDL('TABLE','<your table 2>','<schema>') from DUAL;
select DBMS_METADATA.GET_DDL('TABLE','<your table 3>','<schema>') from DUAL;
.
.
.
spool off;
So basically you still are gonna use a GUI tool to build your diagram, but the trick resides in how you get your source scripts.
If you are worried about how to generate the get_ddl instruction for all of your 10,000 tables, fear not, as you can run the following:
SELECT 'SELECT DBMS_METADATA.GET_DDL(''TABLE'','''||table_name||''', ''<LOCAL/FOREIGN SCHEMA>'') FROM dual;' as get_ddls
FROM all_tables
WHERE owner = '<User of the foreign schema where tables are stored>';
and then just copy paste the results to the previous script.
In order to retrieve the file generated server side, you can always ask (kindly, of course) your DBA to hand it over to you, unless you have a way to access directly into the server directories/files.
Good luck.

Related

Same tables with same data in different sql server instance in different pc

I have one pc as main database server which all clients are logging to main table. I have another two pcs lying around and I want to use them as backup servers. These backup servers will have data from main table in main database server. I am not sure how to achieve such process and really appreciate the help. My database server is microsoft sql express edition and incoming data are from apis in aspnet core. Usually, I will use Microsoft SQL Management Studio and extract data tier from table and import data tier in another pc with same table name.
Main Database (Main PC) -> Second Backup Database (Second PC) and Third Backup Database (Third PC)
I have never done this before and I can't find the solution yet. I want to replicate table from Main PC in another two pc. Not replicate whole database in another pc.
I found that there is no replication feature in express edition. Any possible approach for this backup process?
As I said in my comment you are going in wrong direction.
First of all you said
I have another two pcs lying around and I want to use them as backup
servers.
Backup server does not mean "to replicate table from Main PC in another two pc. Not replicate whole database in another pc.", what can you do with the copy of 1 table if something happen to your main server?
Backup server should contain transactionally consistent copy of your database, only this way you can re-direct your applications to the backup server and they will be able to work with it in case of disaster with your main server. And this means you should backup your database on the main server and restore it on the backup server, backup/restore will provide you with transactionally consistent copy of database, and bacpac won't.
As you are on Express Edition and cannot use SQL Server Agent you can write 2 scripts to backup and restore and launch them using sqlcmd. To schedule it you can use Windows scheduler.
Your backup script can look like this:
backup database MyDB to disk = 'path-to-backup-file' with init;
And your restore script looks like this:
restore database MyDB from disk = 'path-to-backup-file'
with move 'MyDB' to 'db-copy-path\MyDB.mdf',
move 'MyDB_log' to 'db-copy-path\MyDB_log.ldf',
replace;
Your cmd command looks like this:
sqlcmd -S myServer\instanceName -i C:\myScript.sql –U login_name –P password
Here you pass your backup or restore command in the file myScript.sql
my source address is 10.11.20.181 and port is 5001
This means that for execute your backup script you should use the following:
sqlcmd -S 10.11.20.181,5001 -i C:\myBackupScript.sql –U login_name –P password
SQL Server doesn't allowed SQL Server agent also in Express edition.
CREATE the linked server on your destination database to connect primary database.
Schedule one Operating system scheduler to execute database script. In your database script you need to fetch new records from source database using linked server based on "Which are inserted or updated in last n minutes".
check those data in your tables using LEFT JOIN. If not exist the insert into the table.
For better performance, Insert fetched data into the temp table, then use below query.
INSERT INTO your_table()
SELECT t.*
FROM #temp t
LEFT JOIN your_table y ON t.id = y.id
WHERE y.id IS NULL
I tested this solution that can fulfill my requirement with minimum steps.
I copy powershell script from this link.
I also install sqlpackage from microsoft.
.\SqlPackage.exe /a:Export /ssn:ServerName /sdn:TableName/tf:path-to-backup-folder\mybackup$(get-date -f dd-MM-yyyy-HH-mm-s).bacpac
and I created task scheduler in my backup pc to execute this script every 6hrs. and I have another script to import this data back to database inside backup pc every 12hrs and delete those bacpac after import. One thing to consider using this method is how big is your database since I am exporting every data every six hours and if your database is huge, this would cause the performance issue & I don't know what will happen new rows are inserted or updated when executing this operation.
I am really not sure what kind of errors will occur in the long run.

how to mirror a whole database cluster in postgresql

I'm using a postgresql (9.6) database in my project which is currently in development stage.
For production I want to use an exact copy/mirror of the database-cluster with a slightly different name.
I am aware of the fact that I can make a backup and restore it under a different cluster-name, but is there something like a mirror function via the psql client or pgAdmin (v.4) that mirrors all my schemas and tables and puts it in a new clustername?
In PostgreSQL you can use any existing database (which needs to be idle in order for this to work) on the server as a template when you want to create a new database with that content. You can use the following SQL statement:
CREATE DATABASE newdb WITH TEMPLATE someDbName OWNER dbuser;
But you need to make sure no user is currently connected or using that database - otherwise you will get following error.
ERROR: source database "someDbName" is being accessed by other users
Hope that helped ;)

Database Generator php and mysql

How do I create a database generator to allows a user to create his own table in the database without access to PHPMyAdmin and without any emphasized text knowlege about PHP or MySQL?
You at-least need the sql file to run so that the database can be created....
create the sql file (the whole backup of the database along with subroutines)
just load the database via this command
mysql -u'root' -p'password' databaseName < /path/to/file.sql
In order to run mysql, it should be in your environment variable..

How can I use a SQL Scripts in a Database Project with the System.Data.SQLite data provider?

I've got a project where I'm attempting to use SQLite via System.Data.SQLite. In my attempts to keep the database under version-control, I went ahead and created a Database Project in my VS2008. Sounds fine, right?
I created my first table create script and tried to run it using right-click->Run on the script and I get this error message:
This operation is not supported for the provider or data source you are using.
Does anyone know if there's an automatic way to use scripts that are part of database project against SQLite databases referenced by the databases, using the provider supplied by the System.Data.SQLite install?
I've tried every variation I can think of in an attempt to get the script to run using the default Run or Run On... commands. Here's the script in it's most verbose and probably incorrect form:
USE Characters
GO
IF EXISTS (SELECT * FROM sysobjects WHERE type = 'U' AND name = 'Skills')
BEGIN
DROP Table Skills
END
GO
CREATE TABLE Skills
(
SkillID INTEGER PRIMARY KEY AUTOINCREMENT,
SkillName TEXT,
Description TEXT
)
GO
Please note, this is my first attempt at using a Database, and also the first time I've ever touched SQLite. In my attempts to get it to run, I've stripped any and everything out except for the CREATE TABLE command.
UPDATE: Ok, so as Robert Harvey points out below, this looks like an SQL Server stored procedure. I went into the Server Explorer and used my connection (from the Database project) to get do what he suggested regarding creating a table. I can generate SQL from to create the table and it comes out like thus:
CREATE TABLE [Skills] (
[SkillID] integer PRIMARY KEY NOT NULL,
[SkillName] text NOT NULL,
[Description] text NOT NULL
);
I can easily copy this and add it to the project (or add it to another project that handles the rest of my data-access), but is there anyway to automate this on build? I suppose, since SQLite is a single-file in this case that I could also keep the built database under version-control as well.
Thoughts? Best practices for this instance?
UPDATE: I'm thinking that, since I plan on using Fluent NHibernate, I may just use it's auto-persistence model to keep my database up-to-snuff and effectively in source control. Thoughts? Pitfalls? I think I'll have to keep initial population inserts in source-control separately, but it should work.
I built my database using an SQLite SQL script and then fed that into the sqlite3.exe console program like this.
c:\sqlite3.exe mydatabase.db < FileContainingSQLiteSQLCommands
John
Well, your script looks like a SQL Server stored procedure. SQLite most likely doesn't support this, because
It doesn't support stored procedures, and
It doesn't understand SQL Server T-SQL
SQL is actually a pseudo-standard. It differs between vendors and sometimes even between different versions of a product within the same vendor.
That said, I don't see any reason why you can't run any (SQLite compatible) SQL statement against the SQLite database by opening up connection and command objects, just like you would with SQL Server.
Since, however, you are new to databases and SQLite, here is how you should start. I assume you already have SQLite installed
Create a new Windows Application in Visual Studio 2008. The database application will be of no use to you.
Open the Server Explorer by pulling down the View menu and selecting Server Explorer.
Create a new connection by right-clicking on the Data Connections node in Server Explorer and clicking on Add New Connection...
Click the Change button
Select the SQLite provider
Give your database a file name.
Click OK.
A new Data Connection should appear in the Server Explorer. You can create your first table by right-clicking on the Tables node and selecting Add New Table.

How to version control SQL Server databases?

I have SQL Server databases and do changes in them. Some database tables have records that are starting records required my app to run. I would like to do version control over database and these records (rows). Is it possible to do this and bundle it to SVN version control I have for my source code or are there other solutions to this? I would like to accomplish this to be able to return to previous version of database and compare changes between database revisions. It would be nice if tools for this are free, open source or not very expensive.
My environment is Visual C# Express, SQL Server 2008 Express and Tortoise SVN.
Late answer but hopefully useful to other readers
I can suggest using the SSMS add-in called ApexSQL Source Control. By utilizing this add-in, developers can easily map database objects with the source control system via the wizard directly from SSMS. It includes support for Git, TFS, Mercurial, Subversion, TFS (including Visual Studio Online) and other Source Control systems. It also includes support for source controlling Static data (so you can version control records also).
After downloading and installing ApexSQL Source Control, simply right-click the database you want to version control and navigate to ApexSQL Source Control sub-menu in SSMS. Click the “Link database to source control” option and select the source control system and the database development model, for example:
After that, you may exclude objects you don’t want to be linked to source control. It is possible to exclude specific objects by owner or type.
On the next step, you will be prompted to provide the log-in information for the source control management system:
Once done, just click the “Finish” button and the “Action center” window will be shown, offering the objects that will be committed to the repository (this is by default, if the repository is empty).
Once the database has been linked to source control, all the operations that can be executed from a source control client will be available from the “Object Explorer” pane. Those include:
checking out with or without lock the versioned objects,
view history of that object and apply specific revision,
view changes on that object that were made and
place data from table to source control using the “Link static data”
You can read this article for more information: http://solutioncenter.apexsql.com/sql-source-control-reduce-database-development-time/
We've just started doing the following on some of our projects, and it seems to work quite well, for populating "static" tables.
Our scripts follow a pattern where a temp table is constructed, and is then populated with what we want the real table to resemble. We only put human readable values here (i.e. we don't include IDENTITY/GUID columns). The remainder of the script takes the temp table and performs appropriate INSERT/UPDATE/DELETE statements to make the real table resemble the temp table. When we have to change this "static" data, all we have to update is the population of the temp table. This means that DIFFing between versions works as expected, and rollback scripts are as simple as getting a previous version from source control.
The INSERT/UPDATE/DELETEs only have to be written once. In fact, our scripts are slightly more complicated, and have two sets of validation run before the actual DML statements. One set validate the temp table data (i.e. that we're not going to violate any constraints by attempting to make the database resemble the temp table). The other validate the temp table and the target database (i.e. that foreign keys are available).
Static data support is being added to SQL Source Control 2.0, currently available in beta. More information on how to try this can be found here:
http://www.red-gate.com/messageboard/viewtopic.php?t=12298
There is a free microsoft product called Database Publishing Wizard which you can use to script the entire database (schema and data). It is great for taking snapshots of the current state of a DB and will enable you to recreate from scratch at any point
For database (schema) versioning we use custom properties, which are added to the database when the installer is ran. The contents of these scripts is generated with our build scripts.
The script to set the properties looks like this:
DECLARE #AssemblyDescription sysname
SET #AssemblyDescription = N'DailyBuild_20090322.1'
DECLARE #AssemblyFileVersion sysname
SET #AssemblyFileVersion = N'0.9.3368.58294'
-- The extended properties DatabaseDescription and DatabaseFileVersion contain the
-- AssemblyDescription and AssemblyFileVersion of the build that was used for the
-- database script that creates the database structure.
--
-- The current value of these properties can be displayed with the following query:
-- SELECT * FROM sys.extended_properties
IF EXISTS (SELECT * FROM sys.extended_properties WHERE class_desc = 'DATABASE' AND name = N'DatabaseDescription')
BEGIN
EXEC sys.sp_updateextendedproperty #name = N'DatabaseDescription', #value = #AssemblyDescription
END
ELSE
BEGIN
EXEC sys.sp_addextendedproperty #name = N'DatabaseDescription', #value = #AssemblyDescription
END
IF EXISTS (SELECT * FROM sys.extended_properties WHERE class_desc = 'DATABASE' AND name = N'DatabaseFileVersion')
BEGIN
EXEC sys.sp_updateextendedproperty #name = N'DatabaseFileVersion', #value = #AssemblyFileVersion
END
ELSE
BEGIN
EXEC sys.sp_addextendedproperty #name = N'DatabaseFileVersion', #value = #AssemblyFileVersion
END
GO
You can get a version of SQL Management Studio for SQL Server Express. I believe you'll be able to use this to produce scripts of the schema of your database. I think that will leave you to create scripts by hand for inserting the starting records.
Then, put all the scripts into source control, along with a master script that runs the individual scripts in the correct order.
You'll be able to run diffs using windiff (free with Visual Studio SDK), or else Beyond Compare is inexpensive, and a great diff/merge/sync tool.
MS Visual Studio Team System for Database Developers has functionality to easily generate create scripts for the whole schema. Only drawback is the cost!
Have you considered using SubSonic?
You should rather use DB specific versioning.
http://msdn.microsoft.com/en-us/library/ms189050.aspx
When either the
READ_COMMITTED_SNAPSHOT or
ALLOW_SNAPSHOT_ISOLATION database
options are ON, logical copies
(versions) are maintained for all data
modifications performed in the
database. Every time a row is modified
by a specific transaction, the
instance of the Database Engine stores
a version of the previously committed
image of the row in tempdb. Each
version is marked with the transaction
sequence number of the transaction
that made the change. The versions of
modified rows are chained using a link
list. The newest row value is always
stored in the current database and
chained to the versioned rows stored
in tempdb.
I use bcp for this (bulk loading utility, part of a standard SQL Server install, Express edition included).
Each table with data needs a control file Table.ctl and a data file Table.csv (these are text files that can be generated from an existing database using bcp). As text files, these can very easily be versioned.
As part of my generation batches (see my answer there for more information), I iterate through every control file like this :
SET BASE_NAME=MyDatabaseName
SET CONNECT_STRING=.\SQLEXPRESS
FOR /R %%i IN (.) DO (
FOR %%j IN ("%%~fi\*.ctl") DO (
ECHO + %%~nj
bcp %BASE_NAME%..%%~nj in "%%~dpsj%%~nj.csv" -T -E -S %CONNECT_STRING% -f "%%~dpsj%%~nj.ctl" >"%TMP%\%%~nj.log"
IF %ERRORLEVEL% GTR 0 (
TYPE "%TMP%\%%~nj.log"
GOTO ERROR_USAGE
)
)
)
A current limitation of this script is that the name of the file must be the name of the table, which may not be possible if the table name contains specific special characters.
This project has a good example of deploy and rollback

Resources