关于MySQL中自增的理解和设置

show create table t10;--查看表的创建结果
show create table t10\G;--竖列查看
alter table t10 AUTO_INCREMENT =20 --设置自增为20
alter table t2.name  char(30);
insert into t2(name) values ('yaoming');
ALTER TABLE t2 ALTER Column Name varchar(100) not null;
主键:
    1.一个表只能有一个主键,主键可以由多列组成,主键不能为空.
    CREATE TABLE t5 (
                    nid int(11) NOT NULL AUTO_INCREMENT,
                    pid int(11) not NULL,
                    num int(11),
                    primary key(nid,pid)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
 外键:
     1.创建外键时可以在一个表达式中创建多个外键
          create table t5(
     id int auto_increment primary key,
     name char(10),
     nid int,
     pid int,
     )engine = innodb default charset=utf8;
     
     create table t6(
     id int auto_increment primary key,
     name char(10),
     id1 int,
     id2 int,
     constraint fk_t5_t6 foreign key (id1,id2)
     references t1(nid,pid)
     )engine = innodb default charset=utf8;
 数据行:
    插入多个数据是可以在values后面使用,分割写
    insert into tb1(name,age) values ('xiaoli',18),('xiaoming',12)
自增:
    show create table t2\G 可以查看创建表时的结构
    alter create table  t2 auto_increment = 20; --可以改变自增的起始数字
    MySQL:自增步长:
    --基于会话级别的:
        show session variables like 'auto_inc%';--查看全局变量的步长和起始值
        set session auto_increment_increment=2;
        --设置步长
        set session auto_increment_offset=10;
        --设置起始数字
     --基于全局级别的:
        show global variables like 'auto_inc%';
        --查看全局变量
        set global auto_increment_increment=2;
        --设置全局步长
        set global auto_increment_offset=10;
        --设置全局起始数字
      SqlServer: 自增步长基于表可以实现
        create table t2(
        sid int primary key identity(3,5),--从3开始 ,步长为5
        sname nchar(8) not null,
        ssex nchar(1)
        )

猜你喜欢

转载自www.cnblogs.com/tataerzu/p/10165614.html