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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > 数据库 >内容正文

数据库

mysql增加一条学生记录_Mysql基本操作记录

發布時間:2023/12/20 数据库 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 mysql增加一条学生记录_Mysql基本操作记录 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

# DataBase:Mysql Tool:Navicat# 創建學生信息表create tableStudent (

# 學號作為主鍵,varchar是可變長字符串

SnoVARCHAR(20) primary key,

# 使用default定義默認值, Sage int auto_increment 可設置自動遞增

Sageint default 18,

SnameVARCHAR(20)

)

# 創建老師表create tableTeacher (

TnoVARCHAR(20) primary key,

TnameVARCHAR(20),

TageINT,

AcademyNoVARCHAR(20),

# 創建外鍵約束constraint Fk_Teacher_Academy foreign key(AcademyNo) referencesAcademy(AcademyNo)

)

# 創建課程表create tableCourse (

# 列級定義主鍵

CnoVARCHAR(20) primary key,

TnoVARCHAR(20),

CnameVARCHAR(50)

)

# 添加約束alter table Course add constraint Fk_Course_Teacher foreign key(Tno) referencesTeacher(Tno);

# 創建成績表create tableSc (

SnoVARCHAR(20),

CnoVARCHAR(20),

ScoreFLOAT,

# 表級定義多個主鍵primary key(Sno, Cno),

# 添加外鍵并級聯操作constraint Fk_Sc_Student foreign key(Sno) references Student(Sno) on delete cascade on update cascade,

# 默認刪除跟修改都是restrictconstraint Fk_Sc_Course foreign key(Cno) referencesCourse(Cno)

)

# 查看創建的表select * fromStudent

# 修改表操作,#增加一列alter tableTeacheradd column Tage VARCHAR(3);

# 刪除指定列alter tableTeacherdrop columnTname;

# 修改某一列alter tableTeacher

changecolumn Tage Tage int;

# 刪除表drop tableTeacher

# Mysql常用五類約束類型:

#not null:非空約束,指定某列不為空

#unique: 唯一約束,指定某列和幾列組合的數據不能重復

#primary key:主鍵約束,指定某列的數據不能重復、唯一

#foreign key:外鍵,指定該列記錄屬于主表中的一條記錄,參照另一條數據

#check:檢查,指定一個表達式,用于檢驗指定數據

# 插入內容insert into student VALUES('2016211', 21, 'tom');

# 插入指定內容,注意,主鍵不為空insert into student(Sno, Sname) VALUES('2016205', 'jerry');

# 更新內容updatestudentset Sage = 25

where Sno = '2016213'# 刪除內容delete fromstudentwhere Sno = '2016218'# 選擇并映射指定的列select Sname, Sage fromstudent

# 去掉重復的行distinct

select distinct Sage fromstudent

# 使用where語句篩選數據select * from student where Sage > 21# 指定范圍 (not)between .. and .. 包含等號 (也可以 where Sage >= 21 and Sage <= 25)select * from student where Sage between 21 and 25# 使用 in 查找屬性值屬于指定集合的元組select * from student where Sname in('tom','LiMing')

# 字符匹配like, _代表任意單個字符,%代表任意長度select * from student where Sno like '2016211'

select * from student where Sname like 'j%'

select * from student where Sname like 't_m'# 轉義字符 \, 如 \_hell 表示 _hellselect * from student where Sname like 'm\_ary'# 涉及空值的查詢select * from student where Sage is NULL# 與:and 或:or

select * from student where Sno like'2016%' and Sage < 21

select * from student where Sno='2016215' or Sage >= 24# order by , DESC降序,ASC升序select * fromstudentwhere sno like'2016%'

order by Sage desc# 常用聚集函數

#COUNT():統計個數

#sum():計算總和

#avg():平均值

#max():最大值

#min():最小值

# 統計不同的年齡個數select count(distinct Sage) fromstudent

# 計算平均年齡select avg(Sage) 平均年齡 fromstudent

