There are many ways to insert data into a database. Most are automated and exists through the creation of an app’s user data. I can’t speak to these processes. However, I am very interested in learning about these!
In this blog, I want to show two manual ways of creating a SQL table and adding data to that table. So, there are two goals of this post:
Goal: learn how to insert data into a table for SQL toy examples, and
Goal: learn how to create a SQL table from a csv file.
Again, I am going to use data from astronomical objects as examples. Let’s begin.
First, let’s cover creating a table of data for a toy example. Suppose you want to recreate the black_holes table I have used in many of my SQL posts, one of which is Classifying black holes with SQL. Let’s use the same data, which I have provided again, here.
Image for post
Now create a table with the data columns specified. Here, the columns are:
name — the name associated with the black hole (a record); this is recorded as TEXT data type.
base_mass and power — the mass of the record, each recorded as anINT (integer). To understand this, use scientific notation: base_mass x 10^power. The unit of mass for these black holes is in solar masses, or “the mass of the Sun”. For example, the Sombrero galaxy supermassive black hole is 1x10⁹ times the mass of the Sun.
And, we will include one more column:
num — the unique number of the record, which is recorded as an INT.
To create a table in the database, we need to create a table object with the CREATE TABLE statement. There is a simple syntax for creating a SQL Server table
#postgresql #postgres #sql