在 MySQL 中向现有表的某个新列中插入数据?
首先创建一个表 -
mysql> create table DemoTable( Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, Name varchar(100) ); Query OK, 0 rows affected (0.47 sec)
使用 insert 命令向表中插入一些记录 -
mysql> insert into DemoTable(Name) values('John'); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable(Name) values('Bob'); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable(Name) values('Adam'); Query OK, 1 row affected (0.28 sec)
使用 select 语句显示表中的所有记录 -
mysql> select *from DemoTable;
这将产生以下输出 -
+----+------+ | Id | Name | +----+------+ | 1 | John | | 2 | Bob | | 3 | Adam | +----+------+ 3 rows in set (0.00 sec)
以下是添加列的查询 -
mysql> alter table DemoTable add column Gender ENUM('MALE','FEMALE'); Query OK, 0 rows affected (0.55 sec) Records :0 Duplicates : 0 Warnings : 0
以下是向现有表的某个新列中插入数据的查询 -
mysql> update DemoTable set Gender='FEMALE' where Id=2; Query OK, 1 row affected (0.13 sec) Rows matched − 1 Changed − 1 Warnings − 0
让我们再次检查表记录 -
mysql> select *from DemoTable;
这将产生以下输出 -
+----+------+--------+ | Id | Name | Gender | +----+------+--------+ | 1 | John | NULL | | 2 | Bob | FEMALE | | 3 | Adam | NULL | +----+------+--------+ 3 rows in set (0.00 sec)
广告