mysql命令 详细整理,web开发教学


在设计表里边的

外键设置为 setnull 就可以删除外键关联的id

外键设置为 restrict 和 no action 都不能删除外键

DELETE from classes WHERE id=2;

———————————-

CASCADE:父表delete、update的时候,子表会delete、update掉关联记录;

SET NULL:父表delete、update的时候,子表会将关联记录的外键字段所在列设为null,所以注意在设计子表时外键不能设为not

null;

RESTRICT:如果想要删除父表的记录时,而在子表中有关联该父表的记录,则不允许删除父表中的记录;

NO ACTION:同 RESTRICT,也是首先先检查外键;

——————————– 查询

在stus表里面查询id ,name ,clsid

SELECT

stus.id,

stus.name,

stus.clsid

FROM

stus

LEFT JOIN classes ON stus.clsid = classes.id

WHERE

classes.id = 1

视图 create view 计划 到达某个点执行一批任务

==================================== 聚合命令(去重[distinct]),取总数[count],平均数[avg],取和[sum]等) 分页查询(limit) distinct

去重 ORDER BY 语句用于根据指定的列对结果集进行排序。 LIKE 操作符用于在 WHERE 子句中搜索列中的指定模式。 SQL

通配符必须与 LIKE 运算符一起使用。通配符(%或者_)

%a:选取a结尾的 IN 操作符允许我们在 WHERE 子句中规定多个值。

id in (1,2,3); join 用于根据两个或多个表中的列之间的关系,从这些表中查询数据。

JOIN: 如果表中有至少一个匹配,则返回行

LEFT JOIN: 即使右表中没有匹配,也从左表返回所有的行

RIGHT JOIN: 即使左表中没有匹配,也从右表返回所有的行

FULL JOIN: 只要其中一个表中存在匹配,就返回行


show databases;

create database students;

drop database 数据库名;

use students;

show tables;

create table users(

    id int,

    name varchar(10),

    age int

);

create table users(

    id int NOT NULL DEFAULT 'no name' COMMENT '用户名称',

    name varchar(10),

    age int

);

drop table 数据库表名;

show tables;

select * from users;

describe users;



insert into users(id,name,age) value(1,"张三",90);

insert into users(id,name,age) value(2,"李四",90);

insert into users(id,name) value(3,"abef");

insert into users(id,name) value(4,"lioi");

insert into users(id,name) value(5,"lysi");

insert into users(id,name) value(6,"laD");

insert into users(id,name) value(7,"lra");

insert into users(id,name) value(8,"lra");

#修改编号为4

update users set id=4 where name='wfcd';

select * from users;

delete from users where id=0;



select name from users where id=2;

select id,name from users where id=1;

select * from users where id=2 and name="zs";

select * from users where id=2 or name= 'zs'

select * from users;

#查找包含a

select * from users where name like '%a%';

#查找a开头

select * from users where name like 'a%';

#查找a结尾

select * from users where name like '%a';

#不等于

select * from users where id != 1;

select * from users where id <> 1;

select * from users where id in(1,3,5);

#超过2个以上用in,

select * from users where name in('zs','ls');

select id as '编号',name as '姓名' from users;

select count(*) from users; 8

select count(id) from users; 8

select count(name) from users; 8

select count(distinct name) from users; 7

#去重复不能包含唯一的列

select distinct name from users; 

#求平均

select avg(id) from users;

#求和

select sum(id) from users;

#排序 默认正序

select * from users order by id zsc;

#倒序

select * from users order by id desc;

select * from users order by id ,name desc;

#ascII排序

select * from users order by name;

select * from users where name like 'a%' order by name;

select * from users where name like 'a%' order by name desc;



#分组查询

select * from users group by age;

select age ,count(*) as '人数' from users group by age;

select age ,name,count(*) as '人数' from users group by age,name;

#id不能大于5

select age ,name,count(*) as '人数' from users group by age,name having name in ('zs','ls');

#只想让张三李四分组

select age ,name,count(*) as '人数' from users group where name in('zs','ls')by age,name



select * from users limit 0,4;



最后

MySQL,web开发,命令行,SQL语句
MySQL,web开发,命令行,SQL语句

由于文档内容过多,为了避免影响到大家的阅读体验,在此只以截图展示部分内容

 CodeChina开源项目:【大厂前端面试题解析+核心总结学习笔记+真实项目实战+最新讲解视频】