In my yii2 form I have a field name number
, which is unique in DB.
This number
will be used for sorting in frontend.
Whats the best way to deal with this field in backend?
I guess you're looking how to create a primary key ? primary key must be unique here I declare number_field
as primary key which is unique.
Here is an example of a table creation:
CREATE TABLE `table_name` (
`number_field` bigint(20) NOT NULL,
`another_field` bigint(20) DEFAULT NULL,
`extra_field` varchar(255) DEFAULT NULL,
PRIMARY KEY (`number_field`),
) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8
also you can use UNIQUE KEY prop instead of PRIMARY KEY
you can change existing table like this
alter table
your_table_name
add unique (number_field)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.