Mysql學(xué)習(xí)筆記二 數(shù)據(jù)表的基礎(chǔ)操作

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

  • 查看當(dāng)前數(shù)據(jù)庫(kù)中所有表
mysql> show tables; -- 顯示指定數(shù)據(jù)庫(kù)的所有表,使用該命令前需要使用 use 命令來(lái)選擇要操作的數(shù)據(jù)庫(kù)。
+------------------+
| Tables_in_demo   |
+------------------+
| bm_list          |
| rhs_users        |
+------------------+
2 rows in set (0.00 sec)
  • 創(chuàng)建表
-- auto_increment表示自動(dòng)增長(zhǎng)
-- 創(chuàng)建一個(gè)學(xué)生的數(shù)據(jù)表(id、name、age、high、gender、cls_id)
-- create table 數(shù)據(jù)表名字 (字段 類(lèi)型 約束[, 字段 類(lèi)型 約束]);   -- [] 表示可有可無(wú)
-- 多個(gè)約束 不分先后順序
-- enum 表示枚舉
-- 最后一個(gè)字段不要添加逗號(hào)
-- unsigned: 無(wú)符號(hào), 不能夠存儲(chǔ)負(fù)數(shù)

-- 創(chuàng)建students表
mysql> create table students(
    ->         id int unsigned primary key auto_increment not null,
    ->         name varchar(10) not null,
    ->         age tinyint unsigned default 0,
    ->         high decimal(5,2) default 0.0,   -- 180.88
    ->         gender enum("男", "女", "中性", "保密") default "保密",
    ->         cls_id int unsigned not null
    ->     );
Query OK, 0 rows affected (0.02 sec)
  • 查看表的創(chuàng)建語(yǔ)句
mysql> mysql> show create table students;
+----------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table    | Create Table                                                                                                                                                                                                                                                                                                                                              |
+----------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| students | CREATE TABLE `students` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `name` varchar(10) NOT NULL,
  `age` tinyint(3) unsigned DEFAULT '0',
  `high` decimal(5,2) DEFAULT '0.00',
  `gender` enum('男','女','中性','保密') DEFAULT '保密',
  `cls_id` int(10) unsigned NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8         |
+----------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
  • 查看表結(jié)構(gòu)
mysql> desc students;  -- show columns form students;也可以 -- 使用頻率非常高
+--------+-------------------------------------+------+-----+---------+----------------+
| Field  | Type                                | Null | Key | Default | Extra          |
+--------+-------------------------------------+------+-----+---------+----------------+
| id     | int(10) unsigned                    | NO   | PRI | NULL    | auto_increment |
| name   | varchar(10)                         | NO   |     | NULL    |                |
| age    | tinyint(3) unsigned                 | YES  |     | 0       |                |
| high   | decimal(5,2)                        | YES  |     | 0.00    |                |
| gender | enum('男','女','中性','保密')         | YES  |     | 保密     |                |
| cls_id | int(10) unsigned                    | NO   |     | NULL    |                |
+--------+-------------------------------------+------+-----+---------+----------------+
6 rows in set (0.00 sec)

顯示數(shù)據(jù)表的屬性,屬性類(lèi)型,主鍵信息 ,是否為 NULL,默認(rèn)值等其他信息。

  • 查看數(shù)據(jù)表的詳細(xì)索引信息
show index from students;
image
  • 修改表結(jié)構(gòu)
-- add  -添加字段
-- 用法: alter table 表名 add 列名 類(lèi)型/約束;

-- 給students表添加‘birthday‘字段,默認(rèn)值為"2011-11-11 11:11:11"

mysql> alter table students add birthday date default "2011-11-11";
Query OK, 0 rows affected (0.02 sec)
Records: 0  Duplicates: 0  Warnings: 0

