Mysql的基本操作(增刪改查)

來源:互聯網
上載者:User

標籤: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‘地址不詳‘增加一個列

?

  • > update class set name=‘wangwu‘ where id=1;  zhangsan改成wangwu  修改位置是id=1的列

?

  • > show databases;      查看資料庫> use school;              進入資料庫> show tables;        查看所有表(需要進入資料庫查看)> desc class;           查看class表的結構> select * from class;  查看class表的所有資料> select name from class;    查看class表的指定列> select distinct * from class;   查看所有資料 (且不重複)> select * from class where score>85;   條件查看(查看score>85的)> select * from class where score>85 and score<90;   (查看大於85 小於90的資料)> select * from class where exists (select * from class where score<90) and score<80;使用exists查詢  先查詢小於90的人  再在小於的90的資料中 查詢 小於80的資料  最終顯示

?

  • 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的基本操作(增刪改查)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.