MySQL語句整理(一)

來源:互聯網
上載者:User

標籤:資料表   curd   roo   資料庫的操作   物理   增刪改   rem   quit   where   

--01 mysql 資料庫的操作     -- 連結資料庫    mysql -uroot -pmysql        -- 不顯示密碼  *****    mysql -uroot -p        -- 退出資料庫    quit/exit ctrl+d     -- sql語句最後需要有分號;結尾    -- 顯示資料庫版本 version    select version();     -- 顯示時間 now    select now();        -- 查看當前使用的資料庫    select database();     -- 查看所有資料庫    show databases;     -- 建立資料庫    create database 資料庫名 charset = utf8;     -- 查看建立資料庫的語句    show create database 資料庫名         -- 使用資料庫   use 資料庫的名字     -- 刪除資料庫    drop database 資料庫名; --02 資料表的操作     -- 查看當前資料庫中所有表    show tables;           -- 建立表    -- int unsigned 無符號整形    -- auto_increment 表示自動成長    -- not null 表示不可為空    -- primary key 表示主鍵    -- default 預設值    -- create table 資料表名字 (欄位 類型 約束[, 欄位 類型 約束]);    create table 表名(id int unsigned auto_increment primary key,name varchar(30) not null,age int unsigned);     -- 修改表名    alter table 舊錶名 rename 新表名;    rename table 舊錶名 to 新表名;     -- 查看錶結構    desc 資料表的名字;     -- 建立 students 表(id、name、age、high (decimal)、gender (enum)、cls_id)    create table students (id int unsigned auto_increment primary key,name varchar(30) not null ,age int unsigned,high decimal(5,2),gender enum("男","女","保密","妖") default "保密",cls_id int unsigned);       -- 查看錶的建立語句    show create table 表名字;        -- 修改表-添加欄位 mascot (吉祥物) **    -- alter table 表名 add 列名 類型;       -- 修改表-修改欄位:不重新命名版    -- alter table 表名 modify 列名 類型及約束;    alter table classes modify mascot varchar(100);     -- 修改表-修改欄位:重新命名版  ***    -- alter table 表名 change 原名 新名 類型及約束;    alter table classes change mascot jxw varchar(30);     -- 修改表-刪除欄位  *    -- alter table 表名 drop 列名;    alter table classes drop jxw;     -- 刪除表    -- drop table 表名;    -- drop database 資料庫;    drop table classes; *    drop database python10; 半*       --03 增刪改查(curd)     -- 增加        -- 全列插入        -- insert [into] 表名(欄位名) values(值)        -- 主鍵欄位 可以用 0  null   default 來佔位        -- 向classes表中插入 一個班級        insert into classes(name) values("python10");        insert into classes(name) values(‘python11‘),(‘python12‘);         -- 全部插入        insert into students(id,name,age,high,gender,cls_id) values(2,‘yangyang‘,18,1.80890,‘保密‘,001);        -- 部分插入        -- insert into 表名(列1,...) values(值1,...)        insert into students(name) values("yang3");         -- 多行插入        insert into students(name) values("張三"),("李四");        insert into 表名(欄位) values(值) #一一對應     -- 修改    -- update 表名 set 列1=值1,列2=值2... where 條件;        -- 全部修改        update students set name = "王五"        -- 按條件修改        update students set name =‘李四‘ where id = 4;        -- 按條件修改多個值        -- update students set gender ="",name = "xxx" where ;        update students set name=‘張三‘,age = 20 where id = 5;            -- 查詢基本使用        -- 查詢所有列        -- select * from 表名;        ---定條件查詢            select * from 表名 where id = 2;        -- 查詢指定列        -- select 列1,列2,... from 表名;                -- 可以使用as為列或表指定別名        -- select 欄位[as 別名] , 欄位[as 別名] from 資料表;        -- 欄位的順序        select age,name from students;     -- 刪除        -- 物理刪除        -- delete from 表名 where 條件;        -- 邏輯刪除        -- 用一個欄位來表示 這條資訊是否已經不能再使用了        -- 給students表添加一個 is_delete 欄位 bit 類型        --alter table 表名 add 欄位 類型 default 預設值;     -- Database Backup與恢複(瞭解)        -- mysqldump –uroot –p 資料庫名 > python.sql;        -- mysql -uroot –p 新資料庫名 < python.sql; # 注意匯入的時候需要先要建立資料庫

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.