MySQL
 Computer >> コンピューター >  >> プログラミング >> MySQL

MySQLで列名の名前を変更しますか?


MySQLで列名の名前を変更するには、ALTERコマンドとCHANGEコマンドを使用する必要があります。

まずテーブルを作成しましょう-

mysql> create table DemoTable796 (
   StudentId int NOT NULL AUTO_INCREMENT PRIMARY KEY,
   Name varchar(100),
   StudentAge int
);
Query OK, 0 rows affected (0.56 sec)

表の説明を確認しましょう-

mysql> desc DemoTable796;

これにより、次の出力が生成されます-

+------------+--------------+------+-----+---------+----------------+
| Field      | Type         | Null | Key | Default | Extra          |
+------------+--------------+------+-----+---------+----------------+
| StudentId  | int(11)      | NO   | PRI | NULL    | auto_increment |
| Name       | varchar(100) | YES  |     | NULL    |                |
| StudentAge | int(11)      | YES  |     | NULL    |                |
+------------+--------------+------+-----+---------+----------------+
3 rows in set (0.00 sec)

以下は、MySQLで列名の名前を変更するためのクエリです-

mysql> alter table DemoTable796 change Name StudentName varchar(100);
Query OK, 0 rows affected (0.29 sec)
Records: 0 Duplicates: 0 Warnings: 0

テーブルの説明をもう一度確認しましょう-

mysql> desc DemoTable796;

これにより、次の出力が生成されます-

+-------------+--------------+------+-----+---------+----------------+
| Field       | Type         | Null | Key | Default | Extra          |
+-------------+--------------+------+-----+---------+----------------+
| StudentId   | int(11)      | NO   | PRI | NULL    | auto_increment |
| StudentName | varchar(100) | YES  |     | NULL    |                |
| StudentAge  | int(11)      | YES  |     | NULL    |                |
+-------------+--------------+------+-----+---------+----------------+
3 rows in set (0.00 sec)

  1. MySQLで列にNULL行を掛けますか?

    NULL行で乗算するには、COALESCE()を使用できます。まずテーブルを作成しましょう- mysql> create table DemoTable1842      (      NumberOfItems int,      Amount int      ); Query OK, 0 rows affected (0.00 sec) 挿入コマンド-を使用して、テーブルにいくつかのレコードを挿入します mysql> insert into DemoTab

  2. MySQLで個別の列名を表示する

    テーブルを作成しましょう- mysql> create table DemoTable1996 (    ShippingDate datetime,    CustomerName varchar(20) ); Query OK, 0 rows affected (0.84 sec) 挿入コマンド-を使用して、テーブルにいくつかのレコードを挿入します mysql> insert into DemoTable1996 values('2019-12-21 10:45:00','Chris'); Query O