mysql基本操作命令

來源:互聯網
上載者:User

標籤:script   poi   mon   value   姓名   date   info   test   fulltext   

資料庫增刪改查

//建立資料庫
create database school;
//建立表
create table info (id int not null primary key auto_increment,name char(10) not

null,score decimal(5,2),hobby int(2));
#primary key 主鍵 auto_increment 自增列
//查看錶結構
desc info;
//多表查詢(關聯表)
select * from info inner join hob where info.hobby=hob.id;

select info.name,score,hob.hobname from info inner join hob where

info.hobby=hob.id;
//別名查詢
select i.name,score,h.hobby from info as i inner join hob as h where i.hobby=h.id;

//彙總函式
統計count(): 可以改為1
select count(
) from info;
平均值avg ()
select avg(score) from info;
//查看資料庫
show databases;
//進入資料庫
use myschool;
//查看myschool中的表
show tables;
//查看info中的資料
select from info;
//在info中插入資料
insert into info (id,name,score) values (‘tianqi‘,55); #前後匹配,如果為空白:null
//篩選資訊
mysql> select
from 表名 where id=2[and name=?] [or name=?]
//更新資訊
update info set score=75 where id=6;
//刪除資訊
delete from info where name=‘test‘; #整行刪除
//刪除表、資料庫
drop table info; drop database school;
//排序
select from info where 1=1 order by score ; asc--升序,可不寫 #預設升序
select
from info where 1=1 order by score desc ; desc--降序

資料庫索引、事務、視圖

索引:快速查詢資料  條件:資料數目大於兩千條 相當於一本書前的目錄頁

create index 索引名稱 on tablename 列;
id name score address hobby

create index id_index on info(id); 建立普通索引

show index from info \G; 查看索引折行顯示

drop index id_index on info; 刪除索引

create unique index id_index on info(id); 建立唯一索引

alter table info add primary key(id); 主鍵索引

alter table info add column age int(3); 添加列

alter table info drop column age; 刪除列

create table infos (descript TEXT,FULLTEXT(descript));全文索引,descript列名描述

create index multi_index on info(name,address); 多頁索引,講兩個條件聯合起來進行

查詢

事務:一組操作共同執行或者都不執行,結果保持一致

舉個栗子:銀行轉賬
條件:轉賬條件餘額大於0
姓名 餘額
張三 100
李四 200

張三轉賬100 to 李四

begin 開始

updata bank set money=money-100 where name=‘zhangsan‘

updata bank set money=money+100 where name=‘lisi‘

commit 提交

savepoint s1; 設定復原點

rollback to savepoint s1; 回到s1復原點

set autocommit=0 禁止自動認可
set autocommit=1 開啟自動認可
rollback 復原

原子性 不可分割

一致性 前後結果保持一致

隔離性 事務之間隔離,互不影響

持久性 一旦執行成功,不可更改

視圖      資料庫中的虛擬表

作用:一張表或者多張表中的資料給不同的許可權使用者提供訪問

create view 視圖名稱 AS

select 語句

select * from info where score > 80; 查看大於80分的人

create view score_view as select * from info where score >80; 形成視圖進行查看

select * from score_view; 查看視圖

mysql基本操作命令

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.