Web数据库SQL命令,数据库创建_增删改查_表与表之间的关联

2 篇文章 0 订阅
1)创建数据库
create database mytest1;

2)删除数据库
drop database mytest1;

3)创建表
 create table stu(StudyNo int primary key auto_increment,
         IdCarNo int,
                Name char(10),
                Sex char(10),
                Elective char(10));

 4)创建一对一表( 两个表关联,副表的主键必须是主表的外键才可以。
 create table country(id int primary key auto_increment,
                    Name char(10),
                    Language char(10));
create table President(id int primary key auto_increment,
                      Name char(10),
                       sex char,
                       f_country_id);
 alter table President add constraint  foreign key(f_country_id) references country(id) on delete cascade; //该语句红色前面部分是将附表中的外键与主表中的外键相关联,红色部分表示删除主表中的记录会影响到删除副表中的相应数据,删除副表中数据不会影响主表中的相应数据。

创建表:

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时删除相应的presidentnull

on delete no action:如果country中某条记录被president指向,那么删除country报错,即不能删除。

注:on delete cascade à表示class表中的记录删除时,stu2表中的外键相关联的记录也会被删除

 



5)创建一对多表
create table class(id int primary key  auto_increment,
                  classname char(10),
                  HederTeacher char(10));
create table stu(id int primary key auto_increment,
                  name char(10),
                   age int,
                   f_class_id);
   alter table stu add constraint foreign key(f_class_id) references class(id) o n delete set null; //红色部分表示删除主表的主键后, 副表相关联的部分用null表示 。删除副表不会影响主表。

一对多的关系:

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



  6)创建多对多表
create table teacher_stu_middle(id int primary key auto_increment,
                              f_Teacher_id int,
                              f_Stu_id );
create table Teacher(id int primary key auto_increment,
                    name char(10));
create table Stu(id int primary key auto_increment,
                 name char(10));
   alter table teacher_stu_middle add constraint foreign key(f_Teacher_id)references Teacher(id) on delete no action;
   alter table teacher_stu_middle add constraint foreign key(f_Stu_id)references Stu(id)  on delete no action; //红色部分表示 主表不能删除与附表有任何关联的记录 ,可以删掉附表中的任意记录。

多对多的的关系:

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 foreign key(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表中的记录不能删除

 



7)字段约束
主键字约束:primary key
外键字约束:foreign key
唯一性约束:unique
非空约束:not null
检查约束:check(mysql数据库对该约束不起作用)
缺省约束:default

8)数值类型
int、float、double
字符串类型:char(只能存储一个字符)
char(M)(能存储M个字符)( 优点:效率高,缺点:占用内存多
varchar(M)(根据内容决定,但不超过M)( 优点:占用内存少,缺点:效率低



9)数据库增加条目
 insert into stu(id,name,sex,age)values(13,’小李’,’男’,20); //根据添加的顺序依次添加
 insert into stu values(14,’小李’,’男’,34); //必须把所有字段都添加

根据表中的全部字段添加数据

Inset into 表名(字段1,字段2,字段3)values(值1,值2,值3);

这种方式要求添加表中全部的值才可以添加数据

Insert into 表名 values(值1,值2,值3);

根据表中的某些字段添加数据

Inset into 表名(字段2)values(值2);



10)数据库删除条目
     delete from stu where id=2;
     delete from stu where id=2 and sex=’女’; //and表示都满足的意思
delete from stu where id=2 or sex = ‘男’; //or表示满足其中之一都可以
delete from stu where id in(25,26); //in表示删除id=25或者26
delete from stu where id not in(25,26) //not in 表示删除除了id=25或者26的其它记录。

删除字段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不等于123的记录;

Delete from 表名 where id not in(1,2,3);

删除id>35的所有记录,不包括35

Delete from 表名 where id>35;

删除id>30并且id<34的记录,不包括3034

Delete from 表名 where id>30 and id<34;

删除id<2或者id>35的所有记录,不包括235

Delete from 表名 where id<2 or id>35;



11)数据库修改条目
update stu set sex=’男’where age>10 and age<20;
update stu set sex=’男’,name=’小李’where id=’2’; //修改多个用逗号分隔

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<10namesexelective的字段信息

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; 



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值