mysql属性查询_MySQL——列属性、查询select、模糊查询、聚合函数

----------------------MySQL列属性——唯一键(unique)--------------

create database day3 charset=utf8;

use day3;

--unique [key]

--唯一键,保证数据唯一性

--唯一键和主键区别:

--1.主键只有一个,唯一键可以有多个

--2.主键不能重复,不能为空

--3.唯一键不能重复,可以为空(NULL)

--方法一:定义字段时添加

--key可以省略,一个表的唯一键可以有多个

--unique

--unique key

create table `unique` (

stuid int primary key,

stuname varchar(20) unique key,

stuaddr varchar(50) unique

)engine=innodb charset=utf8;

--方法二:单独指定

create table `unique2` (

stuid int primary key,

stuname varchar(20),

stuaddr varchar(50),

unique key (stuname),

unique (stuaddr)

);

--将两个字段(两列)组合为一个唯一键

create table `unique3` (

stuid int primary key,

stuname varchar(20),

stuaddr varchar(50),

unique key(stuname,stuaddr)

);

--方法三:修改字段属性时添加

--alter table add unique

create table `unique4` (

stuid int primary key,

stuname varchar(20),

stuaddr varchar(50)

);

alter table `unique4` add unique key (stuname),add unique (stuaddr);

--删除unique键

--使用index关键字删除unique

alter table `unique4` drop index stuname;

alter table

--添加两个字段组合为一个unique键

--组合unique键在MySQL中存储的index名默认为第一个字段名

alter table `unique4` add unique (stuname,stuaddr);

show create table `unique4`;

--删除组合unique键

alter table `unique4` drop index stuname;

--为组合unique键命名

alter table `unique4` add unique uuu (stuname,stuaddr);

show create table unique4;

--添加值

insert into `unique4` values (1,'aa','aa');

insert into `unique4` values (2,'aa','bb');

-----------------------MySQL列属性——备注(comment)---------------

--comment

create table `comment`(

stuno int primary key comment '学生编号',

stuname varchar(20) comment '学生姓名'

);

-----------------------MySQL列属性——注释---------------

--注释方法1:--

--注释方法2:#

--注释方法3:/**/(多行注释)

-------------------数据完整性--------------------

--1、实体完整性

----a)主键约束

----b)标识列(自动增长列)

----c)唯一约束

--2、域完整性

----a)数据类型约束

----b)非空约束

----c)默认值约束

--3、引用完整性

----a)外键约束

--4、自定义完整性

----a)存储过程

----b)触发器

-------------------主表和从表----------------

--1、主表中没有对应的记录,从表中不允许插入

--2、从表中有的,主表中不允许删除。

--3、先删除从表,再删主表

-----------------------外键(foreign key)---------------------

--foreign key

--外键:从表中的公共字段

--外键用来保证引用完整性

--公共字段的名字可以不一样,但是类型必须是一样的

--外键的操作

--1.严格操作

--2.置空操作(set null)

--3.级联操作(cascade)

--创建外键

--方法一:创建表的时候创建外键

--创建主表

create table `foreign_key` (

stuid int primary key,

stuname varchar(20)

);

--创建从表

create table `foreign_key_2` (

stuno int primary key,

score int,

foreign key(stuno) references `foreign_key`(stuid)

);

show create table `foreign_key_2`;

--删除表时要先删除从表,再删除主表,先删主表会报错

drop table `foreign_key_2`;

drop table `foreign_key`;

--方法二:修改表时创建外键

create table `foreign_key` (

stuid int primary key,

stuname varchar(20)

);

create table `foreign_key_2` (

stuno int primary key,

score int

);

alter table `foreign_key_2` add foreign key (stuno) references `foreign_key`(stuid);

--创建外键的时候指定外键的名字

--constraint 'name'

alter table `foreign_key_2` add constraint `FK1` foreign key(stuno) references `foreign_key`(stuid);

--删除外键

alter table `foreign_key_2` drop foreign key `FK1`;

--级联操作

--当主表主键删除的时候,从表置空,主表更新的时候从表也更新,即级联

--只有innodb的引擎才支持主外键,myisam是不支持的。

--MySQL5.5默认引擎是innodb,低版本默认为myisam的。

