《MysqL入门MysqL学习笔记之数据定义表约束,分页方法总结》要点:
本文介绍了MysqL入门MysqL学习笔记之数据定义表约束,分页方法总结,希望对您有用。如果有疑问,可以联系我们。
MysqL数据库本文实例讲述了MysqL学习笔记之数据定义表约束,分页方法.分享给大家供大家参考,具体如下:
MysqL数据库1. primary key 主键
MysqL数据库特点:主键是用于唯一标识一条记录的约束,一张表最多只能有一个主键,不能为空也不能重复
MysqL数据库create table user1(ID int primary key,name varchar(32));MysqL> insert into user1 values(1,'hb');query OK,1 row affected (0.10 sec)MysqL> insert into user1 values(1,'hb');ERROR 1062 (23000): Duplicate entry '1' for key 'PRIMARY'MysqL> insert into user1 (name) values('hb');ERROR 1364 (HY000): FIEld 'ID' doesn't have a default value
MysqL数据库2. auto_increament 自增长
MysqL数据库MysqL> create table user2(ID int primary key auto_increment,name varchar(34));MysqL> insert into user2 (name ) values ("name1");query OK,1 row affected (0.09 sec)MysqL> insert into user2 (name ) values ("name2");query OK,1 row affected (0.05 sec)MysqL> insert into user2 (name ) values ("name3");query OK,1 row affected (0.13 sec)MysqL> select * from user2;+----+-------+| ID | name |+----+-------+| 1 | name1 || 2 | name2 || 3 | name3 |+----+-------+
MysqL数据库3. unique 唯一约束
MysqL数据库特点:表的某列值不能重复,可以添加重复的NulL
MysqL数据库create table user3(ID int primary key auto_increment,name varchar(34) unique);MysqL> create table user3(ID int primary key auto_increment,name varchar(34) unique);query OK,0 rows affected (0.39 sec)MysqL> insert into user3 (name ) values ("name3");query OK,1 row affected (0.11 sec)MysqL> insert into user3 (name ) values ("name3");ERROR 1062 (23000): Duplicate entry 'name3' for key 'name'
MysqL数据库允许插入null,并且可以多个
MysqL数据库MysqL> insert into user3 (name ) values (null);query OK,1 row affected (0.12 sec)MysqL> insert into user3 (name ) values (null);query OK,1 row affected (0.12 sec)MysqL> select * from user3;+----+-------+| ID | name |+----+-------+| 3 | NulL || 4 | NulL || 1 | name3 |+----+-------+
MysqL数据库4. not null
MysqL数据库MysqL表的列默认情况下可以为null,如果不允许某列为空则可以使用not null说明
MysqL数据库create table user4 (ID int primary key auto_increment,name varchar(32) not null);MysqL> insert into user4 (name) values(null);ERROR 1048 (23000): Column 'name' cannot be null
MysqL数据库5. foreign key 外键
MysqL数据库从理论上说先建立主表,再建立从表
MysqL数据库雇员表:
MysqL数据库create table dept(ID int primary key,name varchar(32));
MysqL数据库部门表:
MysqL数据库create table emp(ID int primary key,name varchar(32),deptID int,constraint myforeignkey foreign key(deptID) references dept(ID));MysqL> select * from dept;+----+-------+| ID | name |+----+-------+| 1 | name1 |+----+-------+1 row in set (0.00 sec)MysqL> insert into emp values(1,'aaa',1);query OK,1 row affected (0.22 sec)MysqL> insert into emp values(1,2);ERROR 1062 (23000): Duplicate entry '1' for key 'PRIMARY'MysqL> insert into emp values(1,null);ERROR 1062 (23000): Duplicate entry '1' for key 'PRIMARY'MysqL> insert into emp values(2,null);query OK,1 row affected (0.13 sec)MysqL> select * from emp;+----+------+--------+| ID | name | deptID |+----+------+--------+| 1 | aaa | 1 || 2 | aaa | NulL |+----+------+--------+2 rows in set (0.00 sec)
MysqL数据库总结:
MysqL数据库① 外键只能指向主表的主见列或者unique
② 外键的数据类型应该与它指向的列类型一致
③ 外键的值:NulL 或者 指向列中存在的值
④ 外键可以指向本表的主键列或者unique
MysqL数据库MysqL 不支持check
MysqL数据库create table user99(age int check(age>13));MysqL> create table user99(age int check(age>13));query OK,0 rows affected (0.19 sec)MysqL> insert into user99 values(99);query OK,1 row affected (0.04 sec)MysqL> select * from user99;+------+| age |+------+| 99 |+------+
MysqL数据库MysqL 分页
MysqL数据库基本语法:
MysqL数据库select * from 表明 where 条件 limit 从第几条取,取出几条
MysqL 是从第0条开始取数据
MysqL数据库MysqL> select * from student;+------+--------+---------+---------+------+| ID | name | chinese | english | math |+------+--------+---------+---------+------+| 1 | 张小明 | 89 | 78 | 90 || 2 | 李进 | 67 | 98 | 56 || 3 | 王五 | 87 | 78 | 77 || 4 | 李一 | 88 | 98 | 90 || 5 | 李来财 | 82 | 84 | 67 || 6 | 张进宝 | 55 | 85 | 45 || 7 | 张小明 | 75 | 65 | 30 |+------+--------+---------+---------+------+7 rows in set (0.05 sec)MysqL> select * from student limit 2,2;+------+------+---------+---------+------+| ID | name | chinese | english | math |+------+------+---------+---------+------+| 3 | 王五 | 87 | 78 | 77 || 4 | 李一 | 88 | 98 | 90 |+------+------+---------+---------+------+2 rows in set (0.00 sec)
MysqL数据库按照语文成绩排序,查处第3条到第5条
MysqL数据库MysqL> select * from student order by chinese desc limit 3,2;+------+--------+---------+---------+------+| ID | name | chinese | english | math |+------+--------+---------+---------+------+| 5 | 李来财 | 82 | 84 | 67 || 7 | 张小明 | 75 | 65 | 30 |+------+--------+---------+---------+------+2 rows in set (0.00 sec)
MysqL数据库扩展,分页:pageNow,pageSize
MysqL数据库select * from 表明 where 条件 [group by … having … order by …]limit 从第几条取,取出几条
select * from 表明 where 条件 [group by … having … order by …]limit (pageNow-1)*pageSize,pageSize
MysqL数据库更多关于MysqL相关内容感兴趣的读者可查看本站专题:《MysqL索引 *** 作技巧汇总》、《MysqL日志 *** 作技巧大全》、《MysqL事务 *** 作技巧汇总》、《MysqL存储过程技巧大全》、《MysqL数据库锁相关技巧汇总》及《MysqL常用函数大汇总》
MysqL数据库希望本文所述对大家MysqL数据库计有所帮助.
总结以上是内存溢出为你收集整理的Mysql入门MySQL学习笔记之数据定义表约束,分页方法总结全部内容,希望文章能够帮你解决Mysql入门MySQL学习笔记之数据定义表约束,分页方法总结所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)