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

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 运维知识 > 数据库 >内容正文

数据库

方立勋_30天掌握JavaWeb_MySQL和表约束

發(fā)布時間:2023/12/20 数据库 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 方立勋_30天掌握JavaWeb_MySQL和表约束 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
  • mysql管理員的用戶名和密碼:root root

  • 創(chuàng)建一個名稱為mydb1的數(shù)據(jù)庫
    create database mydb1;
    show databases;

  • 創(chuàng)建一個使用utf-8字符集的mydb2數(shù)據(jù)庫。
    create database mydb2 character set utf8;

  • 創(chuàng)建一個使用utf-8字符集,并帶校對規(guī)則的mydb3數(shù)據(jù)庫。
    create database mydb3 character set utf8 collate utf8_general_ci;

  • 查看前面創(chuàng)建的mydb2數(shù)據(jù)庫的定義信息
    show create database mydb2;

  • 刪除前面創(chuàng)建的mydb1數(shù)據(jù)庫
    drop database mydb1;

  • 查看服務(wù)器中的數(shù)據(jù)庫,并把其中某一個庫的字符集修改為gb2312;
    alter database mydb2 character set gb2312;
    show create database mydb2;

  • 演示備份

    create database tt; use tt; create table a (name varchar(20) ); insert into a(name) values('aaaa'); select * from a;

    —–看到a表有數(shù)據(jù)
    對tt作備份操作,啟動一個window命令行窗口,執(zhí)行如下命令
    mysqldump -uroot -p tt>c:\tt.sql

  • 演示恢復(fù)

  • 先刪除庫
    drop database tt;

  • 恢復(fù)tt庫(法1)
    2.1 為恢復(fù)庫,要先創(chuàng)建庫 create database tt;
    2.2 再恢復(fù)tt庫
    use tt;
    source c:\tt.sql (source:可以執(zhí)行一個 sql腳本)

  • 恢復(fù)tt庫(法2)
    2.1 為恢復(fù)庫,要先創(chuàng)建庫 create database tt;
    2.2 恢復(fù)庫 mysql -uroot -proot tt

增刪改查示例

創(chuàng)建一個員工表

use mydb2; create table employee (id int,name varchar(40),sex varchar(4),birthday date,entry_date date,job varchar(40),salary decimal(8,2),resume text );

show tables; 查看庫的所有表(查看庫里的表要先打開庫)
show create table employee; 查看表的創(chuàng)建細(xì)節(jié)
desc employee; 看表結(jié)構(gòu)

在上面員工表的基本上增加一個image列。
alter table employee add image blob;

修改job列,使其長度為60。
alter table employee modify job varchar(60);

刪除sex列
alter table employee drop sex;

表名改為user。
rename table employee to user;

修改表的字符集為utf-8
alter table user character set utf8;

列名name修改為username
alter table user change column name username varchar(40);

刪除表
drop table user;

使用insert語句向表中插入三個員工的信息。
rename table user to employee;
insert into employee(id,username,birthday,entry_date,job,salary,resume) values(1,’aaa’,’1980-09-09’,’1980-09-09’,’bbb’,90,’aaaaa’);
select * from employee;

插入數(shù)據(jù)的細(xì)節(jié)1
insert into employee values(1,’aaa’,’1980-09-09’,’1980-09-09’,’bbb’,90,’aaaaa’);

插入數(shù)據(jù)的細(xì)節(jié)2
insert into employee values(‘1’,’aaa’,’1980-09-09’,’1980-09-09’,’bbb’,’90’,’aaaaa’);

