MySQL——カラム属性、クエリーselect、ファジイクエリー、集約関数
7297 ワード
----------------------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 '__ %';