Tuhinga Tuhinga
MySQLMe pehea te hanga raupapa?pātengi raraunga MySQLWaihangahia he tauākī raupapa piki-aunoa
Whakamahinga raupapa MySQL
Ko te raupapa MySQL he huinga tauoti: 1, 2, 3, ..., i te mea ka taea e te ripanga raraunga te whai i te waahanga matua-aunoa matua matua, ki te hiahia koe ki te whakatutuki i te pikinga aunoa o etahi atu mara, ka taea e koe te whakamahi i te raupapa MySQL ki tutuki.
I tenei upoko ka whakaahuahia me pehea te whakamahi i nga raupapa MySQL.
Whakamahia AUTO_INCREMENT
Ko te huarahi ngawari ki te whakamahi i nga raupapa i MySQL ko te whakamahi MySQL AUTO_INCREMENT hei tautuhi i nga pou.
Akoranga
Ka waihangahia te ripanga raraunga Insect i te tauira e whai ake nei, kaore he uara i tohua mo te tipu aunoa.
mysql > CREATE TABLE昆虫 - > (- > id INT UNSIGNED NOT NULL AUTO_INCREMENT ,- > PRIMARY KEY (id ),- > name VARCHAR (30 )NOT NULL ,#昆虫类型- > 日期DATE NOT NULL ,#收集日期- > origin VARCHAR (30 )NOT NULL #where collected ); 查询OK ,0 行受影响(0.02 秒) mysql > INSERT INTO insects (id ,name ,date ,origin )VALUES - > (NULL ,'housefly' ,'2001-09-10' ,'kitchen' ),- > (NULL ,'millipede' ,'2001-09-10 ' ,'车道' ),- > (NULL ,'grasshopper' ,'2001-09-10' ,'front yard' ); 查询OK , 秒)记录:3 重复:0 警告:0 mysql > 选择* 从昆虫ORDER BY ID ; + ---- + ------------- + ------------ + ------------ + | id | 名字 | 日期 | 起源 | + ---- + ------------- + ------------ + ------------ + | 1 | 家蝇 | 2001 - 09 - 10 | 厨房 | | 2 | 千足虫 | 2001 - 09 - 10 | 车道 | | 3 | 蚱蜢| 2001 - 09 - 10 | 前院| + ---- + ------------- + ------------ + ------------ + 3 行中集(0.00 秒)
Tikina te uara AUTO_INCREMENT
I roto i te kiritaki MySQL ka taea e koe te whakamahi i te mahi LAST_INSERT_ID() i roto i te SQL ki te whakauru i te uara o te tīwae whakauru-aunoa whakamutunga ki roto i te ripanga.
Ka whakaratohia ano nga mahi e rite ana ki te tuhinga PHP, PERL ranei kia whiwhi ai i te uara o te pou taapiri-aunoa whakamutunga kua whakauruhia ki te ripanga.
tauira PERL
Whakamahia te taonga mysql_insertid ki te tiki i te uara o AUTO_INCREMENT.Ko nga tauira e whai ake nei:
$ dbh - > do (“INSERT INTO insect(name,date,origin) VALUES('moth','2001-09-14','windowsill')“ ); my $ seq = $ dbh - > { mysql_insertid };
tauira PHP
Ka whiwhi a PHP i te uara o te AUTO_INCREMENT tīwae i roto i te tauākī SQL whakauru mā te mahi mysql_insert_id().
mysql_query (“INSERT INTO昆虫(名字,日期,起源) VALUES('moth','2001-09-14','windowsill')“ ,$ conn_id ); $ seq = mysql_insert_id ($ conn_id );
tautuhi raupapa
Mena ka mukua e koe nga rekoata maha i roto i te ripanga raraunga ka hiahia koe ki te whakariterite ano i te AUTO_INCREMENT tīwae o te toenga raraunga, ka taea e koe ma te whakakore i te tīwae piki-aunoa ka tāpiri anō.Heoi, me tino tupato tenei mahi, mena kei reira.I te wa ano o te mukunga, ka taapirihia nga rekoata hou, ka raru pea nga raraunga. Ko nga mahi e whai ake nei:
mysql > ALTER TABLE昆虫DROP ID ; mysql > ALTER TABLE昆虫 - > ADD ID INT UNSIGNED NOT NULL AUTO_INCREMENT FIRST ,- > ADD PRIMARY KEY (id );
Tautuhia te uara timatanga o te raupapa
I te nuinga o te waa, ko te uara timatanga o te raupapa he 1, engari ki te hiahia koe ki te tohu i te uara timatanga o te 100, katahi ka taea e taatau ki te korero e whai ake nei:
mysql > CREATE TABLE昆虫 - > (- > id INT UNSIGNED NOT NULL AUTO_INCREMENT ,- > PRIMARY KEY (id ),- > name VARCHAR (30 )NOT NULL ,- > date DATE NOT NULL ,- > origin VARCHAR (30 )NOT NULL )engine = innodb auto_increment = 100 charset = utf8 ;
Ka taea ranei e koe te whakamahi i te korero e whai ake nei i muri i te hanganga angitu o te ripanga:
mysql > ALTER TABLE t AUTO_INCREMENT = 100 ;
Hope Chen Weiliang Blog ( https://www.chenweiliang.com/ ) shared "Me pehea e hanga ai e MySQL he raupapa? MySQL pātengi raraunga ki te hanga i te tauākī raupapa piki ake", hei awhina i a koe.
Nau mai ki te tohatoha i te hono o tenei tuhinga:https://www.chenweiliang.com/cwl-498.html
Nau mai ki te hongere Telegram o te blog a Chen Weiliang ki te tiki i nga korero hou!
📚 He uara nui tenei aratohu, 🌟He waahi onge tenei, kaua e ngaro! ⏰⌛💨
Tohaina me te pai ki te pai koe!
Ko to tiritiri me o hiahia ko to maatau hihiri tonu!