SQL Create Table and Insert Data - A Complete Tutorial

  Рет қаралды 223,697

The SQL Guy

The SQL Guy

Күн бұрын

In this tutorial we are going to look at how to create a table and insert data in SQL Server. We also look at how to insert multiple rows into a table.
This is an extensive lesson which covers a number of topics which I have provided the timestamps for below, so feel free to jump to the correct section you are interested in
00:48 - Create a Database
01:18 - How To Create a Table in SQL Server
02:50 - Insert single row into Table
04:20 - Column name or number of supplied values does not match table definition.
04:54 - Insert multiple rows into Table
06:30 - Create Table with Primary Key
07:12 - IDENTITY insert - Auto Incrementing Id Columns
08:00 - NULL VS NOT NULL
09:09 - Considerations when creating SQL tables
10:46 - Cannot insert the value NULL into COLUMN
11:24 - How To Add a Column To Table in SQL
12:43 - Add Primary Key To a table
13:39 - Drop a Primary Key Column in SQL SERVER
14:17 - Drop a column from A Table in SQL
Primary Keys
Primary Keys are required in SQL Server to keep our data unique and to enforce integrity. We want don't want duplicate data and we would like to be able to differentiate two employees
with the same name for example, so we assign each an Id. This makes each row in a SQL Server unique. Understanding how to create a primary key in SQL is vital to good database table design
Auto Incrementing Id Columns
The IDENTITY Keyword is explained in this tutorial. It runs through what it does and why it is needed. When creating Id's we usually want to auto increment our column by 1, however there are times
where we may want to increment the values by 10 or a 100 for example. IDENTITY insert gives us the ability to define that when we create a table.
NULL vs NOT NULL
Do we want a value for every row on insert? We will do for some columns! Here we use the example of a persons name - we would always want their first name, but not always their middle name so NULL and NOT NULL
come into play here. NULL essentially means blank! Do we want this column to be blank or not blank?
Data Types
Creating tables in SQL Server is a relatively easy thing to do, however it is VERY easy to do this incorrectly. A number of things need to be covered, especially data types -
because we need to understand beforehand what data is going into our tables, then we can determine what data types best suit the column.
For example; string data such as Name, Addresses would be suited to having a VARCHAR data type. As this accepts string data. However if we are inserting timestamps VARCHAR wouldn't be
ideal and would look to using a datatype such as DATETIME or simply DATE. SQL will then handle these columns as date and time fields and it makes us easier to query and report on.
The most common types of data types we will see in SQL tables are:
VARCHAR
DATETIME
INT
BIT
FLOAT
Understanding data types is briefly covered in this tutorial but to cover it extensively would be within another video
Errors when Inserting into Tables
This lesson covers a number of errors that we often encounter when inserting both single and multiple rows into SQL Tables. The most common errors are:
Column name or number of supplied values does not match table definition.
There are fewer columns in the insert statement than values specified in the VALUES clause. The number of values in the VALUES column must match number of columns
Cannot insert the value NULL into column, table; column does not allow nulls. insert fails.
The definition for column 'Id' must include a data type
This tutorial shows these error and how to avoid them why they are caused
Resources
How To Create Tables in SQL Server www.sqltutorialacademy.com/sql...
Primary Key
www.w3schools.com/sql/sql_pri...
Data Types
www.w3schools.com/sql/sql_dat...
Microsoft Definition of SQL Tables
docs.microsoft.com/en-us/sql/...

