时间:2021-07-01 10:21:17 帮助过:4人阅读
mysql> desc t7;
+-------+---------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+---------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
| sex | char(6) | YES | | male | |
+-------+---------+------+-----+---------+-------+
2 rows in set (0.01 sec)
示例二
mysql> create table t1(id int primary key auto_increment,name char(6),sex char(5) default ‘male‘); Query OK, 0 rows affected (0.48 sec)
mysql> insert t1(name) values -> (‘alex‘); Query OK, 1 row affected (0.30 sec) mysql> select * from t1; +----+------+------+ | id | name | sex | +----+------+------+ | 1 | alex | male | +----+------+------+ 1 row in set (0.00 sec)
2 auto_increment 自增
针对所有表的设置:
设置自增的步长:auto_increment_increment
设置自增的初始值:auto_increment_offset
针对单个表的设置:
auto_increment
查看回话级别变量
show session variables like ‘auto_inc%‘;
查看全局级别变量
show global variables like ‘auto_inc%‘;
设置基于回话级别:
set session auto_increment_increment=2
设置基于全局级别
set global auto_increment_increment=2
PS:
如果自增的步长小于自增的初始值,则自增的初始值会被忽略!即自增的初始值一定要小于等于自增的步长!
所以你会发现这个并没有多大卵用。
设置完以后,不能生效。退出,重新登录,才能生效。
示例 1:
针对所有表的设置
mysql> set global auto_increment_increment=2; Query OK, 0 rows affected (0.00 sec) mysql> set global auto_increment_offset=3; Query OK, 0 rows affected (0.00 sec) mysql> \q Bye C:\Users\zuo>mysql Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 19
mysql> create table t2(id int primary key auto_increment,name char(6)); Query OK, 0 rows affected (0.28 sec) mysql> insert t2(name) values -> (‘alex‘), -> (‘egon‘), -> (‘wupeiqi‘); Query OK, 3 rows affected, 1 warning (0.06 sec) Records: 3 Duplicates: 0 Warnings: 1
mysql> select * from t2; +----+--------+ | id | name | +----+--------+ | 1 | alex | | 3 | egon | | 5 | wupeiq | +----+--------+ 3 rows in set (0.00 sec)
mysql> show global variables like ‘auto_inc%‘; +--------------------------+-------+ | Variable_name | Value | +--------------------------+-------+ | auto_increment_increment | 2 | | auto_increment_offset | 3 | +--------------------------+-------+ 2 rows in set (0.00 sec)
因为自增的步长小于自增的初始值,所以自增的初始值被忽略!
示例 2:
针对单个表的设置:
mysql> create table t3(id int primary key auto_increment,name char(4))auto_increment=10; Query OK, 0 rows affected (0.26 sec) mysql> insert t3(name) values -> (‘egon‘), -> (‘alex‘); Query OK, 2 rows affected (0.06 sec) Records: 2 Duplicates: 0 Warnings: 0 mysql> select * from t3; +----+------+ | id | name | +----+------+ | 10 | egon | | 11 | alex | +----+------+ 2 rows in set (0.00 sec)
3 foreign key 外键
外键具有保持数据完整性和一致性的机制,目前MySQL只在InnoDB引擎下支持。
B 存在外键 b_f_k,以 A 表的 a_k 作为参照列,则 A 为主表,B 为从表,A 中某记录更新或删除时将会联动 B 中外键与其关联对应的记录做更新或删除操作。
举个例子:员工表、部门表。关系是:一个员工属于一个部门,一个部门有多个员工在这里,员工表中的部门编号就是外键。部门表是主键表(主表)员工表是外键表(从表)。
示例:
先创建主表,然后创建从表。
主表中的操作(删除,修改)会影响到从表。
create table dep(id int primary key auto_increment,name char(7)); Query OK, 0 rows affected (0.26 sec) mysql> insert dep(name) values -> (‘IT‘), -> (‘xx‘), -> (‘yy‘); Query OK, 3 rows affected (0.06 sec) Records: 3 Duplicates: 0 Warnings: 0 mysql> create table emp(id int primary key auto_increment,name char(6),dep_id int, foreign key(dep_id) references dep(id) on delete cascade on update cascade); Query OK, 0 rows affected (0.26 sec) mysql> insert emp(name,dep_id) values -> (‘egon‘,1), -> (‘alex‘,1), -> (‘wupeiqi‘,1), -> (‘sb‘,2); Query OK, 4 rows affected, 1 warning (0.05 sec) Records: 4 Duplicates: 0 Warnings: 1 mysql> select * from emp; +----+--------+--------+ | id | name | dep_id | +----+--------+--------+ | 1 | egon | 1 | | 2 | alex | 1 | | 3 | wupeiq | 1 | | 4 | sb | 2 | +----+--------+--------+ 4 rows in set (0.00 sec) mysql> show create table emp; +-------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | Table | Create Table | +-------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | emp | CREATE TABLE `emp` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` char(6) DEFAULT NULL, `dep_id` int(11) DEFAULT NULL, PRIMARY KEY (`id`), KEY `dep_id` (`dep_id`), CONSTRAINT `emp_ibfk_1` FOREIGN KEY (`dep_id`) REFERENCES `dep` (`id`) ON DELETE CASCADE ON UPDATE CASCADE ) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8 | +-------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ 1 row in set (0.00 sec) mysql> delete from dep where id =1; Query OK, 1 row affected (0.06 sec) mysql> select * from emp; +----+------+--------+ | id | name | dep_id | +----+------+--------+ | 4 | sb | 2 | +----+------+--------+ 1 row in set (0.00 sec)
数据库操作之——约束
标签:查看 comm date 记录 cti innodb str lex oba