Mysql
 sql >> Teknologi Basis Data >  >> RDS >> Mysql

Apakah ada cara di MySQL untuk secara implisit membuat kunci utama untuk sebuah tabel?

Tidak, KUNCI UTAMA perlu didefinisikan di atas meja.

Anda mungkin berpikir tentang ini , yang berlaku untuk mesin InnoDB:

Di bawah ini adalah contoh yang menunjukkan pembuatan indeks ini untuk tabel tanpa PRIMARY KEY dan tanpa kolom UNIK.

# Create the table
create table test.check_table (id int, description varchar(10)) ENGINE = INNODB;

# Verify that there is no primary or unique column
desc test.check_table;
+-------------+-------------+------+-----+---------+-------+
| Field       | Type        | Null | Key | Default | Extra |
+-------------+-------------+------+-----+---------+-------+
| id          | int(11)     | YES  |     | NULL    |       |
| description | varchar(10) | YES  |     | NULL    |       |
+-------------+-------------+------+-----+---------+-------+


# Insert some values
insert into test.check_table values(1, 'value-1');
insert into test.check_table values(2, 'value-2');
insert into test.check_table values(null, 'value-3');
insert into test.check_table values(4, null);
insert into test.check_table values(1, 'value-1');


# Verify table
select * from test.check_table;
+------+-------------+
| id   | description |
+------+-------------+
|    1 | value-1     |
|    2 | value-2     |
| NULL | value-3     |
|    4 | NULL        |
|    1 | value-1     |
+------+-------------+


# Verify that the GEN_CLUST_INDEX index is auto-created.
select * from INFORMATION_SCHEMA.INNODB_INDEX_STATS where TABLE_SCHEMA='test' and TABLE_NAME = 'check_table';
+--------------+-------------+-----------------+--------+--------------+-------------------+------------------+
| table_schema | table_name  | index_name      | fields | rows_per_key | index_total_pages | index_leaf_pages |
+--------------+-------------+-----------------+--------+--------------+-------------------+------------------+
| test         | check_table | GEN_CLUST_INDEX |      1 | 5            |                 1 |                1 |
+--------------+-------------+-----------------+--------+--------------+-------------------+------------------+

# Duplicate rows are still allowed (Primary Key constraints not enforced)

insert into test.check_table values(1, 'value-1');

select * from test.check_table;
+------+-------------+
| id   | description |
+------+-------------+
|    1 | value-1     |
|    2 | value-2     |
| NULL | value-3     |
|    4 | NULL        |
|    1 | value-1     |
|    5 | value-5     |
|    1 | value-1     |
+------+-------------+

Sebaliknya, tabel dengan PRIMARY KEY yang ditentukan membuat indeks dengan nama PRIMARY.

# Create another table
create table test.check_table_2 (id int, description varchar(10), PRIMARY KEY(id)) ENGINE = INNODB;

# Verify primary key column
desc check_table_2;
+-------------+-------------+------+-----+---------+-------+
| Field       | Type        | Null | Key | Default | Extra |
+-------------+-------------+------+-----+---------+-------+
| id          | int(11)     | NO   | PRI | 0       |       |
| description | varchar(10) | YES  |     | NULL    |       |
+-------------+-------------+------+-----+---------+-------+

# Verify index
select * from INFORMATION_SCHEMA.INNODB_INDEX_STATS where TABLE_SCHEMA='test' and TABLE_NAME = 'check_table_2';
+--------------+---------------+------------+--------+--------------+-------------------+------------------+
| table_schema | table_name    | index_name | fields | rows_per_key | index_total_pages | index_leaf_pages |
+--------------+---------------+------------+--------+--------------+-------------------+------------------+
| test         | check_table_2 | PRIMARY    |      1 | 0            |                 1 |                1 |
+--------------+---------------+------------+--------+--------------+-------------------+------------------+

# Primary key is enforced
insert into check_table_2 values(1,'value-1');
OK

insert into check_table_2 values(1,'value-1');
ERROR 1062 (23000): Duplicate entry '1' for key 'PRIMARY'



  1. Database
  2.   
  3. Mysql
  4.   
  5. Oracle
  6.   
  7. Sqlserver
  8.   
  9. PostgreSQL
  10.   
  11. Access
  12.   
  13. SQLite
  14.   
  15. MariaDB
  1. Bagaimana cara membuat kueri rekursif hierarkis MySQL?

  2. PDO beberapa tempat penampung bernama tidak mengambil data

  3. BATAS offset atau OFFSET dalam kueri SQL UPDATE

  4. Periksa kesalahan kueri codeigniter alih-alih menunjukkannya kepada pengguna

  5. Ingin membatasi nilai bidang MySQL ke rentang tertentu (Nilai desimal)