Mysql問題まとめ&学習記録3:テーブル間の関係とレコードの追加・削除・変更

MySQL 学習記録 3

テーブルの関係:

1.多対一

2. 多対多

3. 1対多

1.多対一

アソシエーションテーブルと関連テーブル、外部キーを作成するだけです

create table press(
    id int primary key auto_increment,
    name varchar(20)
);

create table book(
    id int primary key auto_increment,
    name varchar(20),
    press_id int not null,
    foreign key(press_id) references press(id)
    on delete cascade
    on update cascade
);

insert into press(name) values
('出版社1'),
('出版社2'),
('出版社3');

insert into book(name,press_id) values
('饺子',1),
('包子',2),
('馄饨',2),
('玉米',3),
('馒头',3);
2. 多対多

2 つのテーブル間の関係を確立するには、中間テーブルを確立する必要があります。

#先建立book表(1中已建立),author表如下
create table author(
    id int primary key auto_increment,
    name varchar(20)
);

#建立两表格的关系
create table author2book(
    id int not null unique auto_increment,
    author_id int not null,
    book_id int not null,
    constraint fk_authon foreign key(author_id) references author(id)
    on delete cascade
    on update cascade,
    constraint fk_book foreign key(book_id) references book(id)
    on delete cascade
    on update cascade,
    primary key(author_id,book_id)
);

#插入author,id依次排开
insert into author(name) values('dumpling'),('corn');

#每个作者的代表作
dumpling:
饺子
包子
玉米
馄饨
馒头
corn:
玉米
馒头

insert into author2book(author_id,book_id) values
(1,6),(1,7),(1,8),(1,9),(1,10),(2,9),(2,10);

#查看对应关系,只需查看关联表即可。
3. 1対1

外部キー + 一意

create table customer(
    id int primary key auto_increment,
    name varchar(10) not null,
    qq varchar(10) not null,
    phone char(16) not null
);

create table student(
    id int primary key auto_increment,
    class_name varchar(20)not null,
    customer_id int unique,#该字段必须唯一
    foreign key (customer_id) references customer(id)#外键的字段必须保证unique
    on delete cascade
    on update cascade
);

insert into customer(name,qq,phone) values
('饺子','9218127214',19827191228),
('包子','9218123144',19241121238),
('汤圆','9218231145',11298319412);

insert into student(class_name,customer_id) values
('BB1班',1),
('AA2班',3);

記録の追加、削除、変更

データの挿入 insert : 前の値の使用法に加えて、別の値の使用法があります。

insert into 表名(字段1,字段2,...,字段n)
select(字段1,字段2,...,字段n) From 表2
where...;

アップデートデータ更新

update 表名 set
	字段1 = 值1,
	字段2 = 值2,
	where condition;

#示例:更改密码
	update mysql.user SET password = password('123'),
	where user='root' and host = 'localhost';

**データ削除削除:** 指定した場所にある特定のデータのみを削除します

delete from 表名
	where condition;
	
#示例
	delete from mysql.user
	where password='';

テーブルの切り捨て: テーブル table 内のすべてのレコードを削除します。

**クエリ データの選択: **単一テーブル クエリ、複数テーブル クエリ

単一テーブルクエリ:

library.table から個別の (重複を削除する) フィールド 1、フィールド 2、フィールド 3 を選択します。

​ where 条件

グループ化条件でグループ化

フィルターあり

ソートフィールドで並べ替える

制限 n; 制限

クエリデータ

単一テーブルクエリ:
#简单查询:
select xxx,xx,xxx from xxx;
select * from xxx;

#避免重复
select distinct xx from xxx;#筛去重复项

#通过四则运算查询
select xxx*12 as xxxA from xxx;#可以直接将原数据*12的结果查出来(所得为虚拟表),xxxA为该新数据的表头名称

#定义显示格式
concat()#函数用于连接字符串
select concat('姓名:',name) as xxxB,concat('性别:',sex) as xxxC from xxx;#按照该格式查出name包含的数据,xxxB为标题;
concat_ws()#第一个参数为分隔符
select concat_ws(':',name,sex) as xxxD from xxx;
select concat(name,':',sex) from xxx;#与上一条相同,当数据多的时候使用concat_ws指定分隔符更好
ここで、制約は次のとおりです。

where 句は次のように使用できます。

1. 比較比較演算子: >< >= <= !=

2、80 ~ 100 の間の値 80 ~ 100

3. in(80,90,100) の値は 80、90、または 100 です。

4、「xxx%」のような

5. 論理演算子: 論理演算子 and or not を複数の条件で直接使用できます。

#单条件查询
select xxx from xxx where id>7;

