The initialization value of the column field when the SQL statement creates the database table

In SQL, when creating a database table, you can specify the initial value of each column field, which is called "default value" (Default Value). The default value is the value that is automatically applied when a new record is inserted, if no value is explicitly provided for that column. When inserting a new row, if no value is provided for the column, the database will fill the column with a default value.

Defaults are useful for ensuring data integrity and providing default options. When inserting a new row, the database will use the default values ​​for some columns if no values ​​are provided for those columns, thus avoiding null values ​​or invalid data.

Following is the syntax for setting default values ​​when creating a database table in SQL:

CREATE TABLE table_name (
    column1 datatype DEFAULT default_value,
    column2 datatype DEFAULT default_value,
    ...
);

where table_nameis the name of the new table, column1and column2is the column name of the table, datatypeis the data type of the column, default_valueand is the default value for the column.

Now, let's elaborate with a concrete example.

Suppose we have a simple database that stores user information including user ID, username, age, and registration date. We'll create a table called "users" with some columns that will have default values ​​set.

CREATE TABLE users (
    user_id INT PRIMARY KEY,
    username VARCHAR(50) NOT NULL,
    age INT DEFAULT 18,
    registration_date DATE DEFAULT CURRENT_DATE
);

In the above example, we have created a table called "users" which contains the following columns:

  1. user_id: This is the unique identifier for the user and is set as the primary key, which means that each value must be unique and cannot be empty.
  2. username: This is the username of the user, VARCHAR(50) means it is a text field with a maximum length of 50 characters, NOT NULL specifies that the column cannot be empty.
  3. age: This is the age of the user, INT means it is an integer type field, the default value is 18. If no age is provided when inserting a new user, then the database automatically sets it to 18.
  4. registration_date: This is the user's registration date, DATE means it is a date type field, and the default value is set to the current date (using the CURRENT_DATEfunction). If no registration date is provided when inserting a new user, the database will automatically set it to the current date.

Now, let's insert some data rows and see how the defaults are applied:

-- 插入一行,没有指定年龄和注册日期
INSERT INTO users (user_id, username) VALUES (1, 'John Doe');

-- 插入一行,没有指定注册日期
INSERT INTO users (user_id, username, age) VALUES (2, 'Jane Smith', 25);

-- 插入一行,指定所有列的值
INSERT INTO users (user_id, username, age, registration_date) 
VALUES (3, 'Bob Johnson', 30, '2023-07-31');

In the first insert statement, we did not provide the age and registration date, so the database will set the age to the default value of 18 and the registration date to the current date.

In the second insert statement, we didn't provide a registration date, but we did provide an age, so the database sets the age to the value provided (25) and the registration date to the current date.

In the third insert statement, we provide values ​​for all columns, so the database will use the values ​​we provided.

This is the purpose and significance of the default value when SQL creates a database table. By setting default values, we can ensure that each row in the table has a reasonable default option, which simplifies the data insertion process and maintains data integrity. If there is no default value, then when inserting a new record, all columns that do not allow nulls must provide values ​​explicitly, otherwise the database will report an error.

To sum up, in SQL, by using DEFAULTkeywords to set the default value for the column field of the table, so that when inserting a new record, if no value is provided, the database will use the default value to fill the column. Defaults are useful for providing default options, ensuring data integrity, and simplifying the data insertion process.

Guess you like

Origin blog.csdn.net/i042416/article/details/132023804