MySql02
復(fù)習(xí)
數(shù)據(jù)庫相關(guān)
-
連接數(shù)據(jù)庫的命令
mysql -uroot -p
-
創(chuàng)建數(shù)據(jù)庫
create database db2;
-
查詢所有的數(shù)據(jù)庫
show databases;
-
查詢單個數(shù)據(jù)庫的信息
show create database db2;
-
刪除數(shù)據(jù)庫
drop database db2;
-
創(chuàng)建數(shù)據(jù)庫時指定字符集
create database db2 character set utf8;
-
使用數(shù)據(jù)庫
use db2;
表相關(guān)的SQL
-
創(chuàng)建表student
create table student (id int, name varchar(20));
-
查看所有的表
show tables;
-
查看表屬性 數(shù)據(jù)庫表的引擎 和 編碼
show create table student;
-
查看表結(jié)構(gòu)
desc student;
-
創(chuàng)建表 指定引擎和編碼
create table teacher (id int, name varchar(10)) engine=innodb charset=utf8;
-
修改表的名字
rename table student to stu;
-
給表添加字段
-在最后
alter table stu add age int;
-在最前端添加
alter table stu add age int first;
-在某個字段的后面
alter table stu add age int after id;
-
刪除表字段
alter table stu drop age;
-
修改表的屬性
alter table stu engine=myisam charset=utf8;
-
修改字段的名稱和類型
alter table stu change age fatherAge int;
-
修改字段的類型和位置
alter table stu modify fatherAge double after name;
-
刪除表
drop table stu;
數(shù)據(jù)相關(guān)
-創(chuàng)建商品表:
create table t_item(
id int,
title varchar(40),
num int,
price double,
category varchar(20),
createDate date
);
-
插入數(shù)據(jù)
-全表插入
insert into t_item values(1,'毛巾',89,4.5,'日用品','1985-06-23');
-指定字段插入insert into t_item (title,price,category) values ('香皂',1.5,'日用品');
-批量插入數(shù)據(jù)
insert into t_item values (3,'鼠標(biāo)',200,20,'電腦配件','2008-12-21'), (4,'鍵盤',260,35,'電腦配件','2018-11-25'), (3,'寫字板',10,120,'電腦配件','2006-10-28');
-
修改數(shù)據(jù)
update t_item set title='雙飛燕鼠標(biāo)' where title='鼠標(biāo)';
-修改多條數(shù)據(jù)
update t_item set title='鼠標(biāo)',price=38 where title='雙飛燕鼠標(biāo)';
-
刪除數(shù)據(jù)
delete from t_item where id is null; delete from t_item where title='鍵盤';
-
查詢數(shù)據(jù)
-查詢sql 后面也可以添加where 條件
select title from t_item where category='日用品';
練習(xí):
1.創(chuàng)建newdb數(shù)據(jù)庫創(chuàng)建表emp(員工表)字段有:id name谭羔、salary(工資)攒砖、dept(部門名稱)、joinDate(入職日期)create table emp(
id int,
name varchar(10),
salary double,
dept varchar(10),
joinDate date
);
2.插入劉關(guān)張和唐僧四人組 7個人 劉關(guān)張部門為:三國部 唐僧它們部門為取經(jīng)部
insert into emp values(1,'劉備',1800,'三國部','2001-12-10'),(2,'關(guān)羽',800,'三國部','2002-12-10'),(3,'張飛',5800,'三國部','2003-12-10'),(4,'唐僧',11800,'取經(jīng)部','2004-12-10'),(5,'悟空',2800,'取經(jīng)部','2005-12-10'),(6,'八戒',1800,'取經(jīng)部','2006-12-10'),(7,'悟凈',800,'取經(jīng)部','2007-12-10')
3.修改劉備的工資為2000
update emp set salary=2000 where id=1;
4.修改唐僧名稱為唐長老
update emp set name='唐長老' where id=4;
5.給表添加一個年齡字段 在name字段的后面
alter table emp add age int after name;
6.修改三國部的年齡為45
update emp set age=45 where dept='三國部';
7.修改取經(jīng)部的日期為今天日期
update emp set joinDate='2018-2-26' where dept='取經(jīng)部';
8.修改工資小于5000的年齡為18
update emp set age=18 where salary<5000;
9.刪除三國部門所有員工
delete from emp where dept='三國部';
10.刪除全表
delete from emp;
drop table emp;
eclipse自定義代碼塊 自定義模板代碼
-添加方式:window->最后一個->data management->sql Develepment->sql Editor->templates->new
name:是簡化后的代碼
description:描述
pattern:簡化前的代碼
應(yīng)用場景:代碼比較復(fù)雜 并且頻繁使用的時候可用自定義代碼塊
主鍵及自增
每一張表通常會有一個且只有一個主鍵 來表示每條數(shù)據(jù)的唯一性
主鍵其實是對某一個字段的 約束
-主鍵特性:1.值不能重復(fù) 2.非空(不能賦值為null)
-主鍵約束寫法:
create table student (id int primary key,age int);
-主鍵+自增的寫法:
create table student (id int primary key auto_increment,age int);
非空
-非空約束 not null
create table student (id int primary key auto_increment, age int not null);
表字段的注釋
-表字段的注釋關(guān)鍵字:comment
create table user (
id int comment '用戶的id',
age int comment '用戶的年齡'
);
單引號和`的區(qū)別
單引號:用于給字符串賦值
`:用于給表名 和 字段名賦值 可以省略不寫
表設(shè)計
-在設(shè)計表字段的時候测摔,如果某個字段有可能出現(xiàn)大量的重復(fù)(稱為數(shù)據(jù)冗余)蛮瞄,需要把該字段單獨提出創(chuàng)建一張新表所坯,把可能重復(fù)的放到新表內(nèi),在原表只需要使用新表的id即可
練習(xí):
-創(chuàng)建員工表(emp) 和 部門表(dept) 分別在兩個表中插入幾條數(shù)據(jù) id要進(jìn)行主鍵約束并且自增 部門名稱不能為null
create table emp (
id int primary key auto_increment,
name varchar(20),
detpId int,
joinDate date
);
create table dept (
id int primary key auto_increment,
name varchar(20) not null
);
insert into dept (name) values ('財務(wù)');
insert into dept (name) values ('市場');
insert into emp (name,deptId) values ('小明',1),('小紅',1),('小白',2),('小綠',2);
事務(wù)
-事務(wù)是數(shù)據(jù)庫中的執(zhí)行單元
-事務(wù)能夠保證事務(wù)內(nèi)部的sql語句要么都成功挂捅,要么都失敗
-mysql 客戶端(終端芹助、命令行)事務(wù)是默認(rèn)提交的
-修改mysql事務(wù)的自動提交
-查看當(dāng)前數(shù)據(jù)庫是否自動提交事務(wù)
show variables like '%autocommit%';
-給自動提交參數(shù)賦值為0 是關(guān)閉 1是開啟
set autocommit=0;
-演示事務(wù):
創(chuàng)建User表 有id 和money字段
插入一條數(shù)據(jù) id=1 money=100;
1.開啟兩個終端A闲先、B状土,在A里面 執(zhí)行
set autocommit=0; 然后修改表中的某個數(shù)據(jù)
此時去B終端查看,因為A開啟了事務(wù)此時并沒有提交伺糠,則A的操作是在內(nèi)存中操作所以此時B終端看不到數(shù)據(jù)的改變蒙谓,直到A執(zhí)行Commit才把內(nèi)存中的操作執(zhí)行到數(shù)據(jù)庫,如果在執(zhí)行Commit之前執(zhí)行了Rollback 則A窗口 會回到修改之前的狀態(tài)
begin:開啟事務(wù)
commit:提交事務(wù)
rollback:回滾
SQL分類
數(shù)據(jù)定義語言 DDL
data Definition language
-DDL不支持事務(wù) 不能回滾
常見命令有: create alter drop
數(shù)據(jù)操縱語言 DML
Data Manipulation Language
-DDL支持事務(wù)
常見命令:insert update delete
select屬于DML
數(shù)據(jù)查詢語言 DQL
Data Query Language
select 是最常用的命令
面試問的最多
TCL
Trainsaction Control Language 事務(wù)控制語言
常見命令:begin commit rollback
DCL
Data Control Language 數(shù)據(jù)控制語言
數(shù)據(jù)庫數(shù)據(jù)類型
整數(shù)
-常用類型為 int(m) bigint(m)
-m代表顯示的位數(shù) 意思是當(dāng)顯示的數(shù)據(jù)不足m值時在前面補0训桶,前提是必須在字段的聲明后面添加zerofill
create table t_int (id int(6) zerofill);
注意:有個別公司把這個m 當(dāng)成數(shù)據(jù)長度來用
浮點數(shù)
float(m,d): 不常用
double(m,d):比float精度高
decimal(m,d):比double精度還要高好多
m代表總長度累驮,的代表小數(shù)的位數(shù)
當(dāng)涉及到錢的時候需要用到高精度的類型 會使用decimal
字符串
varchar(20) :可變長度 執(zhí)行效率低 節(jié)省空間 abc 3 :65535 但是建議不超過255如果文本超過255則建議使用text
char(20) :固定長度 執(zhí)行效率高 占空間大 abc 20
長度255
text:長度可變最大65535
日期
date:只能保存日期
time:只能保存時間
datetime:保存日期+時間 如果不賦值默認(rèn)值為null酣倾,最大值9999-12-31
timestamp:保存日期+時間 如果不賦值默認(rèn)為當(dāng)前時間,最大值2038-01-19
因為timestamp有默認(rèn)值當(dāng)前時間所以平時使用較多
案例:創(chuàng)建表t谤专,字段d1 date躁锡,d2 time,d3 datetime,d4 timestamp
create table t (d1 date,d2 time,d3 datetime, d4 timestamp);
insert into t (d1,d2) values ('2018-12-21','12:22:38');
insert into t values ('2018-12-21','12:22:38','9898-12-10 12:06:38','2028-05-06 11:11:11');
timestamp如果插入數(shù)據(jù)超出取值范圍則報錯
回顧
1.主鍵
primary key 唯一 不能為null
2.自增
auto_increment
3.非空
not null
4.注釋
comment
5.'區(qū)別
字段名和表名 '字符串的值的時候
6.事務(wù) 數(shù)據(jù)庫的執(zhí)行單元
7.begin 開啟事務(wù) commit 提交事務(wù) rollback 回滾事務(wù)
查看當(dāng)前數(shù)據(jù)庫是否自動提交
show variables like '%autocommit%';
關(guān)閉自動提交 開啟為1
set autocommit=0;
8.SQL分類
ddl 數(shù)據(jù)定義語言 不支持事務(wù) create drop alter
dml 數(shù)據(jù)操作語言 支持事務(wù) insert update delete select(也屬于dql)
dql 數(shù)據(jù)查詢語言 select
tcl 事務(wù)控制語言 begin commit rollback
dcl 數(shù)據(jù)控制語言 控制權(quán)限分配
9.數(shù)據(jù)類型
整數(shù):int bigint
浮點數(shù):float double decimal(m,d) m總長度 d小數(shù)點后面的位數(shù)
字符串: char(20) varchar(20) text
char 固定長度 text長度65535
日期:date time datetime timestamp
作業(yè):
創(chuàng)建 員工表 emp 部門表 dept
商品表 t_item 商品分類 t_item_category