我将列id
定义为主键,但如何自动将其设置为大于最后一列?
答案 0 :(得分:3)
您正在寻找AUTO_INCREMENT
,您可以查看documentation here
您需要将id
列设置为AUTO_INCREMENT
文档中的示例
CREATE TABLE animals (
id MEDIUMINT NOT NULL AUTO_INCREMENT,
name CHAR(30) NOT NULL,
PRIMARY KEY (id)
) ENGINE=MyISAM;
答案 1 :(得分:1)
你必须设置自动增量。
CREATE TABLE animals (
id MEDIUMINT NOT NULL AUTO_INCREMENT,
name CHAR(30) NOT NULL,
PRIMARY KEY (id)
) ENGINE=MyISAM;
INSERT INTO animals (name) VALUES
('dog'),('cat'),('penguin'),
('lax'),('whale'),('ostrich');
SELECT * FROM animals;
Which returns:
+----+---------+
| id | name |
+----+---------+
| 1 | dog |
| 2 | cat |
| 3 | penguin |
| 4 | lax |
| 5 | whale |
| 6 | ostrich |
+----+---------+
http://dev.mysql.com/doc/refman/5.0/en/example-auto-increment.html mysql参考
答案 2 :(得分:0)
"AUTO_INCREMENT" it ...
请参阅链接http://www.w3schools.com/sql/sql_autoincrement.asp
示例:强> 以下SQL语句将“P_Id”列定义为“Persons”表中的自动增量主键字段:
CREATE TABLE Persons
(
P_Id int NOT NULL AUTO_INCREMENT,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Address varchar(255),
City varchar(255),
PRIMARY KEY (P_Id)
)
答案 3 :(得分:0)
好的没注意到标签。
点击phpMyAdmin中的A_I
复选框获取id列。
--- ---老
Oracle方式,触发序列:
CREATE sequence aic increment BY 1 start WITH 1;
CREATE TABLE blarg (
id NUMBER(15,0) PRIMARY KEY,
foobar VARCHAR2(255)
);
CREATE TRIGGER trigger ait BEFORE INSERT ON blarg
REFERENCING NEW AS NEW OLD AS OLD FOR EACH ROW
Begin
SELECT aic.NEXTVAL INTO :NEW.id FROM DUAL;
End;