当前位置:   article > 正文

Mysql数据库标签总结_数据库标记名称

数据库标记名称

– 创建数据表关键字 create table
create database if not exists itheima;

– 选择数据库
use itheima;

create table if not exists student(
id int unsigned primary key auto_increment comment ‘学号’,
name varchar(32) not null comment ‘姓名’,
gender char(1) default ‘0’ comment ‘性别’,
)default charset=utf8;
– 创建表名 student
– 字段名分别为 id name gender
– 常规字符 int
– 设置字段数据类型是无符号的 unsigned
– 设置主键 唯一标识表中的某一条记录 primary key
– 表示自动增长 每增加一条记录 该字段会自动加1 auto_increment
– 表示注释内容 comment
– 表示可变长度的字符串 最多保存32个字符 varchar(32)
– 表示该字段不允许出现null值 not null
– 枚举类型 其值只能男或女 enum(‘男’,‘女’)
– 设置字段的默认值 default
– 设置该表的默认字符编码为 default charset=utf8

– 查看数据库中已有的表
show tables;

– 查看指定表的字段信息
desc student;

– 查看指定表的某一列信息
desc student name;

– 查看数据表的创建语句和字符编码
show create table student\G

– 查看表的结构
show columns from student;

– 添加字段
alter table student add area varchar(100);

– 修改字段名称
alter table student change area desc char(50);

– 修改字段类型
alter table student modify desc varchar(255);

– 删除指定字段
alter table student drop desc;

– 修改数据表名称
alter table student rename stu;

– 将名字为stu的表重命名为student
rename table stu to student;

– 查询所有字段
select * from t_student;

– 查询指定字段
select stu_name from t_student;

– 为指定字段添加别名
select stu_name as nema from t_student;

– 条件查询
select * from t_student where sex=‘女’;
select studentld,sex,age from t_student where stu_name = ‘杨朝’;

– 查询id等于2的学生信息
select * from student where id=2;

– 查询id为2或3的学生信息
select * from student where id in(2,3);

– 查询名字以y结尾的学生信息
select * from student where name like ‘%y’;

– 将查询结果按照名字升序排序
select * from student ORDER BY name ASC;

– #按性别查询男女各有多少人
select gender,count(*) from student group by gender;

– 查询结果从第二个开始 至多有两个
select * from student limit 1,2;

– 删除数据
#delete from student where gender = ‘女’; – 删除部分数据

#delete from student; – 删除全部数据

#truncate student; – 清空数据表
– delete和truncate的区别是 前者可以加上where字句,只删除满足条件的部分记录 再次向表中添加记录时,不影响自动增长值
– 而后者只能用于清空表中的所有记录 且再次向表中添加记录时 自动增加字段的默认初始值将重新有1开始。

– from 用于指定待查询的数据表
– where 用于指定查询条件
– in 关键字用于判断某个字段的值是否在指定集合中
– like 用于模糊查询 %表示一个或多个字符
– order by 用于将查询结果按照指定字段进行排序
– asc 表示升序
– desc 表示降序
– limit 用于限定查询结果
– group by 用于按照指定字段进行分组查询

#合并连接
select cid,cname from category where cid = 1
union all
select cid,aname from article where cid = 1;

#交叉连接
select * from category c cross join article a where c.cid = a.cid;
– 等价于下面语句
select * from category c,article a where c.cid = a.cid;

#内链接inner join … on … 内链接(内链接只查匹配行,所以内链接会丢失数据)
select * from category c inner join article a on c.cid = a.cid;

#on 和 where的区别
#都是用于连接查询条件,
#on:用于过滤俩表连接的条件
#where:用于过滤中间表的 记录数据

#外连接
#左外连接

select * from category c left join article a on a.cid = c.cid;

#右外连接
select * from article a right join category c on a.cid = c.cid;

#全外连接
select * from category c left join article a on a.cid = c.cid
union
select * from category c right join article a on a.cid = c.cid;

– 子查询

select * from article where cid in (select cid from category where cname in(‘诗’,‘歌’,‘新闻’));

select * from article where cid in (select cid from category where cname not in(‘歌’,‘诗’));

update article set aname = ‘哇莎卡’
where cid = 9 and exists (select cname from category where cid = 5);

any (some) 和all 都用于子查询,any表示诗任何一个,all表示所有
select * from article where cid>any(select cid from category where cname in(‘新闻’,'诗 '));

select *from article where cid >all(select cid from category where cname in(‘新闻’,'诗 '));

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/一键难忘520/article/detail/855298
推荐阅读
相关标签
  

闽ICP备14008679号