標籤:let drop 暫存資料表 lock dad alter 產生 distinct proc
以下是MySQL最基本的增刪改查語句,建立索引,刪除索引,查看索引。資料庫裡表的關聯。很多IT工作者都必須要會的命令,也是IT行業面試最常考的知識點,由於是入門級基礎命令
- 增
> create database school; 建立資料庫school> use school; 進入資料庫> create table class (id int,name char(10),score decimal(5,2));建立一個表class(id整數 name字串10個 成績 資料長度5 小數點後面保留2位)> insert into class (id,name,score) values (1,‘zhangsan‘,70.5); > insert into class (id,name,score) values (2,‘lisi‘,80.5);寫入資料> alter table class add address varchar(50) default‘地址不詳‘增加一個列
?
?
?
- 刪
delete from class where id=2; 刪除id為2的行
alter table class drop column address ; 刪除address列
drop table class; 刪除整個表
drop database school; 刪除資料庫
?
?
索引
建立一個資料庫和表 來示範下索引操作
> create database school;> use school;> create table class (id int(4) not null primary key auto_increment,name char(10) not null,score decimal(5,2),address varchar(50) default‘地址不詳‘,hobby int(4))charset=utf8;> insert into class (name,score,address,hobby) values (‘zhangsan‘,70.5,‘金川校區‘,2);> insert into class (name,score,address,hobby) values (‘lisi‘,80.5,default,2);
普通索引
create index name_index on class(name); 建立索引
show index from class; 查看索引
drop index name_index on class; 刪除索引
唯一索引
create unique index name_index on class(name); 建立索引
drop index name_index on class; 刪除索引
- 關聯資料庫
> create table hob (hid int,hobname char(10) not null); 建立表用來關聯> select c.id,c.name,c.score,c.address,h.hobname from class c inner join hob h on c.hobby=h.hid;查看(class別名c,hob別名h)class表id,name,score,address.。 hob表的hobname 將class表的hobby 關聯hob的hid。 (注意:這隻是查看)> create temporary table tempclass (select c.id,c.name,c.score,c.address,h.hobname from class c inner join hob h on c.hobby=h.hid;); (產生暫存資料表class 注意:暫存資料表show tables; 查詢不到 去掉參數temporary則會產生永久的表)> select * from class_hob; 查看class_hob暫存資料表。
Mysql的基本操作(增刪改查)