4D v16

CREATE DATABASE

Home

 
4D v16
CREATE DATABASE

CREATE DATABASE  


 

 

CREATE DATABASE [IF NOT EXISTS] DATAFILE <Complete pathname>

The CREATE DATABASE command lets you create a new external database (.4db and .4dd files) at a specific location.

If the IF NOT EXISTS constraint is passed, the database is not created and no error is generated if a database with the same name already exists at the location specified.
If the IF NOT EXISTS constraint is not passed, the database is not created and the "Database already exists. Failed to execute CREATE DATABASE command." error message is displayed if a database with the same name already exists at the location specified. 

The DATAFILE clause lets you specify the complete name (complete pathname + name) of the new external database. You must pass the name of the structure file. The program automatically adds the ".4db" extension to the file if it is not already specified and creates the data file. The pathname can be expressed either in POSIX syntax or in the system syntax. It can be absolute or relative to the structure file of the main 4D database. 

  • POSIX syntax (URL type): folder names are separated by a slash ("/"), regardless of the platform that you use, for example: ".../extdatabases/myDB.4db"
    For an absolute path, pass in first position the volume name and a colon, for example: "C:/test/extdatabases/myDB.4db"
  • system syntax: pathname respecting the syntax of the current platform, for example:
    • (Mac OS) Disque:Applications:monserv:extdatabases:mabase.4db
    • (Windows) C:\Applications\myserv\extdatabases\myDB.4db

After successful execution of the CREATE DATABASE command, the new database created does not automatically become the current database. To do this, you must explicitly declare it as the current database using the USE DATABASE command.

An external database is a 4D database that is independent from the main 4D database, but that you can work with from the main 4D database by using the SQL engine of 4D. Using an external database means temporarily designating this database as the current database, in other words, as the target database for the SQL queries executed by 4D. By default, the main database is the current database.

You can create an external database directly from the main database with the CREATE DATABASE command. Once created, an external database can be designated as the current database using the USE DATABASE command. It can then be modified via standard SQL commands (CREATE TABLE, ALTER TABLE, etc.) and you can store data in it. The DATABASE_PATH function can be used to find out the current database at any time.
The main interest of external databases resides in the fact that they can be created and worked with via 4D components. This allows the development of components that are capable of creating tables and fields according to their needs.

Note: An external database is a standard 4D database. It can be opened and worked with as the main database by a 4D or 4D Server application. Conversely, any standard 4D database can be used as an external database. However, it is imperative that you do not activate the access management system (by assigning a password to the Designer) in an external database, otherwise you will no longer be able to have access to it via the USE DATABASE command.

Creation of ExternalDB.4DB and ExternalDB.4DD external database files at the location C:/MyDatabase/:

 Begin SQL
    CREATE DATABASE IF NOT EXISTS DATAFILE 'C:/MyDatabase/ExternalDB';
 End SQL

Creation of TestDB.4DB and TestDB.4DD external database files next to the structure file of the main database:

 Begin SQL
    CREATE DATABASE IF NOT EXISTS DATAFILE 'TestDB';
 End SQL

Creation of External.4DB and External.4DD external database files at the location specified by the user:

 C_TEXT($path)
 $path:=Select folder("Destination folder of external database:")
 $path:=$path+"External"
 Begin SQL
    CREATE DATABASE DATAFILE <<$path>>;
 End SQL



See also 

DATABASE_PATH
USE DATABASE

 
PROPERTIES 

Product: 4D
Theme: SQL Commands

 
HISTORY 

 
ARTICLE USAGE

4D SQL Reference ( 4D v16)