Example 1: create database mysql
CREATE DATABASE `mydb`;
CREATE TABLE `my_table`
(
my_table_id INT AUTO_INCREMENT,
my_table_name VARCHAR(30) NOT NULL,
my_foreign_key INT NOT NULL,
my_tb_created TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
my_tb_updated TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, ,
# Any other properties here
PRIMARY KEY(my_table_id),
CONSTRAINT fk_name_of_parent_table
FOREIGN KEY(my_foreign_key) REFERENCES parent_table(parent_table_column)
);
SHOW DATABASES;
Example 2: create database in mysql
The CREATE DATABASE statement is used to create a new SQL/MySQL database.
Syntax
CREATE DATABASE databasename;
CREATE DATABASE Example
The following SQL statement creates a database called "testDB":
Example
CREATE DATABASE testDB;
Example 3: create database sql
CREATE TABLE example (
id INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
username VARCHAR(50) NOT NULL UNIQUE,
password VARCHAR(255) NOT NULL,
created_at DATETIME DEFAULT CURRENT_TIMESTAMP
);
Example 4: how to create a sql database
CREATE table songs (
id INTEGER PRIMARY KEY AUTOINCREMENT,
title TEXT,
artist TEXT,
mood TEXT,
duration INTEGER,
released INTEGER);
INSERT INTO songs (title, artist, mood, duration, released)
VALUES ("Bohemian Rhapsody", "Queen", "epic", 60, 1975);
INSERT INTO songs (title, artist, mood, duration, released)
VALUES ("Let it go", "Idina Menzel", "epic", 227, 2013);
INSERT INTO songs (title, artist, mood, duration, released)
VALUES ("I will survive", "Gloria Gaynor", "epic", 198, 1978);
INSERT INTO songs (title, artist, mood, duration, released)
VALUES ("Twist and Shout", "The Beatles", "happy", 152, 1963);
INSERT INTO songs (title, artist, mood, duration, released)
VALUES ("La Bamba", "Ritchie Valens", "happy", 166, 1958);
INSERT INTO songs (title, artist, mood, duration, released)
VALUES ("I will always love you", "Whitney Houston", "epic", 273, 1992);
INSERT INTO songs (title, artist, mood, duration, released)
VALUES ("Sweet Caroline", "Neil Diamond", "happy", 201, 1969);
INSERT INTO songs (title, artist, mood, duration, released)
VALUES ("Call me maybe", "Carly Rae Jepsen", "happy", 193, 2011);
SELECT * FROM songs;
Example 5: MySQL CREATE DATABASE
MySQL implements a database as a directory that contains all files which correspond to tables in the database.
To create a new database in MySQL, you use the CREATE DATABASE statement with the following syntax:
CREATE DATABASE [IF NOT EXISTS] database_name
[CHARACTER SET charset_name]
[COLLATE collation_name]
Example 6: sql create database
Creates a new database.
Example: Creates a new database named ‘websitesetup’.
CREATE DATABASE websitesetup;