日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 人文社科 > 生活经验 >内容正文

生活经验

c潭州课堂25班:Ph201805201 MySQL第二课 (课堂笔记)

發布時間:2023/11/27 生活经验 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 c潭州课堂25班:Ph201805201 MySQL第二课 (课堂笔记) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

?

mysql> create table tb_2(
-> id int,
-> name varchar(10) not null
-> );

?

插入數據 insert into tb_2 value(1,'xiaobai'); ? 在非空時,NOT NULL 必須有值,

?

2,在已有的表中設置一個字段的非空約束

mysql> alter table tb_2
-> modify id int not null;

?

取消非空約束

mysql> alter table tb_2
-> modify id int:

?

?

?

mysql> create table t3
-> (id int unique key,
-> name varchar(10);

?

?unique key ?字段不可重復,否則報錯,

?

?

2, 在已有的表中添加唯一約束

方法1

mysql> alter table t3
-> add unique key(name);

?

方法2

alter table t3
-> modify name varchar(10) unique key;

alter table t3 modify id int unique key;

?

刪除唯一

mysql> alter table t3
-> drop key name;

?

主鍵的作用: 可以唯一標識一條數據,每張表里只能 有一個主鍵,

主鍵特性: ?非空且唯一,當表里沒有主鍵時,第一個非空且唯一的列,被當成主鍵,

創建定有主鍵的表

create table t4(
-> id int primary key,
-> name varchar(10));

?

在已有的表中設定主鍵

方法1

> alter table t4
-> add primary key(id);

方法2

> alter?table t4

>modify id int primary key;

?

?

刪除主鍵

mysql> alter table t4
-> drop primary key;

?



?

auto_increment ?自動編號,要與鍵一起使用,一般與主鍵一起使用,一個表里只有一個自增長,

默認情況下起始值為 1,每次的增量為 1,

?

新建個有自增長的表

create table tb5(
-> id int primary key auto_increment,
-> name varchar(10)

->)auto_increment = 100; ? ? 設?auto_increment 從100開始

?

?

插入 name ?的值,

insert into tb5 (name) values('a'),('b'), ('c');

這里的 ID 字段自增長,

?

給已有表添加自增長:

alter table tb5
-> modify id int auto_increment;

?

刪除:

alter table tb5
-> modify id int;

?

?

default 初始值設置,插入數據時,如果沒有給該字段賦值,則會給以默認值,

新建個表

create table tb6(
-> id int primary key auto_increment,
-> name varchar(10),
-> age int not null default 18
-> );

?

插入 name 值 ,這里 id 自增長,age 不傳值則默認18

insert tb6 (name) values('a'),('b'),('c');

?

insert into tb6 set name='qq',age=20;

?

?

?

刪除默認約束

alter table tb6
-> modify age int;

?

給已有的表添加默認約束

alter table tb6 modify age int default 20;

?

?

fofeign key 保持數據一致性完整性,實現一對一,一對多,多對多的關系,

外鍵必須關聯到鍵上,一般是關聯另一個表的主鍵,

因為一個表只存一類信息,所以用鍵來做參照,可以減少數據冗余,

?

建 a 表

create table a(
-> a_id int primary key auto_increment,    唯一性,自增長,
-> a_name varchar(20) not null        非空
-> );  

insert into a values(1,'a1'),(2,'a2');      插入數據

?

建個 b 表

create table b(
-> b_id int primary key,
-> b_name varchar(20) not null,
-> fy_id int not null,
-> constraint AB_id foreign key(fy_id) references a(a_id)
-> );

?

b 表中 fy_id 的字段只能添加 a 表中的 a_id 中已有的數據,

此時 ,a 表 a_id 的數據不能被修改和刪除,

?

刪除外鍵

alter table b drop foreign key AB_id;

?

添加外鍵

alter table `b`
-> add constraint AB_id foreign key(fy_id) references a(a_id);

?

?

通常一個學校可以有很多學生,而一個學生只屬于一個學校,

學校與學生的關系就是一對多的關系,通過外鍵關聯實現,

?

?

創建學院表

create table tanzhou(
-> t_id int primary key auto_increment,     學院 id
-> t_name varchar(10) not null         ?學院名
-> );

?

創建學生表?

create table stu(
-> s_id int primary key auto_increment,    學生 id
-> s_name varchar(10),            學生名
-> tz_id int not null,              所屬學院 id
-> foreign key(tz_id) references tanzhou(t_id)
-> );

?

創建學生詳情表

create table stu_d(
-> id int primary key,
-> age int ,
-> address varchar(20) comment '家庭住址',
-> home_num varchar(20),
-> foreign key (id) references stu(s_id)
-> );

?


創建一張課程表

create table k_c_b(
-> kid int primary key not null,
-> kname varchar(10),
-> ke_shii int
-> );

?


創建選課表

create table x_k_b(
-> xid int primary key not null,
-> kid int unique key,
-> sid int,

->primary key(kid,?sid),     這行為聯合組鍵,意義在同一學生不能選兩次一樣的課程,
-> foreign key(kid) references k_c_b(kid), ? ? ? ?引課程表
-> foreign key(sid) references stu(s_id)    引學生表 
-> );

?

?

?

?

補充

?

?

?

數據類型

tinyint?    1 字節?    -128 --》》127

smallint    2字節     -12768--》12768

int     4字節      -2147483643--》2147483643

bigint    8字節

?

float    4字節      

double    8字節   

double(5,2) ?  表示 5位數,2位小數,?如:999.99

char

    char(3) ?3個字節

varchar

    varchar(20)?  從0到20個字節

?

tmestamp?

?

?創建個員工表

   主鍵,自增

create table ygb(
id int primary key auto_increment,
age int,
sex char(5),
xin_shui DOUBLE(7,2)
);

?

desc ygb;

?

?

?

添加字段

alter table ygb ADD 入值時間 tinyint 

?

alter table ygb ADD 入值時間2 DATE NOT NULL ;


一次多個字段
alter table ygb ADD a int,
ADD b INT ,
add c INT ;

刪除字段
alter table ygb DROP a;
alter table ygb DROP b,
DROP c;


改字段  默認值為 18 放在 id 的后邊
alter table ygb modify age SMALLINT DEFAULT 18 after id;


改字段名
alter table ygb change name 姓名 VARCHAR (20);

?

改表名

rename table ygb to 員工表;

?

插入數據
insert into ygb (id,name,age,sex,xin_shui)
values(1,'aa',18,'boy',17000.00)

查詢
select * from ygb;

?

insert into ygb (name,sex,xin_shui)
values('ab','boy',17000.00);

? id 自增,gae 默認值,

這樣寫要對應著字段寫全

insert into ygb VALUES (10,'bb',20,'g',5000);

?

插入多組

insert into ygb (name,sex,xin_shui)
values('ac','boy',17000.00),
('ad','g',3000),
('af','b',3000);


set 插入
insert into ygb set name = 'cc'


改:
update ygb set xin_shui=xin_shui+2000 where id=10;
在原有基礎上加,如果不加條件,將對所有添加


刪除
delete from ygb where id=11 or id=12;

?

 
刪除字段 ?alter table 表名 drop 字段名;:

刪表:留下空表,

delete from ygb ;    一條條刪

truncate table ygb;  整個刪除,重建 個空表  






建一成績表

create table c_j_b(
id int primary key auto_increment,
name varchar(20),
js DOUBLE,
django DOUBLE,
python DOUBLE );


insert into  c_j_b(name,js,django,python)
values('aa',70,80,90),
('ab',78,91,81),
('ac',80,82,79),
('af',88,82,89),
('bb',78,76,79),
('cc',88,77,65);

?

 

select name from c_j_b;

select name,js from c_j_b;

?

重復只出現一次 ?distinct

select distinct js from c_j_b;

?

?

?這只是顯示,并沒有改庫,

?elect name,js+10,django+10,python+10 from c_j_b;

?

?

select name as 姓名,js as js成績,django as django成績,

  python as python成績 from c_j_b;_j_b;

?

select name js from c_j_b where js>80;

 

?

查 70 到 90 內的?between

select name, js from c_j_b where js between 70 and 90;

?

查 in里邊的是否有

select name ,js from c_j_b where js in (78,90,70);

?

模糊匹配??like'a%'  ?like'a__'

select name ,js from c_j_b where name like'a%';

?

select name from c_j_b where js is null;\

?

?

?

排序

?select ?name,JS from c_j_b ORDER BY JS;

?

?


?select ?name,JS from c_j_b ORDER BY JS desc;



select name,JS+django+python as 總成績 from c_j_b;
+------+-----------+
| name | 總成績 |
+------+-----------+
| aa | 240 |
| ab | 250 |
| ac | 241 |
| af | 259 |
| bb | 233 |
| cc | 230 |
| jj | NULL |
+------+-----------+

?

總成績排序,從低到高,

select name,JS+django+python as 總成績 from c_j_b order by 總成績 desc;
+------+-----------+
| name | 總成績 |
+------+-----------+
| af | 259 |
| ab | 250 |
| ac | 241 |
| aa | 240 |
| bb | 233 |
| cc | 230 |
| jj | NULL |
+------+-----------+


分組 group by
select name from c_j_b group by name;


分組后
  
     分組后 js 的和,      按名字分組

select name sum(js) from c_j_b group by name;

?

      把分組后,js 的總分大于150的打印,group by 后不可用 where ,要用 havin,  

select name sum(js) from c_j_b group by name having?sum(js) >150;

group by 前用 where, 其后用 having,

?

說明,如果 字段中有 null,此時他與誰計算都 分得到個 null,

    所以 ?ifnull(js,0), ?如果 js 里有null 那就讓他 = 0.

?

?

顯示

select * from c_j_b; limit 3;

select * from c_j_b; limit 1:4;

?

轉載于:https://www.cnblogs.com/gdwz922/p/9241551.html

總結

以上是生活随笔為你收集整理的c潭州课堂25班:Ph201805201 MySQL第二课 (课堂笔记)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。