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:/**/(多行註釋)


-------------------數據完整性--------------------
--一、實體完整性
----a)主鍵約束
----b)標識列(自動增加列)
----c)惟一約束
--二、域完整性
----a)數據類型約束
----b)非空約束
----c)默認值約束
--三、引用完整性
----a)外鍵約束
--四、自定義完整性
----a)存儲過程
----b)觸發器


-------------------主表和從表----------------
--一、	主表中沒有對應的記錄,從表中不容許插入
--二、	從表中有的,主表中不容許刪除。
--三、	先刪除從表,再刪主表


-----------------------外鍵(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 '__麗%';
相關文章
相關標籤/搜索