Quomodo MySQL sequentia creat? MySQL database creatio auto-incrementum serie constitutionis

MySQLQuomodo sequentia creare?MySQL databaseCreate an auto-incrementa serie dicitur

MySQL sequence usus

MySQL ordo est numerus integerrum: 1, 2, 3, ..., cum tabula data nonnisi unum campum auto-incrementum primarium clavem habere, si vis auto- rum incrementum automaticum consequi aliorum agrorum, uti potes sequentia ad MySQL. consequi.

In hoc capite narrabimus quomodo sequentium MySQL usus sit.


Utere AUTO_INCREMENT

Facillime modus est ut sequentium in MySQL utatur MySQL AUTO_INCREMENT ad columnas definiendas.

Exempli gratia

Tabulae datae Insectum hoc exemplo creatum est, in quo nulla aestimatio pro incrementi automatico specificatur.

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   
     

  

AUTO_INCREMENT valorem posside

In MySQL cliente munus LAST_INSERT_ID() uti potes in SQL ut valorem columnae ultimi auto-incrementi tabellae inseratur.

Munera correspondentia etiam in litteris PHP vel PERL providentur ut obtineant valorem columnae auto- auctae in tabula ultima inserta.

PERL exempli gratia

Utere mysql_insertis possessionibus ut valor ipsius AUTO_INCREMENT.Exempla sunt haec:

$ dbh - > do “INSERT INTO insect(name,date,origin) 
VALUES('moth','2001-09-14','windowsill')“ ); my $ seq = $ dbh - > { mysql_insertid };

PHP example

PHP obtinet valorem columnae AUTO_INCREMENT in exsecuto constitutionis SQL inserta per munus mysql_insert_id()

mysql_query “INSERT INTO昆虫(名字,日期,起源)
VALUES('moth','2001-09-14','windowsill')“ $ conn_id ); 
$ seq = mysql_insert_id $ conn_id );

reset ordinem

Si plures tabulas in tabula data deleas et columnam AUTO_INCREMENT reliquarum notitiarum voles ordinare, id facere potes delendo columna auto-incrementa et iterum addendo.Sed haec operatio diligentius debet esse, si omnino.Eodem tempore deletionis novae tabulae adduntur, et forte confusio data est: res sunt hae:

mysql > ALTER TABLE昆虫DROP ID ; 
mysql > ALTER TABLE昆虫
     - > ADD ID INT UNSIGNED NOT NULL AUTO_INCREMENT FIRST - > ADD PRIMARY KEY id );
    

Set initium valorem sequentis

In genere principium valoris sequentis est 1, sed si valorem initium 100 denotare debes, id cum sequenti enuntiatione facere possumus;

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 ; 
    
    
     
    
    

Vel etiam sequenti enuntiatione uti potes, postquam feliciter mensam creatur;

mysql > ALTER TABLE t AUTO_INCREMENT = 100 ; 

发表 评论

Quisque sit amet nisl. 必填 项 已 用 * Pittacium

Scroll To Top