插入數(shù)據(jù)的細(xì)節(jié)3(插入中文)

  • 要告訴mysql客戶采用gb2312編碼
    show variables like ‘chara%’;
    set character_set_client=gb2312;
    insert into employee(id,username) values(‘3’,’張三’);

  • 要想查看時不亂碼
    show variables like ‘chara%’;
    set character_set_results=gb2312;
    select * from employee;

  • 將所有員工薪水修改為5000元。
    update employee set salary=5000;

    將姓名為’bbb’的員工薪水修改為3000元。
    update employee set salary=3000 where username=’bbb’;

    將姓名為’bbb的員工薪水修改為4000元,job改為ccc。
    update employee set salary=4000,job=’ccc’ where username=’bbb’;

    將bbb的薪水在原有基礎(chǔ)上增加1000元。
    update employee set salary=salary+1000 where username=’bbb’;

    更新要注意的問題
    update employee set username=’ccc’,salary=9000,birthday=’1980-09-09’,…………………
    update where id=1;

    刪除表中名稱為’zs’的記錄。
    delete from employee where username=’bbb’;

    刪除表中所有記錄。
    delete from employee;

    使用truncate刪除表中記錄。
    truncate table employee;

    查詢表中所有學(xué)生的信息。
    select * from student;

    查詢表中所有學(xué)生的姓名和對應(yīng)的英語成績。
    select name,english from student;

    過濾表中重復(fù)的英語數(shù)據(jù)。
    select distinct english from student;

    在所有學(xué)生總分上加10分特長分。
    select name,(chinese+english+math)+10 from student;

    統(tǒng)計每個學(xué)生的總分。
    select name,(chinese+english+math) from student;

    使用別名表示學(xué)生分?jǐn)?shù)。
    select name as 姓名,(chinese+english+math)+10 as 總分 from student;
    select name 姓名,(chinese+english+math)+10 總分 from student;

    查詢姓名為wu的學(xué)生成績
    select * from student where name=’王五’;

    查詢英語成績大于90分的同學(xué)
    select * from student where english>’90’;

    查詢總分大于200分的所有同學(xué)
    select name from student where (chinese+english+math)>200;

    查詢英語分?jǐn)?shù)在 80-90之間的同學(xué)。
    select name from student where english>80 and english<90;
    select name from student where english between 80 and 90; == select name from student where english>=80 and english<=90;

    查詢數(shù)學(xué)分?jǐn)?shù)為89,90,91的同學(xué)。
    select * from student where math in(89,90,91);

    查詢所有姓李的學(xué)生成績。
    select * from student where name like ‘李%’;
    select * from student where name like ‘李_’;

    查詢數(shù)學(xué)分>80,語文分>80的同學(xué)。
    select * from student where math>80 and chinese>80;

    對數(shù)學(xué)成績排序后輸出。
    select name,math from student order by math;

    對總分排序后輸出,然后再按從高到低的順序輸出
    select name 姓名,(chinese+english+math) 總分 from student order by (chinese+english+math) desc;
    select name 姓名,(chinese+english+math) 總分 from student order by 總分 desc;

    對姓李的學(xué)生成績排序輸出
    select * from student where name like ‘李%’ order by (chinese+english+math) desc;

    統(tǒng)計一個班級共有多少學(xué)生?
    select count(name) from student;
    select count(*) from student;

    統(tǒng)計數(shù)學(xué)成績大于90的學(xué)生有多少個?
    select count(*) from student where math>80;

    統(tǒng)計總分大于250的人數(shù)有多少?
    select count(*) from student where (chinese+english+math)>250;

    關(guān)于 count的函數(shù)的細(xì)節(jié) (count只統(tǒng)有值的行)

    統(tǒng)計一個班級數(shù)學(xué)總成績?
    select sum(math) from student;

    統(tǒng)計一個班級語文、英語、數(shù)學(xué)各科的總成績
    select sum(chinese),sum(english),sum(math) from student;

    統(tǒng)計一個班級語文、英語、數(shù)學(xué)的成績總和
    select sum(chinese+english+math) from student;

    統(tǒng)計一個班級語文成績平均分
    select sum(chinese)/count(*) from student;

    統(tǒng)計一個班級語文成績平均分
    select avg(chinese) from student;

    求一個班級總分平均分
    select avg(chinese+math+english) from student;

    求班級最高分和最低分
    select max(chinese+math+english),min(chinese+math+english) from student;

    對訂單表中商品歸類后,顯示每一類商品的總價
    select product,sum(price) from orders group by product;

    查詢購買了幾類商品,并且每類總價大于100的商品
    select product from orders group by product having sum(price)>100;

    表約束

    1. 定義主鍵約束(每一個表必須有一個主鍵列)

    create table student (id int primary key,name varchar(40) );

    定義主鍵自動增長

    create table student (id int primary key auto_increment,name varchar(40) );

    2. 定義唯一約束

    drop table student; create table student (id int primary key auto_increment,name varchar(40) unique );

    3. 定義非空約束

    drop table student; create table student (id int primary key auto_increment,name varchar(40) unique not null );

    4. 定義外鍵約束

    create table husband (id int primary key,name varchar(40) );create table wife (id int primary key,name varchar(40),husband_id int,constraint husband_id_FK foreign key(husband_id) references husband(id) ); 創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎勵來咯,堅持創(chuàng)作打卡瓜分現(xiàn)金大獎

    總結(jié)

    以上是生活随笔為你收集整理的方立勋_30天掌握JavaWeb_MySQL和表约束的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。