#多条件查询
select xxx from xxx where id>7 and age<10;

#between and

#关键词is null(判断某个字段是否为null不能用等号,需要用is)

#关键词in
select xxx from xxx where age in(28,45,22);#可直接取出这三个age的数据

#关键词like模糊查询(模糊匹配)
#eg:比如姓李的开头:
select * from xxx where name like "李%";#%后面可以是任意字符
select * from xxx where name like "李__";#下划线可指定后面有几个字符

論理的な順序: 最初にから、次にどこから、次に選択します。

グループ化: グループ化

1. グループ化は where の後に発生し、where の後に取得されたレコードに基づいて行われます。

2. グループ化: 従業員情報テーブルの職位のグループ化や性別によるグループ化など、特定の同じフィールドに従ってすべてのレコードを分類します。

3. 各データの種類などのカテゴリを取り出します。最高の給与、特定の部門の従業員の数など。

4. 大前提:任意のフィールドでグループ化することができますが、投稿ごとにグループ化するなどグループ化した後は投稿フィールドのみが表示されるため、グループ内の情報を参照する必要がある場合は集計機能を使用する必要があります。

select * from xxx group by post;
set global sql_mode ="ONLY_FULL_GROUP_BY";#设置分组只能取分组的组名,即
select post from xxx group by post;

#聚合函数
max
min
avg#平均值
sum#和
count#数个数

#eg.每个职位有多少个员工
select post,count(id) as emp_count from employee group by post;

#分组之后,只能取分组的字段,以及每个组聚合结果
#没有group by则默认整体算作一组

group_concat(xxx)#可将xxx的所有数据提取出来eg:
select post,group_concat(name) from employee group by post;
持つ:フィルタリング

1. 実行の優先順位: where>group by>having>selectfield

2. where には任意のフィールドを含めることができますが、集計関数を含めることはできません。

3. グループ化されたフィールドが利用可能 他のフィールドを直接取得することはできない 集計関数が利用可能

並べ替え: 並べ替え

独自のソート方法、自然ソート (1、2、...)

実行優先順位は、distinct の後 (ただし制限の前) です。

select * from employee order by age asc;#默认也为asc(升序)
select * from employee order by age desc;#降序
select * from employee order by age asc,id desc;#先按照age升序,如果一样则按照id降序
制限: 表示される項目の数を制限します。
select * from employee limit 3;#选择出前三条来显示
select * from employee limit 0,5;#从0开始往后数五条
select * from employee limit 5,5;#从5开始往后再数五条
#该方法效率不太高

要約: 文法的順序:

select distinct(去重) 字段1,字段2,字段3 from 库.表 
where 条件
group by 分组条件
having 过滤
order by 排序字段
limit n;限制

実行順序:

def from(db,table):

def where(conditon,f):

def group(lines):
	
def having(group_res):

def distinct(having_res):

def order(distinct_res):

def limit(order_res):

def select():
	#按顺序执行以上函数。
補足: 通常のクエリ

正規表現:

select * from xxx where name like 'jin%';#jin开头的字段
select * from xxx where name regexp'^jin';#

テーブル結合操作

内部結合、左結合、右結合、完全外部結合

#引入,加入逻辑关联(内连接)
select * from employee,department where employee.dep_id = department.id;

内部結合: 2 つのテーブルの共通部分のみを取得します

select * from xxx1 inner join department on xxx1.id1 = xxx2.id2;

左結合: 内部結合に基づいて左テーブルのレコードを保持します。

select * from xxx1 left join department on xxx1.id1 = xxx2.id2;

右結合: 内部結合に基づいて右テーブルのレコードを保持します。

select * from xxx1 right join department on xxx1.id1 = xxx2.id2;

完全外部結合: 内部結合に基づいて対応関係がない左右のテーブルのレコードを保持します。

select * from xxx1 full join department on xxx1.id1 = xxx2.id2;
#这个不行的话可以使用联合左右连接的操作,union
select * from xxx1 right join department on xxx1.id1 = xxx2.id2
union
select * from xxx1 left join department on xxx1.id1 = xxx2.id2;

select ステートメント内の主要なキーワードの定義順序の概要

select文のキーワードの定義順序と論理順序(通し番号が論理順序)
(7)select 
(8)distinct<select list>
(1)from <left_table>
(3)<jion_type>join<right_table>
(2)on<jion_condition>
(4)where<where_condition>
(5)group by<group_by_list>
(6)having<having_condition>
(9)order by<order_by_condition>
(10)limit<limit_number>

おすすめ

転載: blog.csdn.net/weixin_47723114/article/details/131833985