-- 結(jié)果
mysql> desc students;
+----------+-----------------------------+------+-----+------------+----------------+
| Field    | Type                        | Null | Key | Default    | Extra          |
+----------+-----------------------------+------+-----+------------+----------------+
| id       | int(10) unsigned            | NO   | PRI | NULL       | auto_increment |
| name     | varchar(10)                 | NO   |     | NULL       |                |
| age      | tinyint(3) unsigned         | YES  |     | 0          |                |
| high     | decimal(5,2)                | YES  |     | 0.00       |                |
| gender   | enum('男','女','中性','保密') | YES  |     | 保密       |                |
| cls_id   | int(10) unsigned            | NO   |     | NULL       |                |
| birthday | date                        | YES  |     | 2011-11-11 |                |
+----------+-----------------------------+------+-----+------------+----------------+
7 rows in set (0.00 sec)
-- modify -修改表字段值  不重命名版
-- 用法: alter table 表名 modify 列名 類(lèi)型及約束;

alter table students modify birthday date default "2011-11-11";
-- change -修改表字段 重命名版
-- 用法: alter table 表名 change 原列名 新列名 類(lèi)型及約束;


mysql> alter table students change birthday birth date default "2011-11-11";
Query OK, 0 rows affected (0.01 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> desc students;
+--------+------------------------------+------+-----+------------+----------------+
| Field  | Type                         | Null | Key | Default    | Extra          |
+--------+------------------------------+------+-----+------------+----------------+
| id     | int(10) unsigned             | NO   | PRI | NULL       | auto_increment |
| name   | varchar(10)                  | NO   |     | NULL       |                |
| age    | tinyint(3) unsigned          | YES  |     | 0          |                |
| high   | decimal(5,2)                 | YES  |     | 0.00       |                |
| gender | enum('男','女','中性','保密')  | YES  |     | 保密       |                |
| cls_id | int(10) unsigned             | NO   |     | NULL       |                |
| birth  | date                         | YES  |     | 2011-11-11 |                |
+--------+------------------------------+------+-----+------------+----------------+
7 rows in set (0.00 sec)
-- 刪除字段  
-- 用法: alter table 表名 drop 字段名

alter table students drop birth;

-- 刪除表
-- 用法: alter table 表名

drop table students;

一、Mysql學(xué)習(xí)筆記目錄

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

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

  • 一、數(shù)據(jù)庫(kù)操作 - 創(chuàng)建數(shù)據(jù)庫(kù) 先通過(guò)show databases;命令查看現(xiàn)有數(shù)據(jù)庫(kù),結(jié)果如下: 創(chuàng)建一個(gè)新的數(shù)...
    陌問(wèn)MW閱讀 1,282評(píng)論 0 5
  • 數(shù)據(jù)表是數(shù)據(jù)庫(kù)中最重要、最基本的操作對(duì)象,是數(shù)據(jù)存儲(chǔ)的基本單位。數(shù)據(jù)表被定義為列的集合,數(shù)據(jù)在表中是按照行和列的格...
    假正經(jīng)乄閱讀 2,083評(píng)論 0 2
  • 國(guó)家電網(wǎng)公司企業(yè)標(biāo)準(zhǔn)(Q/GDW)- 面向?qū)ο蟮挠秒娦畔?shù)據(jù)交換協(xié)議 - 報(bào)批稿:20170802 前言: 排版 ...
    庭說(shuō)閱讀 12,496評(píng)論 6 13
  • 本文主要內(nèi)容來(lái)自慕課網(wǎng)。配合視頻食用口味更佳 主要是順著已經(jīng)學(xué)習(xí)的視頻順序總結(jié)一遍,以深化理解和方便日后復(fù)習(xí) 一些...
    stoneyang94閱讀 1,561評(píng)論 0 1
  • MySQL5.6從零開(kāi)始學(xué) 第一章 初始mysql 1.1數(shù)據(jù)庫(kù)基礎(chǔ) 數(shù)據(jù)庫(kù)是由一批數(shù)據(jù)構(gòu)成的有序的集合,這些數(shù)據(jù)...
    星期四晚八點(diǎn)閱讀 1,237評(píng)論 0 4

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