方立勋_30天掌握JavaWeb_MySQL和表约束
mysql管理員的用戶名和密碼:root root
創建一個名稱為mydb1的數據庫
create database mydb1;
show databases;創建一個使用utf-8字符集的mydb2數據庫。
create database mydb2 character set utf8;創建一個使用utf-8字符集,并帶校對規則的mydb3數據庫。
create database mydb3 character set utf8 collate utf8_general_ci;查看前面創建的mydb2數據庫的定義信息
show create database mydb2;刪除前面創建的mydb1數據庫
drop database mydb1;查看服務器中的數據庫,并把其中某一個庫的字符集修改為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表有數據
對tt作備份操作,啟動一個window命令行窗口,執行如下命令
mysqldump -uroot -p tt>c:\tt.sql演示恢復
先刪除庫
drop database tt;恢復tt庫(法1)
2.1 為恢復庫,要先創建庫 create database tt;
2.2 再恢復tt庫
use tt;
source c:\tt.sql (source:可以執行一個 sql腳本)恢復tt庫(法2)
2.1 為恢復庫,要先創建庫 create database tt;
2.2 恢復庫 mysql -uroot -proot tt
增刪改查示例
創建一個員工表
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; 查看表的創建細節
desc employee; 看表結構
在上面員工表的基本上增加一個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;
插入數據的細節1
insert into employee values(1,’aaa’,’1980-09-09’,’1980-09-09’,’bbb’,90,’aaaaa’);
插入數據的細節2
insert into employee values(‘1’,’aaa’,’1980-09-09’,’1980-09-09’,’bbb’,’90’,’aaaaa’);
插入數據的細節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的薪水在原有基礎上增加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;
查詢表中所有學生的信息。
select * from student;
查詢表中所有學生的姓名和對應的英語成績。
select name,english from student;
過濾表中重復的英語數據。
select distinct english from student;
在所有學生總分上加10分特長分。
select name,(chinese+english+math)+10 from student;
統計每個學生的總分。
select name,(chinese+english+math) from student;
使用別名表示學生分數。
select name as 姓名,(chinese+english+math)+10 as 總分 from student;
select name 姓名,(chinese+english+math)+10 總分 from student;
查詢姓名為wu的學生成績
select * from student where name=’王五’;
查詢英語成績大于90分的同學
select * from student where english>’90’;
查詢總分大于200分的所有同學
select name from student where (chinese+english+math)>200;
查詢英語分數在 80-90之間的同學。
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;
查詢數學分數為89,90,91的同學。
select * from student where math in(89,90,91);
查詢所有姓李的學生成績。
select * from student where name like ‘李%’;
select * from student where name like ‘李_’;
查詢數學分>80,語文分>80的同學。
select * from student where math>80 and chinese>80;
對數學成績排序后輸出。
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;
對姓李的學生成績排序輸出
select * from student where name like ‘李%’ order by (chinese+english+math) desc;
統計一個班級共有多少學生?
select count(name) from student;
select count(*) from student;
統計數學成績大于90的學生有多少個?
select count(*) from student where math>80;
統計總分大于250的人數有多少?
select count(*) from student where (chinese+english+math)>250;
關于 count的函數的細節 (count只統有值的行)
統計一個班級數學總成績?
select sum(math) from student;
統計一個班級語文、英語、數學各科的總成績
select sum(chinese),sum(english),sum(math) from student;
統計一個班級語文、英語、數學的成績總和
select sum(chinese+english+math) from student;
統計一個班級語文成績平均分
select sum(chinese)/count(*) from student;
統計一個班級語文成績平均分
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) ); 創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎總結
以上是生活随笔為你收集整理的方立勋_30天掌握JavaWeb_MySQL和表约束的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 方立勋_30天掌握JavaWeb_JDB
- 下一篇: 方立勋_30天掌握JavaWeb_jdb