Day 62 – SQL CREATE Keyword
SQL CREATE keyword is used to create new database objects, such as tables, views, indexes, and procedures. The basic syntax for using the CREATE keyword is as follows:
CREATE object_type object_name ( column1 datatype, column2 datatype, ... );
object_type specifies the type of object to be created, such as TABLE, VIEW, INDEX, or PROCEDURE, and
object_name is the name of the object. The
column2, etc. are the columns that will be included in the object, along with their data types.
For example, to create a new table named “Students” with columns for student ID, name, age, and gender, the SQL statement would be:
CREATE TABLE Students ( StudentID INT PRIMARY KEY, Name VARCHAR(50), Age INT, Gender VARCHAR(10) );
This would create a new table with the name “Students”, containing four columns named “StudentID”, “Name”, “Age”, and “Gender”, with the first column defined as the primary key.
Similarly, to create a new view that selects data from an existing table, the SQL statement would be:
CREATE VIEW ViewName AS SELECT column1, column2, ... FROM TableName WHERE condition;
In this case,
ViewName is the name of the new view,
TableName is the name of the existing table that the view will select data from, and
condition is an optional condition that can be used to filter the data that is returned by the view.
Overall, the CREATE keyword is a fundamental part of SQL, and it is used extensively to create new database objects and define their properties and relationships.