简单SQL语句,从删库到跑路

创建数据库:
Create database 数据库名
删除数据库:
drop database 数据库名
创建表:
Create table 表名(字段1 primary key auto_increment,字段2,字段3)字段包含:字段1为主键:(字段 类型 标识符为primary key,auto_increment为自动增长)
删除表:
drop table 表名

一对一关系:
创建表:
表1名:county
create table county *(id int primary key auto_increment ,name cahr(10),language char(10));
表2名: president
create table president(id int primary key auto_increment,name char(10),sex char,f_country_id int);
为president表添加外键约束,这个外键的值来自于country表中的主键
Alter table president add constraint foreign key(f_country_id)references country(id)on delete cascade;
on delete cascade:删除country时删除相应的president
on delete set null:删除country时删除相应的president为null
on delete no action:如果country中某条记录被president指向,那么删除country报错,即不能删除。
注:on delete cascade 表示class表中的记录删除时,stu2表中的外键相关联的记录也会被删除

一对多的关系:
1、 创建class表
create table class(classname char(10),primary key,headteacher char(10));
2、 创建stu2表
Create table stu2(num int primary key auto_increment,name char(10),age int,f_classname char(10));
alter table stu2 add constant foreign key(f_classname)reference class(classname)on delete set null;
注:delete set null 表示class表中的记录删除时,stu2表中外键相关联的外键值被设为null。


多对多的的关系:
1、 创建teacher表
crete table teacher(id int primary key auto_increment,name char(10));
2、 创建stu表
create table stu(id int primary key auto_increment,name char(10));
3、 创建一个中间表
create table middle(id int primary key auto_increment,f_teacher_id int ,f_stu_id int);
4、 为中间表设置第一外键,这个外键的值来自于teacher表
alter table middle add constraint foreign key(f_teacher_id)references teacher(id)on delete no action;
5、 为中间表设置第二外键,这个外键的值来自于stu表
alter table middle add constant foreign key(f_stu_id)references stu(id) on delete no action;
注:on delete no action表示stu表中的记录不能删除

添加数据:
根据表中的全部字段添加数据
Inset into 表名(字段1,字段2,字段3)values(值1,值2,值3);
这种方式要求添加表中全部的值才可以添加数据
Insert into 表名 values(值1,值2,值3);
根据表中的某些字段添加数据
Inset into 表名(字段2)values(值2);

删除数据
删除字段1的那条记录
Delete from 表名 where 字段1=值1;
删除字段1并且字段2=值2的那条记录
Delete form 表名 where 字段1=值1and 字段2=值2;
删除字段1=值2或者字段1=值3的两条记录
Delete form 表名where 字段1= 值2 or 字段1= 值3;
删除字段1=值1以及字段1=值3的两条记录
Delete from 表名 where 字段1 in(值1,值3);
删除id不等于1、2、3的记录;
Delete from 表名 where id not in(1,2,3);
删除id>35的所有记录,不包括35
Delete from 表名 where id>35;
删除id>30并且id<34的记录,不包括30、34
Delete from 表名 where id>30 and id<34;
删除id<2或者id>35的所有记录,不包括2、35
Delete from 表名 where id<2 or id>35;

修改数据库
将sex=’m’的全部记录修改为’男’
Update 表名 set sex=’男’where sex=’m’;
将id=30那条记录的sex改为‘女’
Update 表名 set sex=’女’where id=30;
将id=29那条记录的cardno改为‘2016’,name改为‘(●’◡’●)’
Update 表名 set cardno=‘2016’,name=‘(●’◡’●)’where id=29;

查询数据
查询表里的所有数据
Select * from 表名;
查询student表中id>20的所有记录的数据
Select * from student where id>20;
查询表中所有字段2的信息
Select 字段2 from 表名;
查询表中字段2,字段4,字段6的信息
Select 字段2,字段4,字段6 from 表名;
查询id<10的name,sex,elective的字段信息
Select name,sex,elective from 表名 where id<10;

多表查询语句
1、 查询各班的学生信息:笛卡儿积
方式1:
select t_class.C_NAME,t-stu.S_NAME,t_stu_S_SEX,t_stu.S_MONEY
from t_class,t_stu;
方式2:
select c.C_NAME,s.S_NAME,s.S_SEX,s.S_MONEY
from t_class c,s_stu s

2、 查询各班的学生信息,当t_stu表中的C_ID等于t_class表中的C_ID时,表示该班级的学生
select c.C_NAME,s.S_NAME,s.S_SEX,s.S_MONEY
from t_class c,t_stu s
where c.C_ID=s.C_ID

3、 查询班级名称、学生姓名、性别、缴费、相同班级的要放在一起,姓名根据字典顺序排列。
select c.C_NAME,s.S_NAME,s,s.S_SEX,s.S_MONEY
from t_class c,t_stu s
where c.C_ID=s.C_ID
order by c.C_ID,s.S_ID

4、 查询各班的班级名称和人数(通过班级名称进行分组查询)
select c.C_NAME,count(*) as 人数
from t_class c,s_stu s
where s.C_ID=c.C_ID
group by c.C_ID

查询各班名称和人数,但人数必须小于2,人数多的放在前面
注:group by 用于分组,
Having用于分组后进行条件过滤,
Order by用于选取相应的字段进行排序,
desc表示倒序
查询没有人员的班级
注:distinct表示返回表中不同记录的条数,就是返回不同记录的字段值
select * from t_class c
where c.C_ID not in(select distinct s.C_ID from t_stu s where s.S_ID>3);
等价于:
select * from t_class c where c.C_ID not in(21);


分组查询语句:
根据name字段进行分组查询,并统计每一组的人数
select count(*) from student group by name;

根据name字段查询,并得到每一个组的人数
select name,count(*)as 人数 from student group by name;

根据name字段查询,并得到每一组的人数以及每一组中id的最大值
select name,count(*) as 人数, max(id) from student group by name;

根据sex字段进行查询,并统计每一组的人数
select sex,count(*) as 人数,min(id) from student group by sex;

根据sex进行分组查询,并只对id等于3、28、30的3条记录进行分组查询
select sex,count(*)from student where id in(3,28,30) group by sex;
根据sex进行分组查询,并只对id>20的所有记录进行分组查询
select sex ,count(*)from student where id>20 group by sex desc;
  • 0
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值