mysql 資料庫的操作

來源:互聯網
上載者:User

標籤:建表   查詢   sig   預設   value   databases   一個   modify   style   

--01 mysql 資料庫的操作

-- 連結資料庫

mysql -uroot -pmysql

-- 不顯示密碼

mysql -uroot -pmysql

-- 退出資料庫

quit/exit/ctrl + d

-- sql語句最後需要有分號;結尾
-- 顯示資料庫版本 version

select version();

-- 顯示時間

select now();

-- 查看當前使用的資料庫

select database();

-- 查看所有資料庫

show databases;

-- 建立資料庫
-- create database 資料庫名 charset=utf8;

create database sypython01;create database sypython01 charset=utf8;(注意)

-- 查看建立資料庫的語句
-- show create database ....

show create database sypython01;

-- 使用資料庫
-- use 資料庫的名字

use xxx;

-- 刪除資料庫
-- drop database 資料庫名;

drop database sypython01;

--02 資料表的操作

-- 查看當前資料庫中所有表
show tables;

-- 建立表
-- int unsigned 無符號整形
-- auto_increment 表示自動成長
-- not null 表示不可為空
-- primary key 表示主鍵
-- default 預設值
-- create table 資料表名字 (欄位 類型 約束[, 欄位 類型 約束]);

create table xxx(  id int unsigned not null primary key auto_increment,  name varchar(20) not null);create table hero(  id int unsigned not null primary key auto_increment,  name varchar(20) not null,  contry enum("德瑪西亞","諾克薩斯","暗影島","恕瑞瑪","弗雷爾卓德"));

 


-- 查看錶結構
-- desc 資料表的名字;

desc xxx;

 



-- 建立 classes 表(id、name)

create table classes(id int unsigned not null primary key auto_increment,name varchar(20) not null);

 

-- 建立 students 表(id、name、age、high (decimal)、gender (enum)、cls_id)

create table students(id int unsigned not null primary key auto_increment,name varchar(20),age int unsigned,high decimal(5,2),gender enum("男","女","保密") default "保密",cls_id int unsigned);

-- 查看錶的建立語句
-- show create table 表名字;

show create table xxx;

-- 修改表-添加欄位 mascot (吉祥物)
-- alter table 表名 add 列名 類型;

alter table classes add jixiangwu varchar(20);

-- 修改表-修改欄位:不重新命名版
-- alter table 表名 modify 列名 類型及約束;

alter table classes modify jixiangwu varchar(30);

-- 修改表-修改欄位:重新命名版
-- alter table 表名 change 原名 新名 類型及約束;

alter table classes change jixiangwu mascot varchar(20);

-- 修改表-刪除欄位
-- alter table 表名 drop 列名;

alter table classes drop mascot;

-- 刪除表
-- drop table 表名;
-- drop database 資料庫;


--03 增刪改查(curd)


-- 增加

-- 全列插入
-- insert [into] 表名 values(...)
-- 主鍵欄位 可以用 0 null default 來佔位
-- 向classes表中插入 一個班級

insert into classes values(0,"python01");

 

-- 向students表插入 一個學生資訊

insert into students values(0,"班主任",18,166.66,"女",408);

 

-- 部分插入
-- insert into 表名(列1,...) values(值1,...)

insert into students(name,high) values("吳彥祖",188.88);

 

-- 多行插入

insert into students values(default,"謝晨",18,188.88,1,408),(0,"剛哥",18,177.77,"男",408);

-- 修改
-- update 表名 set 列1=值1,列2=值2... where 條件;
-- 全部修改

update students set high = 170.00;

-- 按條件修改

update students set high = 188.88 where id = 2;update hero set contry = "諾克薩斯" where id = 2;

-- 按條件修改多個值
-- update students set gender ="",name = "xxx" ;

update students set age = 28,cls_id = 408 where id = 2;


-- 查詢基本使用
-- 查詢所有列
-- select * from 表名;

select * from xxx;

---定條件查詢

select * from students where id = 2;


-- 查詢指定列
-- select 列1,列2,... from 表名;

select name,gender from students;

-- 可以使用as為列或表指定別名
-- select 欄位[as 別名] , 欄位[as 別名] from 資料表;

select name as "姓名",gender as "性別" from students;


-- 欄位的順序

select gender as "性別",name as "姓名" from students;

-- 刪除
-- 物理刪除
-- delete from 表名 where 條件

delete from students where id = 2;


-- 邏輯刪除
-- 用一個欄位來表示 這條資訊是否已經不能再使用了
-- 給students表添加一個 is_delete 欄位 bit 類型

alter table students add is_delete bit default 0;update students set is_delete = 1 where id = 3;

 


-- 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.