MySql数据库总结

来源:互联网 发布:game dev tycoon mac 编辑:程序博客网 时间:2024/05/29 11:15

主键(Primary Key):在表中的唯一标识,不能重复,但可以用两个字段来作为主键,比如userName和password组合起来作为主键

外键(Foreign Key):这列数据引用了另外一个表的主键

Unique Key(UK):表示该项不能重复,允许一条可以为null

Not Null(NN):不为空

AI:自动增长

UQ:不能重复


最后一行自己设定的是默认的格式

在数据库中字符串是用varchar(45)表示的,45表示字符串大小


desc tablename 查看表结构

select * from mytable查询表中所有列,也即查询整个表结构

select * from mytable limit 3,4表示从该表第三行往下4行的所有数据

select * from mytable order by id 根据id排序

select * from mytable order by id desc根据id反序排序

select * from mytable order by id desc limit 1根据id反序排序并且取第一条

select replace(first name,'d','1') from actor  从actor表中将first name表中的d换成1

select * from categroy where (name='animation' or id=1) and name='new'  当有多个条件优先级易混淆时,最好加上括号避免出错

select * from mytable where id in(1,4,7) 取出mytable中id含有1,4,7的所有数据

select * from mytable where id between 1 and 9 取出1到9之间的数据

select * from mytable where name like 'A%'  取出表中name列中以A开头的所有数据,关键字like和%要注意

select * from mytable where name like 'A_'  取出表中name列中以A开头并且只有2个字母,第二个字母为任意字符

select * from mytable where name like '%A%'  取出表中name列中包含A的数据