MySQL 15 - Auto Increment (AUTO_INCREMENT)

submitted by mkenny400 on 03/20/18 1

If you are using surrogate keys, every row is going to need to have a unique value for this column. The easiest way to do this is to just count upwards. The downside is that every time you insert data you are going to have to know what number to use. Fortunately, there is an easier way, with what is known as an AUTO_INCREMENT column. An AUTO_INCREMENT column is a column in a table that automatically counts upwards. If we set the PK column as an AUTO_INCREMENT column, we do not have to worry about keeping track of the numbers we've already used. Now, you can only have one AUTO_INCREMENT column per table, and you can only have one primary key per table. AUTO_INCREMENT has to be used on a primary key or a unique index. It makes sense to make the primary key column the AUTO_INCREMENT column, but you can create any other column as the AUTO_INCREMENT column as long as it has a UNIQUE index. So, for example, if you wanted to use a natural primary key but wanted to have a column that auto increments, you could do that by making the incrementing column UNIQUE. Now, the AUTO_INCREMENT does not force the data to never change. It is important to understand that the concept of surrogate and natural keys is foreign to the database. The database does not care what kind of key you use, as long as the qualifications are met. There are no keywords that say surrogate. There are no keywords that say natural. These are categories that we've made to help us better communicate about primary keys. An interesting thing you can do with primary keys is that you can make a combination of columns the primary key. This is important when more than one column is required to make something unique. In an intermediary table, instead of having an association ID, we can have the combination of two rows as the primary key. This is known as either a compound or composite primary key. In the situation of using natural keys, composite keys are more common. For example, you could have a shopping website that allows multiple people from the same household to use a certain coupon you sent out. In this situation, you could use the address and the person's name as the natural primary key. How do we reference primary keys? This requires what is known as a foreign key, which we will discuss in the next video. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Support me! www.patreon.com/calebcurry Subscribe to my newsletter: eepurl.com/-8qtH Donate!: bit.ly/DonateCTVM2. ~~~~~~~~~~~~~~~Additional Links~~~~~~~~~~~~~~~ More content: CalebCurry.com Facebook: www.facebook.com/CalebTheVideoMaker Google+: plus.google.com/+CalebTheVideoMaker2 Twitter: twitter.com/calebCurry Amazing Web Hosting - www.dreamhost.com/r.cgi?1487063 (The best web hosting for a cheap price!)

Leave a comment

Be the first to comment

Collections with this video
Email
Message
×
Embed video on a website or blog
Width
px
Height
px
×
Join Huzzaz
Start collecting all your favorite videos
×
Log in
Join Huzzaz

facebook login
×
Retrieve username and password
Name
Enter your email address to retrieve your username and password
(Check your spam folder if you don't find it in your inbox)

×