How can a table be created using SQL statements in MySQL?
To create a table using SQL statements, you can use the CREATE TABLE statement.
The basic syntax of the CREATE TABLE statement is:
Create a table named “tableName” with columns 1, 2, 3, etc., each specifying data type, length, and constraints.
To create a table named “users” with columns for id, name, and age, you can use the following statement:
Create a table called users with columns id, name, and age. The id column is the primary key, the name column is a required field with a maximum length of 50 characters, and the age column is an integer.
In the CREATE TABLE statement above, the id column is specified as the primary key, the name column is specified as not null, and there are no constraints specified for the age column.
In addition to basic data types and constraints, other options can also be used to define tables, such as setting default values and enabling auto-increment.
For example, to create a table named “orders” with three columns: id (auto-increment primary key), amount (default value of 0), and created_at (default value of current time), you can use the following statement:
The table “orders” is being created with three columns: an ID column that is an integer and serves as the primary key, an amount column that is a decimal with a specified format, and a created_at column that is a timestamp with the default value being the current timestamp.