Web数据库SQL命令,数据库创建_增删改查_表与表之间的关联
来源:互联网 发布:青岛少儿编程培训班 编辑:程序博客网 时间:2024/06/17 05:40
创建表:
表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 tableclass(classname char(10),primary key,headteacher char(10));
2、 创建stu2表
Create table stu2(numint primary key auto_increment,name char(10),age int,f_classname char(10));
alter table stu2 addconstant 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 intprimary 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 foreignkey(f_teacher_id)references teacher(id)on delete no action;
5、 为中间表设置第二外键,这个外键的值来自于stu表
alter table middle add constraint 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 studentwhere 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:
selectt_class.C_NAME,t-stu.S_NAME,t_stu_S_SEX,t_stu.S_MONEY
from t_class,t_stu;
方式2:
selectc.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时,表示该班级的学生
selectc.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、 查询班级名称、学生姓名、性别、缴费、相同班级的要放在一起,姓名根据字典顺序排列。
selectc.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
wherec.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字段进行分组查询,并统计每一组的人数
selectcount(*) from student group by name;
根据name字段查询,并得到每一个组的人数
selectname,count(*)as 人数 from student group by name;
根据name字段查询,并得到每一组的人数以及每一组中id的最大值
selectname,count(*) as 人数, max(id) from student group byname;
根据sex字段进行查询,并统计每一组的人数
select sex,count(*)as 人数,min(id) from student group by sex;
根据sex进行分组查询,并只对id等于3、28、30的3条记录进行分组查询
selectsex,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;
- Web数据库SQL命令,数据库创建_增删改查_表与表之间的关联
- 4_数据库的增删改查
- android数据库学习_增删改查
- xUtils数据库---增删改查(表与表之间)
- SQL数据库 表的创建和行以及列的增删改查
- android(19)_数据存储与访问_SQLite数据库_使用SQLiteDatabase提供的增删改查方法
- 数据库的创建和sql语句增删改查
- HTML5的Web SQL DataBase本地数据库增删改查
- SQLite数据库创建与增删查改
- Android中数据库的创建与增删改查操作
- FMDB创建数据库的增删改查
- 数据库的增删改查及创建
- C#,sql数据库的增删改查
- My sql数据库的增删改查
- 链表的基本操作之_增删改查
- JSP与数据库的增删改查
- hibernate简单程序,实现从页面对数据库的增删改查(主从表关联)
- 创建数据库与完成数据增删改查 SQLite数据库
- JavaScript中对象写法
- android xml 的metadata的调用
- Redis在win7上的可视化应用
- Android Studio进行单元测试
- Hadoop、Spark、Storm相关组件安装
- Web数据库SQL命令,数据库创建_增删改查_表与表之间的关联
- git commit 运行时提示错误
- 控制器之间的数据传递——storyboard中控制器的数据传递
- POJ1308——并查集
- 先序后序中序非递归算法
- Mysql Long类型时间 当日数据查询
- OpenTSDB安装与使用
- 第二周项目0-宣告“主权”
- 企业战略管理笔记