1. 程式人生 > >Mysql資料庫基本操作(2)

Mysql資料庫基本操作(2)

-- 資料庫的操作

    -- 連結資料庫
    mysql -uroot -p
    mysql -uroot -pmysql

    -- 退出資料庫
    exit/quit/ctrl+d
    

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

    -- 顯示時間
    select now();

    -- 檢視所有資料庫
    show databases;
    

    -- 建立資料庫
    -- create database 資料庫名 charset=utf8;
    create
database python04; create database python04new charset=utf8; -- 檢視建立資料庫的語句 -- show crate database .... show create database python04; -- 檢視當前使用的資料庫 select database(); -- 使用資料庫 -- use 資料庫的名字 use python04new; -- 刪除資料庫 -- drop database 資料庫名; drop database
python04; -- 資料表的操作 -- 檢視當前資料庫中所有表 show tables; -- 建立表 -- auto_increment表示自動增長 -- not null 表示不能為空 -- primary key 表示主鍵 -- default 預設值 -- create table 資料表名字 (欄位 型別 約束[, 欄位 型別 約束]); create table xxxxx(id int, name varchar(30)); create table yyyyy(id int primary
key not null auto_increment, name varchar(30)); create table zzzzz( id int primary key not null auto_increment, name varchar(30) ); -- 查看錶結構 -- desc 資料表的名字; desc xxxxx; -- 建立students表(id、name、age、high、gender、cls_id) create table students( id int unsigned not null auto_increment primary key, name varchar(30), age tinyint unsigned default 0, high decimal(5,2), gender enum("男", "女", "中性", "保密") default "保密", cls_id int unsigned ); insert into students values(0, "老王", 18, 188.88, "男", 0); select * from students; -- 建立classes表(id、name) create table classes( id int unsigned not null auto_increment primary key, name varchar(30) ); insert into classes values(0, "python04大神"); select * from classes; -- 查看錶的建立語句 -- show create table 表名字; show create table students; -- 修改表-新增欄位 -- alter table 表名 add 列名 型別; alter table students add birthday datetime; -- 修改表-修改欄位:不重新命名版 -- alter table 表名 modify 列名 型別及約束; alter table students modify birthday date; -- 修改表-修改欄位:重新命名版 -- alter table 表名 change 原名 新名 型別及約束; alter table students change birthday birth date default "2000-01-01"; -- 修改表-刪除欄位 -- alter table 表名 drop 列名; alter table students drop high; -- 刪除表 -- drop table 表名; -- drop database 資料庫; -- drop table 資料表; drop table xxxxx; -- 增刪改查(curd) -- 增加 -- 全列插入 -- insert [into] 表名 values(...) -- 主鍵欄位 可以用 0 null default 來佔位 -- 向classes表中插入 一個班級 insert into classes values(0, "菜鳥班"); +--------+-------------------------------------+------+-----+------------+----------------+ | Field | Type | Null | Key | Default | Extra | +--------+-------------------------------------+------+-----+------------+----------------+ | id | int(10) unsigned | NO | PRI | NULL | auto_increment | | name | varchar(30) | YES | | NULL | | | age | tinyint(3) unsigned | YES | | 0 | | | gender | enum('','','中性','保密') | YES | | 保密 | | | cls_id | int(10) unsigned | YES | | NULL | | | birth | date | YES | | 2000-01-01 | | +--------+-------------------------------------+------+-----+------------+----------------+ -- 向students表插入 一個學生資訊 insert into students values(0, "小李飛刀", 20, "女", 1, "1990-01-01"); insert into students values(null, "小李飛刀", 20, "女", 1, "1990-01-01"); insert into students values(default, "小李飛刀", 20, "女", 1, "1990-01-01"); -- 失敗 -- insert into students values(default, "小李飛刀", 20, "第4性別", 1, "1990-02-01"); -- 列舉中 的 下標從1 開始 1---“男” 2--->"女".... insert into students values(default, "小李飛刀", 20, 1, 1, "1990-02-01"); -- 部分插入 -- insert into 表名(列1,...) values(值1,...) insert into students (name, gender) values ("小喬", 2); -- 多行插入 insert into students (name, gender) values ("大喬", 2),("貂蟬", 2); insert into students values(default, "西施", 20, "女", 1, "1990-01-01"), (default, "王昭君", 20, "女", 1, "1990-01-01"); -- 修改 -- update 表名 set 列1=值1,列2=值2... where 條件; update students set gender=1; -- 全部都改 update students set gender=1 where name="小李飛刀"; -- 只要name是小李飛刀的 全部的修改 update students set gender=1 where id=3; -- 只要id為3的 進行修改 update students set age=22, gender=1 where id=3; -- 只要id為3的 進行修改 -- 查詢基本使用 -- 查詢所有列 -- select * from 表名; select * from students; ---定條件查詢 select * from students where name="小李飛刀"; -- 查詢 name為小李飛刀的所有資訊 select * from students where id>3; -- 查詢 name為小李飛刀的所有資訊 -- 查詢指定列 -- select 列1,列2,... from 表名; select name,gender from students; -- 可以使用as為列或表指定別名 -- select 欄位[as 別名] , 欄位[as 別名] from 資料表 where ....; select name as 姓名,gender as 性別 from students; -- 欄位的順序 select id as 序號, gender as 性別, name as 姓名 from students; -- 刪除 -- 物理刪除 -- delete from 表名 where 條件 delete from students; -- 整個資料表中的所有資料全部刪除 delete from students where name="小李飛刀"; -- 邏輯刪除 -- 用一個欄位來表示 這條資訊是否已經不能再使用了 -- 給students表新增一個is_delete欄位 bit 型別 alter table students add is_delete bit default 0; update students set is_delete=1 where id=6;