后台sql语句,表关系,linus易出错知识点(个人笔记随笔)

161 阅读1分钟

Sql语句

数据库的sql

Create database ×××;
Show databases;
Drop database ×××;

数据表的sql

Use ×××;  //使用这个数据库
建表
Create table stu(id int  primary key auto_increatement,name vachar(20));
删除表
Drop table ×××;
删除表的数据
Delete from ×××;
显示表的结构
Desc ×××;
更新表
添加列:Alter table ××× add address vachar(25);
修改列并添加约束
Alter table ××× modify address varchar(25) unique not null;

数据的sql

增加
Insert into ××× values(null,”xiaoming”,18);
删除
Delete from ××× where id = 1;
更新
Update ××× set age = 28 where id = 1;
查询
Select * from ×××;
Select * from ××× where id = 1;
Select count(id) from ×××;
Select * from ××× where name like “%张%”;
分页
Select * from ××× limit 0,5;
分组
Select * from ××× group by category;

表关系

一对多
Category(id) & product(cid)
添加一种约束  跟哪个表建立关系 谁是外键  指向谁
Alter table product add constraint category_FK foreign key(cid)   references category(id);

多表关系的查询
子查询
Select * from product where cid = (select * from category where name = “手机“ );

内连接 查两张表                                  对等条件
隐式:Select * from category c,product p where c.name = ”手机” and c.id = p.cid;
显示:Select * from inner join product p on c.name = “手机” and c.id = p.cid;

外连接   记一个就行了
左外:select * from category c left join product p on c.id = p.cid;

linus易出错知识点

编辑文档
Vim aa.txt --->i------>esc---->:q或者:wq

删除
Rm -rf aa.txt 

将aa.txt压缩到bb.txt
Tar -zcvf aa.tar.gz aa.txt bb.txt

解压到当前文件夹
Tar -zxvf aa.tar.gz

解压到~~~文件夹
Tar -zxvf aa.tar.gz -C 路径