课程: Advanced SQL Practice: Manage Tables

Solution: Build the Contacts table

- [Instructor] This challenge asks us to create the Contacts table for our fictional text chat application. To do that, I started my solution with a Create statement to create the Contacts table. The challenge provides three field definitions that we need to add. The first is a column named ID which is going to be an integer and which will automatically increment when new records are added, giving us a primary key we can use to refer to these records in other places, like in the Messages table that we'll build in a little while. auto_increment tells the database to do this incrementation for us. The next field that we'll add is the FirstName field, which will be of type varying character with a length of 100. This data type saves us a little bit of space as compared to a static field length like we'd get with just a regular character field. This field can't be null. The last field is the LastNname field, and it's also going to be Varchar or Varchar 100. It can be null though, so we'll leave off that not null directive that the previous field has. To start using our table, we'll use an Insert statement to add to contacts. These will be assigned ID numbers automatically, so we don't need to pass that value in when we create the records. Then, we'll delete the second contact, which corresponds with Jerome here. And then, we'll add a third contact, only providing a first name. Adding this contact will result in having contacts with the ID 1 and 3. And to demonstrate that, we'll use a Select statement to return all the fields for all the records in the Contacts table. I'll run my solution. And it looks like it matches what the challenge expects. Now, we have a functional Contact table, so let's keep building our database in the next few challenges.

内容