| 您的当前位置:首页 --> MYSQL教程 --> MySQL中的主键以及设置其自增的用法教程 |
| MYSQL教程 MySQL中的主键以及设置其自增的用法教程 |
| 浏览次数:1239 关键词 ( ) |
| 查看使用该CPU的产品 查看CPU天梯 |
| CPU型号:MySQL中的主键以及设置其自增的用法教程 |
| 主频:Ghz |
| 睿频:Ghz |
| 核心数:个 |
| 不支持超核心 |
| 制作工艺: |
| 插槽类型: |
| 功耗:0W |
| L3缓存:0MB |
| 支持最大内存: 0GB |
| CPU详细参数 |
|
1、声明主键的方法: ALTER TABLE tbl_name ADD PRIMARY KEY (index_col_name,…); /* 创建一个qq表,将qq_id设为主键,且没有对其进行NOT NULl约束 */ create table qq( qq_id int(10), nick_name varchar(255) not null, primary key (qq_id)) /* 插入一条数据,将qq号设为10000(咱也幻想一下),昵称设为"simaopig" */ INSERT INTO qq( qq_id, nick_name ) VALUES ( '10000', 'simaopig'); 主键被认为是NOT NULL和UNIQUE约束最好的结合。如果这些列没有被明确地定义为NOT NULL,MySQL会隐含地定义这些列。
/*
创建防火墙表,将host 和port组合设为主键,注意我没有将port设NOT NULL约束
*/
create table firewall(
host varchar(11) not null,
port smallint(4),
access enum('deny', 'allow') not null,
primary key (host,port))
/*
插入一条新的记录,没有啥问题
1 row(s) inserted.
*/
INSERT INTO firewall (
host ,
port ,
access)
VALUES (
'202.65.3.87', '21', 'deny');
3、设置主键自增 mysql> create database ssh2; Query OK, 1 row affected (0.04 sec) mysql> use ssh2; Database changed mysql> create table user( -> id integer primary key, -> firstname varchar(200) not null, -> lastname varchar(200) not null, -> age integer -> ); Query OK, 0 rows affected (0.46 sec) 给主键增加一个自增的功能: mysql> alter table user modify id integer auto_increment ; Query OK, 1 row affected (0.28 sec) Records: 1 Duplicates: 0 Warnings: 0 这样,上面的user表里面的主键,id可以自增了。 给上面的主键id增加默认值和自增功能。 mysql> alter table user modify id integer auto_increment ; Query OK, 0 rows affected (0.39 sec) Records: 0 Duplicates: 0 Warnings: 0 mysql> alter table user modify id integer default '1'; Query OK, 0 rows affected (0.16 sec) Records: 0 Duplicates: 0 Warnings: 0 mysql> alter table user modify id integer auto_increment ; Query OK, 1 row affected (0.28 sec) Records: 1 Duplicates: 0 Warnings: 0 MySql获取系统时间: mysql> alter table user add createtime timestamp default current_timestamp; Query OK, 2 rows affected (0.17 sec) Records: 2 Duplicates: 0 Warnings: 0 MySql设置主键不能为空,还要自动增长(这里没有设置默认值,但是默认是1,从1开始增长。),还要得到系统默认日期: mysql> create table dd( -> id int primary key not null auto_increment, -> name varchar(20), -> time timestamp default current_timestamp -> ); Query OK, 0 rows affected (0.10 sec)
mysql> insert into dd(name) values ('fhihgifds');
Query OK, 1 row affected (0.14 sec)
mysql> insert into dd(name) values ('steven');
Query OK, 1 row affected (0.08 sec) mysql> select * from dd; +----+-----------+---------------------+ | id | name | time | +----+-----------+---------------------+ | 1 | fhihgifds | 2011-03-27 01:58:46 | | 2 | steven | 2011-03-27 01:59:35 | +----+-----------+---------------------+ 2 rows in set (0.08 sec)
mysql> insert into dd(name) values ('anthony');
Query OK, 1 row affected (0.09 sec) mysql> select * from dd; +----+-----------+---------------------+ | id | name | time | +----+-----------+---------------------+ | 1 | fhihgifds | 2011-03-27 01:58:46 | | 2 | steven | 2011-03-27 01:59:35 | | 3 | anthony | 2011-03-27 02:00:07 | +----+-----------+---------------------+ 3 rows in set (0.00 sec) |
| 下一个产品 SQL计算timestamp的差值的方法 上一个产品 快速理解MySQL中主键与外键的实例教程 |