Пікірлер: 64
@GodzGem
@GodzGem 2 жыл бұрын
Man, the most clear and concise instructional video I’ve found. You did a great job with the walk through and really walked us through it. Didn’t have skip a second… I LOVE SQL !!!, I’m happy you’re showing love back to the tech community.
@TheSQLGuy
@TheSQLGuy 2 жыл бұрын
Thanks for the great comment. Really glad that it helped
@DublinObserver08
@DublinObserver08 2 жыл бұрын
In my opinion, this is the most clear, brief but very explanatory video, just straight to the point. Thanks so much, I managed to complete my assignment with your help.
@TheSQLGuy
@TheSQLGuy 2 жыл бұрын
Thanks, thats a great comment. I'm glad the video has helped
@schumacher1994
@schumacher1994 2 жыл бұрын
Very helpful, clear and to the point! Many thanks
@anthonynorman1212
@anthonynorman1212 Жыл бұрын
I often find my self coming back to your channel. Videos are very simple and easy to understand. I am not a full time dev, been trying to get a promotion by developing on the side of my regular work and using the results to speed things up. Thank for the videos and hope you get many more subscribers.
@roywit7836
@roywit7836 3 жыл бұрын
Perfect tutorial just subbed I've noticed many SSMS Videos do not describe about the primary key, this has helped thanks
@TheSQLGuy
@TheSQLGuy 3 жыл бұрын
Glad it helped. Thanks a lot
@voidthinker
@voidthinker 3 жыл бұрын
Very nice. Brief and precise videos. It helped me a lot in understanding of table creation with SQL queries.
@TheSQLGuy
@TheSQLGuy 3 жыл бұрын
Excellent, thanks a lot. Glad it helped
@PostHeartBreak
@PostHeartBreak Жыл бұрын
You just gained a subscriber, can’t wait to explore more of your videos..keep up the good work
@TheSQLGuy
@TheSQLGuy Жыл бұрын
Glad you enjoyed it! Lots more coming soon
@oratilegodwill4239
@oratilegodwill4239 3 жыл бұрын
best ever ... finally someone who knows what he's doing...!!!
@TheSQLGuy
@TheSQLGuy 3 жыл бұрын
Thanks, glad you enjoyed it! More to come soon too
@samreenfatima2551
@samreenfatima2551 Жыл бұрын
Thank you so much ,I am new to SQL have been trying to create table but it was giving error 208, went through a lot of videos but nothing helped.This is the only video that helped me create my first table. Thank you so much SQL Guy 😊😊
@TheSQLGuy
@TheSQLGuy Жыл бұрын
Glad it helped you! Long may it continue
@lwazikhanye3344
@lwazikhanye3344 2 жыл бұрын
VERY useful thank you
@owosenikehinde5600
@owosenikehinde5600 Жыл бұрын
found this very helpful. thanks
@KanizFatema-qh1rg
@KanizFatema-qh1rg Жыл бұрын
You are a best Teacher, thank you
@nwanebunkemjika7822
@nwanebunkemjika7822 5 ай бұрын
amazing !!!
@thanunvsn7345
@thanunvsn7345 2 жыл бұрын
Thank you for the video, your instruction is way better than my professor explanation.
@TheSQLGuy
@TheSQLGuy 2 жыл бұрын
Glad it helped!
@skurakox
@skurakox 3 жыл бұрын
Finally, understood what is going on with creating tables
@TheSQLGuy
@TheSQLGuy 3 жыл бұрын
Glad it helped, I've got a few more table related tutorials coming out soon too
@yougotonelifespenditwisely4390
@yougotonelifespenditwisely4390 3 жыл бұрын
finally found a tutorial
@teamvamospickleball4815
@teamvamospickleball4815 2 жыл бұрын
when inserting percentages into a table, do you set the column up as int?
@chouchoubn5364
@chouchoubn5364 9 ай бұрын
OMG TYSM I DIDNT KNOW ITS THAT EASY🥰🥰
@arturcarreira8595
@arturcarreira8595 2 жыл бұрын
Please, could you help me with a question? What is the diference between Create type [...] as table and create table () ? Thanks
@jay_with_the_real6381
@jay_with_the_real6381 2 жыл бұрын
Awesome!
@TheSQLGuy
@TheSQLGuy 2 жыл бұрын
Cheers, appreciate it
@belenavelar2106
@belenavelar2106 2 жыл бұрын
Thanks man!
@TheSQLGuy
@TheSQLGuy 2 жыл бұрын
No probs
@rosannaasejo6718
@rosannaasejo6718 2 жыл бұрын
very nice explination. hoping you will upload more video
@TheSQLGuy
@TheSQLGuy 2 жыл бұрын
Thanks, glad you liked it. Yes more coming soon so stay tuned
@itreportcambodia8283
@itreportcambodia8283 3 жыл бұрын
video good
@MrAmrshebl87
@MrAmrshebl87 3 жыл бұрын
Loved the video. I have a question please : I have a table that has two columns;Id & tag names and another table that has much more info but they have the tagnames in it. I want to query every single element in table 1 to find if it exists in table 2. I want this query to run every 5 minutes. How can I do that? thanks in advance
@TheSQLGuy
@TheSQLGuy 3 жыл бұрын
Thanks, glad you enjoyed it. I'd turn this into a stored procedure and schedule it. For the query, there are a few ways. You could use a cursor to loop through table 1 and query table 2 and depending on the result it do the next step. Alternatively you could use a Left JOIN to search for any missing values. It depends on a number of factors, cursors aren't great and try to avoid them where possible. It also depends on table size etc but I'd look at those two options
@UnderSurveillanceSince
@UnderSurveillanceSince 2 жыл бұрын
Great presentation
@TheSQLGuy
@TheSQLGuy 2 жыл бұрын
Thanks, glad you enjoyed it
@Fabian-ew7ly
@Fabian-ew7ly 2 жыл бұрын
Why did you add the [Id] column using ALTER TABLE, when you had already defined [Id] as a column in the CREATE TABLE syntax above it?
@chalkpitbullwatermelon1897
@chalkpitbullwatermelon1897 Жыл бұрын
does anyone know why when I create the table, it doesn't show up under the tables option in the left. I switched the top left thing to the name of my database I was using but it still doesn't show up and I'm not sure how to reopen the query I made under it.
@pratikshadake5311
@pratikshadake5311 2 жыл бұрын
Thank you
@TheSQLGuy
@TheSQLGuy 2 жыл бұрын
You're welcome, hope they help
@flavius95
@flavius95 2 күн бұрын
Which software you use? Mysql ?
@Steven-mf2db
@Steven-mf2db 2 жыл бұрын
I notice you use the abbreviation "tbl" for table. I wrote this in a draft design spec recently and my new boss ("who knew the job inside out") had never heard of such a thing and made me change it to TABLE telling the rest of the team that it wasn't a thing. I feel a sense of vindication that The SQL Guy uses it too.
@TheSQLGuy
@TheSQLGuy Жыл бұрын
I love tbl_ !!! Any naming convention is better than nothing though so I guess TABLE works too
@HIghlordBalkan
@HIghlordBalkan Жыл бұрын
All I get when running something similar is "Incorrect syntax near 'FirstName' _" and no new table to be found. Following exactly what I'm seeing in the video so I'm unsure as to what I'm doing wrong.
@HIghlordBalkan
@HIghlordBalkan Жыл бұрын
Closed my connection then reopened it and everything worked. Not quite sure why but I won't complain. That you very much sir.
@alwinvarghese3307
@alwinvarghese3307 7 ай бұрын
How can i insert adress
@jorgedelacruz6612
@jorgedelacruz6612 2 жыл бұрын
Hi nice work, by the way can you help me how to create table operator in sql developer
@TheSQLGuy
@TheSQLGuy 2 жыл бұрын
I've got a tutorial on SQL Operators. Let me know if that helps
@crb3488
@crb3488 2 жыл бұрын
LOL "Unless you're five, nobody cares about half years"
@TheSQLGuy
@TheSQLGuy 2 жыл бұрын
Glad my joke made someone laugh!
@cleverjay286
@cleverjay286 2 жыл бұрын
The video isn't clear here
@riffle1564
@riffle1564 2 жыл бұрын
Don't know how all have commented but bro it is impossible to see anything what u r displaying u should use Magnifier
@TheSQLGuy
@TheSQLGuy 2 жыл бұрын
Yes, I've changed that with later videos. Thanks for the feedback though
@fitlalapo6673
@fitlalapo6673 Жыл бұрын
You could have at least zoomed into the screen so we could see what your typing
@user-qd7du4ok1s
@user-qd7du4ok1s Жыл бұрын
Video 🎥 and voice is not clear.
@prasadlad2097
@prasadlad2097 Жыл бұрын
Knowledge is best but volume is very slow this video
@twistedinsane3026
@twistedinsane3026 11 күн бұрын
It doesn't work
@TheSQLGuy
@TheSQLGuy 10 күн бұрын
It does work. Which bit isn't working for you
@twistedinsane3026
@twistedinsane3026 10 күн бұрын
🤥🤥🤥
@twistedinsane3026
@twistedinsane3026 10 күн бұрын
🤥🤥🤥
@twistedinsane3026
@twistedinsane3026 10 күн бұрын
🤥🤥🤥
Create Table From SELECT statement | SQL Server Tutorial
3:37
The SQL Guy
Рет қаралды 4,8 М.
아이스크림으로 체감되는 요즘 물가
00:16
진영민yeongmin
Рет қаралды 60 МЛН
Double Stacked Pizza @Lionfield @ChefRush
00:33
albert_cancook
Рет қаралды 79 МЛН
Женская драка в Кызылорде
00:53
AIRAN
Рет қаралды 374 М.
SQL Tutorial for Beginners
44:57
Kevin Stratvert
Рет қаралды 2 МЛН
Creating Primary and Foreign Keys in SQL Server 2012
18:22
Dr. Daniel Soper
Рет қаралды 730 М.
SQL Tutorial - 9: Create Table Statement
7:48
The Bad Tutorials
Рет қаралды 1,6 МЛН
MySQL: How to create a TABLE
8:10
Bro Code
Рет қаралды 225 М.
you need to learn SQL RIGHT NOW!! (SQL Tutorial for Beginners)
24:25
NetworkChuck
Рет қаралды 1,5 МЛН
Advanced SQL Tutorial | Temp Tables
10:19
Alex The Analyst
Рет қаралды 248 М.
SQL Create Table With Primary Key | IF NOT EXISTS
5:34
The SQL Guy
Рет қаралды 266
Adding Columns With Data SQL
9:37
Dimitri Patarroyo
Рет қаралды 31 М.
아이스크림으로 체감되는 요즘 물가
00:16
진영민yeongmin
Рет қаралды 60 МЛН