How to create a table using SQL?

To create a table in SQL, you can use the CREATE TABLE statement. Here's the basic syntax for creating a table:

CREATE TABLE table_name (
column1 datatype,
column2 datatype,
column3 datatype,
...
);

Here's an example of creating a simple table:
Imagine we are going to create a table for school, we want to have Student_ID, Student_Name, Subject, Score in the field of our School table.
To apply it to the SQL syntax:

After each column name, we need to specify the data type for each column. For example, we can use data types like INT for integer, VARCHAR for variable-length text, DATE for date, and so on. The number inside the bracket after VARCHAR representing the maximum number of character we could have.

After creating the School table, we need to insert the values. We can do this by using INSERT INTO statement. Here's the basic syntax for inserting values into a table:


INSERT INTO table_name (column1, column2, column3, ...)
VALUES (value1, value2, value3, ...);

This inserts two additional rows into the "School" table.

Make sure the values you insert match the data types of the columns, and the order of the values corresponds to the order of the columns.

This is the result of the School table after inserting the values:

Author:
Abby Poon
Powered by The Information Lab
1st Floor, 25 Watling Street, London, EC4M 9BR
Subscribe
to our Newsletter
Get the lastest news about The Data School and application tips
Subscribe now
© 2024 The Information Lab