SQL Query : Create database, Create Table
The SQL CREATE DATABASE Statement
The CREATE DATABASE statement is used to create a database.SQL CREATE DATABASE Syntax
CREATE DATABASE dbname;
SQL CREATE DATABASE Example
The following SQL statement creates a database called "my_db":
CREATE DATABASE my_db;
The SQL CREATE TABLE Statement
The CREATE TABLE statement is used to create a table in a database.Tables are organized into rows and columns; and each table must have a name.
SQL CREATE TABLE Syntax
CREATE TABLE table_name
(
column_name1 data_type(size),
column_name2 data_type(size),
column_name3 data_type(size),
....
);
(
column_name1 data_type(size),
column_name2 data_type(size),
column_name3 data_type(size),
....
);
The data_type parameter specifies what type of data the column can hold (e.g. varchar, integer, decimal, date, etc.).
The size parameter specifies the maximum length of the column of the table.
SQL CREATE TABLE Example
Now we want to create a table called "Persons" that contains five columns: PersonID, LastName, FirstName, Address, and City.We use the following CREATE TABLE statement:
Example
CREATE TABLE Persons
(
PersonID int,
LastName varchar(255),
FirstName varchar(255),
Address varchar(255),
City varchar(255)
);
(
PersonID int,
LastName varchar(255),
FirstName varchar(255),
Address varchar(255),
City varchar(255)
);
The LastName, FirstName, Address, and City columns are of type varchar and will hold characters, and the maximum length for these fields is 255 characters.
The empty "Persons" table will now look like this:
| PersonID | LastName | FirstName | Address | City |
|---|---|---|---|---|
No comments:
Post a Comment