# having的使用作用,類似于where,但可以對聚集函數使用,結合group by使用select AcademyName 學院, count(s.AcademyNo) 人數fromstudent s, Academy a

# 等值連接where s.AcademyNo =a.AcademyNo

# 通過學院代號分組group bys.AcademyNo

# 篩選分組having count(s.AcademyNo) >= 2# 嵌套查詢,例:查詢選修了數據庫原理的所有學生信息select * fromstudent

# 如果學生學號在子查詢的集合里面則判斷為 truewhere Sno in(

# 子查詢獲取的是一個包含所有學生學號的集合selects.Snofromsc s, course cwhere s.Cno = c.Cno and Cname = '數據庫原理')select * fromstudent

# 如果把集合直接給出,也就類似于上一個嵌套查詢的效果where Sno in('2016211', '2016212')

# 使用limit篩選數據 limit 開始下標(0開始) 條數

# 篩選第1~2條數據select * from student limit 0,2;

# 篩選前五條數據,相當于limit0,5

select * from student limit 5;select * fromstudent

#exists 返回true orfalsewhere exists(select * fromteacherwhere Tno = '1111')

# 建立視圖create viewstudent_viewas

selectSno 學號, Sname 姓名fromstudent

# 像基本表一樣對視圖查詢select * fromstudent_view

# 刪除視圖drop viewstudent_view

# check短語使用,但Mysql中check不強制執行create tablepeople (

Pidvarchar(20) primary key,

Pnamevarchar(30),

# 當然也可以跟PK等一樣表級定義

Psexvarchar(2) check (Psex in ('男', '女'))

)

# 授予權限:grant 權限1,權限2.. on 對象類型 對象名 to用戶1,用戶2...grant select,insert on table student touser1

# 收回權限:revoke 權限1,權限2.. on 對象類型 對象名 from用戶1,用戶2...revoke delete on table student fromuser1

# 函數以及變量的使用

#create function 函數名([參數]) returns返回值類型

#begin#declare變量名 變量類型...

# sql語句;

#return值;

#end;create function getStudentAge(Id varchar(20)) returns INT

begin# 聲明一個變量以及對應類型declare age int;

# 賦值或者set age= (select Sage from student where Sno =Id);select Sage from student where Sno = Id intoage;

# 返回值returnage;end# 調用函數獲取內容select getStudentAge('2016211');

#drop function 函數名drop functiongetStudentAge1;

# 存儲過程的使用,類似函數

#create procedure proc_getStudentAge(in變量名 類型,out(返回變量) 變量名 類型)

#begin# Sql語句;

#end

create procedure proc_getStudentAge(Id varchar(20))begin

select * from student where Sno =Id;end# in表示傳入變量,out表示傳出變量create procedure proc_getStudentAge2(in Id varchar(20), out age int)begin

select Sage from student where Sno = Id intoage;end# 執行存儲過程

call proc_getStudentAge('2016211');

# 執行存儲過程獲取數據并使用數據

call proc_getStudentAge2('2016211', @age);select @age;

# 刪除存儲過程drop procedureproc_getStudentAge;

# 觸發器的使用,new表示新數據,old表示原數據

#create trigger 觸發器名稱 [before | after] [insert | update | delete]#on 表名 foreach row

#begin# 一個或多個語句列表,列表里面的每條語句必須用分號隔開

#end

create trigger insert_stu after insert

on student foreach rowbegin# 自變量declare age int;

# new.Sno 是指新添元組的Snoset age = (select Sage from student where Sno =new.Sno);insert into sc values(new.Sno, '51', (100-age));end# 測試上述觸發器insert into student values('2016221', 20, 'oppo', '17');

# 刪除觸發器drop trigger insert_stu;

總結

以上是生活随笔為你收集整理的mysql增加一条学生记录_Mysql基本操作记录的全部內容,希望文章能夠幫你解決所遇到的問題。

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