DATABASE
DATABASE in SQL is a structured set of data. In other words, it is the main container that holds data in an organized way for storing, managing, and retrieval. A DATABASE in SQL consists of series of tables that store specific sets of information. It takes a major role in the functioning of an application by allowing the storage, update, and retrieval of data. A DATABASE follows the ACID principles (Atomicity, Consistency, Isolation, Durability) to ensure reliability.
Example
CREATE DATABASE MyDatabase;USE MyDatabase;CREATE TABLE MyTable(ID int, Name varchar(255));INSERT INTO MyTable VALUES (1, 'Name1');SELECT * FROM MyTable;
Output
+----+-------+| ID | Name |+----+-------+| 1 | Name1 |+----+-------+
Explanation
The example demonstrates the procedure of creating a database, using that database, and creating a table inside the database in MySQL. An entry is also inserted into the table and then the contents of the table are displayed.
Example
CREATE DATABASE test_db;
\c test_db;
CREATE TABLE test_table( ID INT PRIMARY KEY NOT NULL, NAME TEXT NOT NULL);
INSERT INTO test_table (ID, NAME) VALUES (1, 'John');
SELECT * FROM test_table;
Output
id | name----+------ 1 | John(1 row)
Explanation
In the example, first, a new database test_db
is created. Then, a connection is established to the new database. Next, a new table test_table
with columns ID
and NAME
is created in the test_db
database. Afterwards, an entry, where ID
is 1
and NAME
is 'John'
, is inserted into test_table
. Finally, using the SELECT
statement, all records from test_table
are retrieved, which currently only holds one entry. As a result, the output shows that single row with ID
equalling 1
and NAME
equalling 'John'
.
Example
CREATE DATABASE TestDB;
Output
-- Command(s) completed successfully.
Explanation
The above SQL code snippet creates a new database named TestDB
. When executed successfully, the SQL Server returns a message stating that the command(s) completed successfully.
Example
CREATE DATABASE sampleDB;
Output
Database created.
Explanation
In Oracle SQL, the CREATE DATABASE
statement is used to create a new database. In this case, a database named sampleDB
has been created.
Example
CREATE DATABASE example_db;
Output
Query OK, 1 row affected (0.00 sec)
Explanation
In this example, a command CREATE DATABASE
is used to create a new database called ‘example_db’. The command creates an entirely new database and an output message “Query OK, 1 row affected” confirms that the command executed successfully and a new database has been created.