1. 程式人生 > >My SQL Case_1: 建立學生表和班級表

My SQL Case_1: 建立學生表和班級表

# 建立資料庫studyhe
create database studyhe charset=utf8;

# 檢視當前使用的資料庫
SELECT database();

# 使用studyhe資料庫
use studyhe;

# 顯示studyhe資料庫下所有表
show tables;

# 建立學生表

# 注意:
# unsigned 無符號型別,因為數值型別分兩類,有符號與無符號
# int unsigned 是型別,後面是約束
# tinyint 0-255 無符號範圍
# decimal 共5位,2個小數
# enum列舉,後面還可以寫約束

create table student(
		id int UNSIGNED not null auto_increment primary key,
		name varchar(30),
		age TINYINT unsigned,
		high decimal(5,2),
		gender enum('男','女','保密') DEFAULT '保密',
		cls_id int UNSIGNED
)
# 查看錶結構
desc student

# 插入資料
insert into student values(0, '老王', 18, 188.88, '男', 0);

# 檢視student資料
select * from student;

# 建立班級表
create table classes(
    id int unsigned auto_increment primary key not null,
    name varchar(10)
);

# 顯示資料庫下的表,是否新增了classes
show tables;

# 檢視classes資料
select * from classes;

# 插入值
insert into classes values(1, '大神');

補充:

  1. 學生表結構顯示:
    在這裡插入圖片描述

  2. 學生表全部資料顯示:
    在這裡插入圖片描述

  3. 型別中的數值型別:
    在這裡插入圖片描述

  4. 型別中的字串型別:
    在這裡插入圖片描述

  5. 型別中的日期時間型別:
    在這裡插入圖片描述

  6. 資料的完整性到底指的什麼?

    一個數據庫就是一個完整的業務單元,可以包含多張表,資料被儲存在表中
    在表中為了更加準確的儲存資料,保證資料的正確有效,可以在建立表的時候,為表新增一些強制性的驗證,包括資料欄位的型別、約束