x

SQL – CREATE DATABASE & TABLE

Prev     Next

Before creating the database and tables, let’s see what is a server, database and table, and then can proceed with creating them.


1. What is a database?

A database is an application which is used to store and retrieve the data. All the data are stored in the database objects called tables.

2. What is a table?

Table is a database object which actually stores the collection of related data. It consists of rows and columns.

3. What is a server?

A physical computer or a computer program that acts as a centralized resource for other computers in a network is called a server. A server serves all the computers which are connected in a network.     

SQL – Create database, table:

SQL – CREATE DATABASE:

  • A new SQL database can be created using “CREATE DATABASE” statement. All databases reside on a server. We can create many databases on a server.

Syntax:

CREATE DATABASE database_name;

Where database_name – Actual database name that you want to create.

Example:


  • When we execute below statement, a new database called STUDENT_DB is created in the server.
CREATE DATABASE STUDENT_DB;

SQL – CREATE TABLE:

  • A new SQL table can be created using “CREATE TABLE” statement. All tables reside in a database. We can create many tables in a database.

Syntax:

CREATE TABLE table_name
(column_name1 data_type(size),
column_name2 data_type(size),
column_name3 data_type(size),
etc…);

Example:

  • When we execute below statement, a new table called ‘student’ is created in the database.
CREATE TABLE student
(
Student_ID int,
Student_name varchar(255),
City varchar(255),
Marks int
);

How to check whether a table exists & the property or description of a table?

  • We can use below statement to check whether a table exists or not in a database and the property/description of the table.

Syntax:

DESC table_name;

Example:

DESC student;

Prev     Next



Like it? Please Spread the word!