--创建主表

create table fk1 (

stuno char(3) primary key,

stuname varchar(20)

);

--创建从表

create table fk2 (

stuno char(3),

stuid int auto_increment primary key,

score int,

foreign key (stuno) references fk1(stuno) on delete set null on update cascade

);

--测试

insert into fk1 values ('001','丁伟韬');

insert into fk1 values ('002','叶利云');

insert into fk1 values ('003','孙峰');

insert into fk2 values ('001',1,99);

insert into fk2 values ('002',2,88);

insert into fk2 values ('003',3,77);

delete from fk1 where stuno='001';

update fk1 set stuno='200' where stuno='002';

-------------------查询语句--select-------------

/*stu测试数据*/

create table stu

(

stuNo char(6) primary key,

stuName varchar(10) not null,

stuSex char(2) not null,

stuAge tinyint not null ,

stuSeat tinyint not null,

stuAddress varchar(10) not null,

ch tinyint,

math tinyint

);

insert into stu values ('s25301','张秋丽','男',18,1,'北京',80,null);

insert into stu values ('s25302','李文才','男',31,3,'上海',77,76);

insert into stu values ('s25303','李斯文','女',22,2,'北京',55,82);

insert into stu values ('s25304','欧阳俊雄','男',28,4,'天津',null,74);

insert into stu values ('s25305','诸葛丽丽','女',23,7,'河南',72,56);

insert into stu values ('s25318','争青小子','男',26,6,'天津',86,92);

insert into stu values ('s25319','梅超风','女',23,5,'河北',74,67);

--select

--选择并显示

select 10;

select 10*10;

--显示时间戳

select unix_timestamp();

--显示随机数

select rand();

--as

--as关键字用来给字段取别名

select 10*10 as total;

select ch,math,ch+math as total from stu;

--as 可以省略

select 10*10 total;

select ch+math total from stu;

/*测试笛卡尔积数据*/

create table stu_info(

name varchar(10),

sex char(1)

);

create table stu_marks(

ch tinyint,

math tinyint

);

insert into stu_info values ('tom','男'),('berry','女');

insert into stu_marks values (11,11),(22,22);

--from

--from后面跟的是数据源,数据源可以有多个,返回的是笛卡尔积

select * from stu_info,stu_marks;

--*

--*查询所有字段

select * from stu;

--table.key

--明确某个表的字段

--select 字段1,字段2 from 表1,表2

--查询部分字段

--dual表

--dual表是个伪表,用来保证select语句的完整

--有些情况下不能省略from,但是又没有确切的表,这时候就用dual伪表

select 10*10 from dual;

--where

--where是在数据源中进行筛选

--查询的结果是一张表,(这张表的结构可能与数据库表的结构不一样)

select * from stu where stuage>25;

select stuName,ch+math as '总分' from stu where stuSex='男';

--1表示真,显示所有数据

select * from stu where 1;

--0表示false,查询不到记录

select * from stu where 0;

--子查询

--在查询结果的表中继续查询

--高级查询

--is null

--is not null

--查询值非空/不为空的数据

select * from stu where ch is null or math is null;

select * from stu where ch is not null and math is not null;

--in

--not in

--表示在/不在某个范围内

select * from stu where stuaddress='北京' or stuaddress='上海';

select * from stu where stuaddress in ('北京','上海');

select * from stu where stuaddress not in ('北京','上海');

--between...and...

--not between...and...

select * from stu where stuage>=20 and stuage <=25;

select * from stu where stuage between 20 and 25;

select * from stu where stuage not between 20 and 25;

---------------------------聚合函数---------------

--sum

--sum(key)

select sum(ch) as '语文总分' from stu ;

--avg

--avg(key)

select avg(math) as '数学平均分' from stu;

--max

--max(key)

select max(stuage) as '学生中的最大年龄' from stu;

--min

--min()

select max(ch) as '语文最低分' from stu;

--count

--count()

select count(*) as '总人数' from stu;

------------------------模糊查询---------------------

--通配符

--'_'下划线:表示一个字符

--'%'百分号:表示任意字符

show tables like 's%';

select * from stu where stuname like '李%';

select * from stu where stuname like '__丽%';

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值