特定の列の後に新しい列を追加し、MySQLでデフォルトを定義しますか?
特定の列の後に新しい列を追加し、デフォルト値を定義するには、いくつかの手順に従う必要があります。これを実現するには、ALTERコマンドを使用する必要があります。まずテーブルを作成しましょう-
mysql> create table DemoTable ( StudentId int NOT NULL AUTO_INCREMENT PRIMARY KEY, StudentFirstName varchar(20), StudentAge int, StudentCountryName varchar(100) ); Query OK, 0 rows affected (0.21 sec)
表の説明を確認しましょう-
mysql> desc DemoTable;
これにより、次の出力が生成されます-
+--------------------+--------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +--------------------+--------------+------+-----+---------+----------------+ | StudentId | int(11) | NO | PRI | NULL | auto_increment | | StudentFirstName | varchar(20) | YES | | NULL | | | StudentAge | int(11) | YES | | NULL | | | StudentCountryName | varchar(100) | YES | | NULL | | +--------------------+--------------+------+-----+---------+----------------+ 4 rows in set (0.15 sec)
以下は、特定の列の後に新しい列を追加し、デフォルトを定義するためのクエリです。列名「StudentFirstName」の後に新しい列「StudentLastName」を追加しましょう。 StudentLastName列のデフォルト値は「Doe」です。
mysql> alter table DemoTable add StudentLastName varchar(20) NOT NULL after StudentFirstName; Query OK, 0 rows affected (0.91 sec) Records: 0 Duplicates: 0 Warnings: 0 mysql> alter table DemoTable alter StudentLastName set default 'Doe'; Query OK, 0 rows affected (0.32 sec) Records: 0 Duplicates: 0 Warnings: 0
テーブルの説明をもう一度確認しましょう。
mysql> desc DemoTable;
これにより、次の出力が生成されます-
+--------------------+--------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +--------------------+--------------+------+-----+---------+----------------+ | StudentId | int(11) | NO | PRI | NULL | auto_increment | | StudentFirstName | varchar(20) | YES | | NULL | | | StudentLastName | varchar(20) | NO | | Doe | | | StudentAge | int(11) | YES | | NULL | | | StudentCountryName | varchar(100) | YES | | NULL | | +--------------------+--------------+------+-----+---------+----------------+ 5 rows in set (0.01 sec)
-
MySQL列にNULL値の特定の値を配置します
IFNULL()を使用して、NULL値の特定の値を見つけて配置します。まずテーブルを作成しましょう- mysql> create table DemoTable1878 ( FirstName varchar(20) ); Query OK, 0 rows affected (0.00 sec) 挿入コマンド-を使用して、テーブルにいくつかのレコードを挿入します mysql> insert into DemoTable1878 values('Chris'); Query OK, 1 r
-
DEFAULTを使用して特定のオプションを使用してMySQLで新しいテーブルを作成しますか?
このためには、列のデータ型の後にDEFAULTキーワードを使用します。 テーブルを作成しましょう- mysql> create table demo33 −> ( −> id int not null auto_increment primary key, −> name varchar(20) not null, −> start_date date default(current_date), −> end_date date default NULL, −> categor