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;
//该语句红色前面部分是将附表中的外键与主表中的外键相关联,红色部分表示删除主表中的记录会影响到删除副表中的相应数据,删除副表中数据不会影响主表中的相应数据。
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) on delete set null;
//红色部分表示删除主表的主键后,副表相关联的部分用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;
//红色部分表示主表不能删除与附表有任何关联的记录,可以删掉附表中的任意记录。
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);//必须把所有字段都添加
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不等于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;
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<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;