數(shù)據(jù)庫操作、DDL 數(shù)據(jù)庫的增刪改查
2.3 數(shù)據(jù)庫操作
學(xué)習(xí)目標(biāo)
? 1. 能夠說出什么是CRUD
? 2. 能夠熟練對數(shù)據(jù)庫進(jìn)行增刪改查操作
--------------------------------------------------------------------------------
2.3.1 數(shù)據(jù)庫操作介紹
在數(shù)據(jù)庫操作中,基本操作都是圍繞增刪改查來操作。簡稱CRUD
? C Create 創(chuàng)建
? R Read/Retrieve 查詢
? U Update 修改
? D Delete 刪除
2.3.2 數(shù)據(jù)庫操作
在數(shù)操作數(shù)據(jù)庫時,所有的數(shù)據(jù)庫語句都要以分號結(jié)束
數(shù)據(jù)庫操作不區(qū)分大小寫
DDL 數(shù)據(jù)庫的增刪改查總結(jié):
? 創(chuàng)建數(shù)據(jù)庫
? ? ? create database 數(shù)據(jù)庫名;
? ? ? create database 數(shù)據(jù)庫名 charset 字符集;
? 顯示數(shù)據(jù)庫創(chuàng)建信息
? ? ? show create database 數(shù)據(jù)庫名;
? 修改數(shù)據(jù)庫字符集
? ? ? alter database 數(shù)據(jù)庫名 charset=字符集;
? 顯示所有數(shù)據(jù)庫
? ? ? show databases;
? 切換、使用數(shù)據(jù)庫
? ? ? use 數(shù)據(jù)庫名;
? ? ? 這個語法可以不加;但是盡量統(tǒng)一加;
? 顯示當(dāng)前選擇數(shù)據(jù)庫
? ? ? select database();
? 刪除數(shù)據(jù)庫
? ? ? drop database 數(shù)據(jù)庫名;
? comment 給字段添加批注COMMENT '老師id',給表添加批注COMMENT='學(xué)生表'
? ? ? CREATE TABLE student(id int(10) unsigned NOT NULL AUTO_INCREMENT,name varchar(250) DEFAULT '1' COMMENT '名字',teacher_id int(11) DEFAULT '0' COMMENT '老師id') ENGINE=InnoDB CHARSET=utf8 COMMENT='學(xué)生表';
詳解:
<1>創(chuàng)建數(shù)據(jù)庫
? create database 數(shù)據(jù)庫名
? ? ? create database testdb;
? create database 數(shù)據(jù)庫名 character set utf8
? ? ? create database testdb2 character set utf8;
? ? ? character set utf8 或者 charset utf8 或者 charset=utf8
<2>顯示數(shù)據(jù)庫創(chuàng)建信息
? show create database 數(shù)據(jù)庫名
? ? ? show create database testdb;
<3>修改數(shù)據(jù)庫編碼
? alter database 數(shù)據(jù)庫名 character set utf8
? ? ? alter database testdb charset=utf8;
<4>顯示所有數(shù)據(jù)庫
? show databases;
<5>切換、使用數(shù)據(jù)庫
? use 數(shù)據(jù)庫名
? ? ? use testdb
<6>顯示當(dāng)前數(shù)據(jù)庫
? select database();
<7>刪除數(shù)據(jù)庫
? drop database 數(shù)據(jù)庫名
? ? ? drop database testdb2;
不要隨便刪庫,刪庫只能跑路,追殺你到天涯海角
2.3.3 總結(jié)
數(shù)據(jù)庫操作命令相對來說比較多,相互之間沒有邏輯,但是并不難理解 。
學(xué)習(xí)數(shù)據(jù)庫操作,一定要多加練習(xí),熟能生巧。