Mysql Create A Table With Code

Mysql Create A Table With Code
Written by Thomas Brown

In this post I will show you how to create a table with code on mysql database. Sometimes we need to create tables with code. I have created a table with the name of the author below. Later we will gradually add some features to this table.

If we want to make a column non-null, we need to write “not null” in the row where we created the column. In the table below, the “authorId” column can no longer be empty

Sometimes we want some columns to have a default value. In the code below, the default value has been added to the author name column.

We can give auto-incrementing number feature to columns with numeric feature. You can see an example of this in the code below. The user does not enter values for such columns. In each new record, the next value is automatically given.

PRIMARY KEY (authorId) code is for to make authorId Primary Key. Primary Key means: No data can be repeated in the AuthorId field. Each table must have a primary key column.

If we want to set auto increment value then we must use the code below. In the code below, the auto-increment value is set to 30. So the first new line to be added will be authorId value 30

If we want to use utf8 character encoding in our table, we must add “COLLATE” to our code.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: