SQL Tutorial

SQL Introduction SQL Aggregate Functions SQL Aliases SQL And SQL Any All SQL Avg SQL Between SQL Case SQL Comments SQL Count SQL Delete SQL Distinct SQL Exists SQL Groupby SQL Having SQL In SQL Insert_into SQL Is Not Null SQL Join SQL Full Outer Join SQL Inner Join SQL Left Join SQL Right Join SQL Self Join SQL Like SQL Min Max SQL NOT Operator SQL Null SQL Operators SQL OR operator SQL OrderBy SQL Select SQL Select Into SQL Top Limit Fetch SQL Store Procedures SQL Sum SQL Union SQL Update SQL Where SQL Wildcards

SQL Database

SQL Alter Table SQL Auto increment SQL BackupDB SQL Check SQL Constrains SQL Create View SQL CreateDB SQL CreateTable SQL Data types SQL Dates SQL DefaultConstrain SQL DropDB SQL DropTable SQL Foreign Key SQL Hosting SQL Index SQL injections SQL Not NULL SQL PrimaryKey SQL Unique SQL Views

SQL CREATE DATABASE Tutorial

Example Database

We will create a database named company for our examples:


    CREATE DATABASE company;
    

Creating the Database

To create the database, use the following SQL command:


    CREATE DATABASE company;
    

Result:

Command CREATE DATABASE company;
Result
  • A database named company is created.

Using the Database

To start using the newly created database, use the following SQL command:


    USE company;
    

Result:

Command USE company;
Result
  • The company database is now selected for use.

Creating a Table in the Database

Let's create a table named employees in the company database:


    CREATE TABLE employees (
        employee_id INT PRIMARY KEY,
        name VARCHAR(50) NOT NULL,
        position VARCHAR(50),
        salary DECIMAL(10, 2),
        age INT
    );
    

Result:

Command CREATE TABLE employees (employee_id INT PRIMARY KEY, name VARCHAR(50) NOT NULL, position VARCHAR(50), salary DECIMAL(10, 2), age INT);
Result
  • A table named employees is created in the company database.

Inserting Data into the Table

Let's insert some data into the employees table:


    INSERT INTO employees (employee_id, name, position, salary, age) VALUES
    (1, 'John Doe', 'Manager', 60000.00, 45),
    (2, 'Jane Smith', 'Developer', 55000.00, 30),
    (3, 'Emily Johnson', 'Designer', 50000.00, 25);
    

Result:

Command INSERT INTO employees (employee_id, name, position, salary, age) VALUES (1, 'John Doe', 'Manager', 60000.00, 45), (2, 'Jane Smith', 'Developer', 55000.00, 30), (3, 'Emily Johnson', 'Designer', 50000.00, 25);
Result
  • Three records are inserted into the employees table.

Querying the Table

We can query the employees table to see the data:


    SELECT * FROM employees;
    

Result:

Command SELECT * FROM employees;
Result
employee_id name position salary age
1 John Doe Manager 60000.00 45
2 Jane Smith Developer 55000.00 30
3 Emily Johnson Designer 50000.00 25