Operasi medan data
Andaikan kita mempunyai jadual pengguna, dan struktur pengguna adalah seperti berikut:
mysql> desc user; +----------+---------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +----------+---------------+------+-----+---------+-------+ | username | varchar(10) | YES | | NULL | | | password | varchar(32) | YES | | NULL | | | createtime | int(10) | YES | | NULL | | | createip | int(10) | YES | | NULL | | +----------+---------------+------+-----+---------+-------+ 4 rows in set (0.01 sec)
Ubah suai jenis medan jadual ubah suai
Mari kita laksanakan dan lihat hasilnya:
mysql> alter table user modify username varchar(20); Query OK, 0 rows affected (0.48 sec) Records: 0 Duplicates: 0 Warnings: 0
mysql> desc user; +----------+---------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +----------+---------------+------+-----+---------+-------+ | username | varchar(20) | YES | | NULL | | | password | varchar(32) | YES | | NULL | | | createtime | int(10) | YES | | NULL | | | createip | int(10) | YES | | NULL | | +----------+---------------+------+-----+---------+-------+ 4 rows in set (0.01 sec)
mysql> alter table emp add column age int(3); Query OK, 0 rows affected (0.40 sec) Records: 0 Duplicates: 0 Warnings: 0
mysql> desc user; +----------+---------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +----------+---------------+------+-----+---------+-------+ | username | varchar(20) | YES | | NULL | | | password | varchar(32) | YES | | NULL | | | createtime | int(10) | YES | | NULL | | | createip | int(10) | YES | | NULL | | | age | int(3) | YES | | NULL | | +----------+---------------+------+-----+---------+-------+ 5 rows in set (0.00 sec)
Kawal susunan medan semasa menambah medan
Kami baru belajar menambah medan. Jika anda bereksperimen dengan teliti dan mendapati bahawa setiap kali ia ditambahkan pada penghujung, bagaimana untuk menambahnya pada yang pertama atau selepas medan penunjuk?
ALTER TABLE user ADD email VARCHAR(60) AFTER createip; Query OK, 0 rows affected (0.40 sec) Records: 0 Duplicates: 0 Warnings: 0
mysql> desc user; +----------+---------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +----------+---------------+------+-----+---------+-------+ | username | varchar(20) | YES | | NULL | | | password | varchar(32) | YES | | NULL | | | createtime | int(10) | YES | | NULL | | | createip | int(10) | YES | | NULL | | | email | varchar(60) | YES | | NULL | | | age | int(3) | YES | | NULL | | +----------+---------------+------+-----+---------+-------+ 6 rows in set (0.00 sec)
Padamkan medan jadual
mysql> alter table user drop column age; Query OK, 0 rows affected (0.27 sec) Records: 0 Duplicates: 0 Warnings: 0
mysql> desc user; +----------+---------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +----------+---------------+------+-----+---------+-------+ | username | varchar(20) | YES | | NULL | | | password | varchar(32) | YES | | NULL | | | createtime | int(10) | YES | | NULL | | | createip | int(10) | YES | | NULL | | | email | varchar(60) | YES | | NULL | | +----------+---------------+------+-----+---------+-------+ 5 rows in set (0.00 sec)
Contoh
alter table user change email em varchar(60);🎜>Penerangan contohDalam jadual pengguna, namakan medan e-mel dalam medan emContoh terperinci:
mysql> alter table user change email em varchar(60); Query OK, 0 rows affected (0.38 sec) Records: 0 Duplicates: 0 Warnings: 0
mysql> desc user; +----------+---------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +----------+---------------+------+-----+---------+-------+ | username | varchar(20) | YES | | NULL | | | password | varchar(32) | YES | | NULL | | | createtime | int(10) | YES | | NULL | | | createip | int(10) | YES | | NULL | | | em | varchar(60) | YES | | NULL | | +----------+---------------+------+-----+---------+-------+ 5 rows in set (0.00 sec)Ubah suai susunan medan jadual Dalam penyataan penambahan dan pengubahsuaian medan sebelumnya (tambah/ubah/ubah suai), anda boleh menambah pilihan pertama|selepas pilihan pada penghujung. Kami telah pun mempelajari cara melaraskan susunan apabila menambah medan jadual. Sekarang mari kita lihat bagaimana perubahan atau pengubahsuaian lain boleh melaraskan susunan itu. Mari kita lakukan percubaan kecil menggunakan dahulu. Gunakan ubah suai untuk melaraskan susunan
mysql> alter table user modify em varchar(60) first; Query OK, 0 rows affected (0.41 sec) Records: 0 Duplicates: 0 Warnings: 0
mysql> desc user; +----------+---------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +----------+---------------+------+-----+---------+-------+ | em | varchar(60) | YES | | NULL | | | username | varchar(20) | YES | | NULL | | | password | varchar(32) | YES | | NULL | | | createtime | int(10) | YES | | NULL | | | createip | int(10) | YES | | NULL | | +----------+---------------+------+-----+---------+-------+ 5 rows in set (0.00 sec)Ubah suai nama jadual
mysql> alter table user rename new_user; Query OK, 0 rows affected (0.35 sec)
mysql> desc new_user; +----------+---------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +----------+---------------+------+-----+---------+-------+ | em | varchar(60) | YES | | NULL | | | username | varchar(20) | YES | | NULL | | | password | varchar(32) | YES | | NULL | | | createtime | int(10) | YES | | NULL | | | createip | int(10) | YES | | NULL | | +----------+---------------+------+-----+---------+-------+ 5 rows in set (0.00 sec)