Dienstag, 9. Februar 2016

MySQL: How to Insert Auto IncrementsIn Feburary 2016 09,

In Feburary 2016 09,
Identify the fields and tables in the database where auto-increment fields are required. Auto-increment fields are normally used as a primary key in a table or part of the primary key. If it is not possible to put the auto-increment value in an existing table, consider creating a new table.
Create a table by writing out the Data Manipulation Commands (DML) in a text file in MySQL format. Choose a suitable auto-incremental field to use for your requirements. Auto increment fields should have the 'NOT NULL' attribute associated with them to avoid potential problems in the database.The following is an example table with the first field, 'trackingid,' set as the auto-increment field. Every time a record is inserted into the table, the field is automatically incremented in the database. In this case, both the 'trackingid' fields and the 'customerid' field are the primary key.CREATE TABLE itemorder (
trackingid INT(8) AUTO_INCREMENT NOT NULL,customerid INT(8) NOT NULL,staffid INT(8) NOT NULL,serviceid INT(8) NOT NULL,itemdescription VARCHAR(100) NOT NULL,destinationid INT(8) NOT NULL,weightclass INT(1) NOT NULL,deliverytime INT(1) NOT NULL,deliverycost DECIMAL(9,2) NOT NULL,trackstatus VARCHAR(30) NOT NULL,lasttracked DATE NOT NULL,PRIMARY KEY (trackingid, customerid)
);
Set the starting value of the auto increments. The default value of the auto incremental is normally '1,' which can be changed by altering the table statement for the increment. In the following example, it has been set to start at '100.' The alter table statement usually occurs after the table has been created.ALTER TABLE itemorder AUTO_INCREMENT = 100;
Run 'Insert' statements on the MySQL command line or in an SQL file to the insert records into the table. The auto-increment field will automatically be updated in the table. The following example shows allows this can be achieved.INSERT INTO itemorder (customerid, staffid, serviceid, itemdescription, destinationid, weightclass, deliverytime, deliverycost, trackstatus, lasttracked)
VALUES ('5','234','98','headphones','789','14','7',9.65,'Order Received','16-05-2011');
The 'trackingid' field has been ignored in the field list, as it does not need to be included. It will be added automatically because it is an auto increment field.Therefore, if the starting value is '100,' the next value inserted into the auto increment field 'trackingid' will be '101.'
In Feburary 2016 09,

Keine Kommentare:

Kommentar veröffentlichen