We have already learned what SQL is and how we can use basic select statements to get information out of our database. We can use that information we’ve selected to draw insights from potentially complicated databases. In this article, we’re going to learn how to create database tables with SQL.
Remember that tables are basically database items that contain organized columns of data. In the article about select statements, we were working with a table called “respondents”. This table contained a vast list of people who had repsonded to an email campaign indicating that they would like to receive new emails. The columns of this table included a unique identification number for each respondent as well as their name, email, and IP address.
In the following example we’re going to show you the code used to create the respondents table:
CREATE TABLE respondents ( id INT, first_name VARCHAR(50), last_name VARCHAR(50), email VARCHAR(50), ip_address VARCHAR(20) );
Notice, in the code block above, the first line starts the “CREATE TABLE” statement followed by open parenthesis. Each new line (followed by a comma) introduces the name of a new column in the table and what kind of “datatype” it will hold. For example, numbers can be held within an INT (integer) datatype.
You now have a better understanding of the syntax and usage of the “create table” statement in SQL. And, you have an example of how this statmement is used in the wild. Be sure to leave a comment if you have any questions.