SQL Create Table Statement - With Example Syntax

Publicado em: 07 Agosto 2020
no canal de: How4Pc mac10
2
0

Text Source:

#SYNTAX #EXAMPLE #WITH #STATEMENT #TABLE #CREATE #SQL

SQL Create Table Statement - With Example Syntax

SQL is one of the most reliable and straightforward querying languages around. It provides clear cut syntax that reads easily without abstracting away too much of the functionality's meaning.

If you'd like some history on the language as well as some interesting facts, check out the introduction portion of my SQL Update Statement article.

In this article, we're going to go through the important parts of creating a table in SQL. My preferred "flavor" of SQL is SQL Server but the information about creating a table is fairly ubiquitous across all SQL variations.

If you've never used SQL or don't know what a table is, fear not! Briefly (and broadly), a table is a database object that holds, or contains, all of the data within that portion of the database. It stores this data in named columns and numbered rows which is not unfamiliar if you've ever used any spreadsheet program. Each row represents a whole database record.

If data were in box form then a table would be a section of the warehouse shelving we store those boxes in.

I'm simplifying the explanation greatly and there is much more to SQL tables but that's outside the scope of this article. If you're itching for a more in-depth explanation on tables, I encourage you to dive into the Microsoft Database Design documentation.

Before we learn how to create the table, it's important that we learn what types of data these columns and rows can store.

When creating our table, we designate the type of data its rows and columns will hold. Here are the overarching classifications of data:

I'll list some of the more commonly used data types below, but if you'd like a more on all data types, I invite you to check out this exhaustive article on each type from Microsoft.

float(size, d) - a number with total number size represented by size and the number of characters after the decimal represented by the d

Alright, now that we know what types of data the rows and columns can contain let's get into the fun parts!

Before we start it's important to note that I'll be providing all of my examples independent of any program.

However, if you'd like to start writing queries and you aren't sure where to start, take a look at SQL Server Management Studio. It's a free, robust program that's widely used and supported in the community.

Alternatively, there are several options including DB Fiddle that allow you to build schemas and write queries right in your browser.

column5_name datatype,) There are other parameters we can add after the datatype to augment the columns:

UNIQUE KEY - passing this parameter will designate that column as a unique identifier. It is essentially a combination of the previous two parameters.

Now, we're going to create a table (named doggo_info which must adhere to the identifier standards for databases) to hold information on the residents of Woof Woof Retreat, a fictional doggy daycare I just thought of :)

You'll notice that our table is completely empty and this is because we haven't added any data to it yet. Doing so is beyond the scope of this article but I wanted you to be aware of that tidbit.

It's pretty easy and doesn't require that much more syntax. We need to select the table and columns to "copy" from:

WHERE conditions_exist So, expediency's sake, I've added some data to our doggo_info table and it now looks like the example below:

Now we can create another table based off of the data we have in our doggo_info table by running the query below:

WHERE Age ← 4 We want to create a new table with all of the columns from the doggo_info table but only where the Age is less than 4. After running this query, our new table will look like this:

I hope you can see just how powerful this statement can be. With a few lines in our query we have essentially copied data from one table into another but only the rows that we wanted.

This is not only a handy tool to have in your developer tool belt – it'll save you untold amounts of time when you need to move data around tables.

Now that you know how to create (or copy) a table in SQL no matter what situation you're presented with, you can start filling the columns and rows with data to store!

The CREATE TABLE statement is extremely useful and powerful. You're ready to start putting it to good use.

If you found this article helpful check out my blog where I frequently post articles about web development, life, and learning.

While you're there why not sign up for my newsletter? You can do that at the top right of the main blog page. I like to send out interesting articles (mine and others), resources, and tools for developers every now and then.

If you have questions about this article or just in general let me know – come say hi on Twitter or any of my other social media accounts which you can find below the newsletter sign up on the main page of my blo…


Nesta página do site você pode assistir ao vídeo on-line SQL Create Table Statement - With Example Syntax duração hora minuto segundo em boa qualidade , que foi baixado pelo usuário How4Pc mac10 07 Agosto 2020, compartilhe o link com seus amigos e conhecidos, no youtube este vídeo já foi visto 2 vezes e gostou 0 espectadores. Boa visualização!