數(shù)據(jù)庫相關(guān)

一、 數(shù)據(jù)庫的操作

-- 鏈接數(shù)據(jù)庫
mysql -uroot -p
mysql -uroot -pmysql

-- 退出數(shù)據(jù)庫
exit/quit/ctrl+d


-- sql語句最后需要有分號;結(jié)尾
-- 顯示數(shù)據(jù)庫版本
select version();

-- 顯示時間
select now();

-- 查看所有數(shù)據(jù)庫
show databases;


-- 創(chuàng)建數(shù)據(jù)庫
-- create database 數(shù)據(jù)庫名 charset=utf8;
create database python04;
create database python04new charset=utf8;


-- 查看創(chuàng)建數(shù)據(jù)庫的語句
-- show crate database ....
show create database python04;


-- 查看當(dāng)前使用的數(shù)據(jù)庫
select database();

-- 使用數(shù)據(jù)庫
-- use 數(shù)據(jù)庫的名字
use python04new;

-- 刪除數(shù)據(jù)庫
-- drop database 數(shù)據(jù)庫名;
drop database python04;

二、 數(shù)據(jù)表的操作

-- 查看當(dāng)前數(shù)據(jù)庫中所有表
show tables;


-- 創(chuàng)建表
-- auto_increment表示自動增長
-- not null 表示不能為空
-- primary key 表示主鍵
-- default 默認(rèn)值
-- create table 數(shù)據(jù)表名字 (字段 類型 約束[, 字段 類型 約束]);
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)
);

-- 查看表結(jié)構(gòu)
-- desc 數(shù)據(jù)表的名字;
desc xxxxx;

-- 創(chuàng)建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;

-- 創(chuàng)建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;

-- 查看表的創(chuàng)建語句
-- 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 數(shù)據(jù)庫;
-- drop table 數(shù)據(jù)表;
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表插入 一個學(xué)生信息
    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");

    -- 枚舉中 的 下標(biāo)從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 數(shù)據(jù)表 where ....;
    select name as 姓名,gender as 性別 from students;


    -- 字段的順序
    select id as 序號, gender as 性別, name as 姓名 from students;


-- 刪除
    -- 物理刪除
    -- delete from 表名 where 條件
    delete from students; -- 整個數(shù)據(jù)表中的所有數(shù)據(jù)全部刪除
    delete from students where name="小李飛刀";

    -- 邏輯刪除
    -- 用一個字段來表示 這條信息是否已經(jīng)不能再使用了
    -- 給students表添加一個is_delete字段 bit 類型
    alter table students add is_delete bit default 0;
    update students set is_delete=1 where id=6;
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容