Tables are contained within a database. You can have multiple tables within a database. We use create table to create a table schema.
The schema below is what defines your database and what your data must adhere to. For example, you'll define field types that can only accept certain types of data. If you try to put the wrong data type into a field, the database will give you an error.
create table names (
id int unsigned not null,
lastName varchar(252) not null,
firstName varchar(252) not null
) engine=innodb default charset=latin1;
In the create table above, we've added options for each field. For example, our id field is an unsigned int field. The unsigned qualifier makes this field allow only positive integers, meaning you can't store negative numbers.
Our other two fields are varchar(252). This means we can store a string that has up to 252 letters, numbers, or symbols. Basically, anything you can type on your keyboard.
We'll start learning the alter table commands. Alter table commands modify how the table operates.
Let's turn the id field into a primary key with the following command:
alter table names add primary key (id);
Next, we'll turn the id field into an auto increment field. An auto increment field allows us to have the database insert a number automatically. The cool thing about this is that each time it inserts a record, it will also increment the number up by one.
alter table names modify id int unsigned not null auto_increment;