MySQL 2 資料型別與運算元據表

2021-08-08 01:40:10 字數 3707 閱讀 9877

mysql資料型別之整型

資料型別:

決定資料儲存格式,代表不同的資訊型別。

整型

有符號位:數字最高位01表示正負

mysql資料型別之浮點型

注意m和d,一定m>=d

mysql資料型別之日期時間型

列型別 儲存需求

year 1

time 3

data 3

datetime 8

timesstamp(時間戳) 4

mysql資料型別之字元型

char 定長

varchar 變長

enum ()內多選1

set ()內多選多

mysql建立資料表

資料表是資料庫最重要組成部分之一,是其他物件的基礎。

use開啟資料路;

use資料庫名稱;

select database();

確定當前開啟的資料庫是啥

建立資料表

·create table [if not exists] table_name (

column_name data_type,

…. )

舉例:

create table tb1(username varchar(20),age tinyint unsigned,salary float(8,2) unsigned)

mysql檢視資料表

show tables [from db_name] [ like 『pattern』|where expr]

舉例:show tables from mysql;

mysql檢視資料表結構

show columns from tbl_name;

舉例:show columns from tb1;

記錄的插入與查詢

insert

插入記錄

insert [into] tbl_name [(col_name, ….)] values(val, ….)

舉例:insert tb1 values (『tom』,25,7863.25);

部分插入

insert tb1 (username,salary) values (『john』,4500.69);

記錄查詢

select

select expr,….. from tbl_name

舉例 select * from tb1;

分析:*是字段的過濾(?)

mysql空值與非空

·null,字段值可以為空

·not null,字段值禁止為空

舉例:create table tb2(username varchar(20) not null, age tinyint unsigned null);

show columns from tb2;

insert tb2 values (『tom』,null);

可以插入

insert tb2 values (null,26);

插入錯誤

mysql自動編號

auto_increment

·自動編號,且必須與主鍵組合使用

·預設起始為1,增量為1

舉例:create table tb2(id smallint unsigned auto_increment, username varchar(20) not null);

分析:報錯,因為自動編號字段,必須定義為主鍵,下學習主鍵

mysql初涉主鍵約束

primary key

·主鍵約束

·每張資料表只能存在乙個主鍵

·主鍵保證記錄的唯一新

·主鍵自動為not null;

create table tb3(id smallint unsigned auto_increment primary key, username varchar(30) not null);

insert tb3(username) values (『tom』);

insert tb3(username) values (『john』);

insert tb3(username) values (『rose』);

insert tb3(username) values (『dimitar』);

select * from tb3;

auto_increment必須配合主鍵,主鍵不一定需要auto_increment

舉例:create table tb4(id smallint unsigned primary key, username varchar(30) not null);

show columns from tb4;

此時主鍵可以賦值,但不能賦相同值。

舉例:insert tb4 values (4,』tom』);

insert tb4 values (22,』john』);

select * from tb4;

insert tb4 values (22,』rose』);

mysql初涉唯一約束

unique key

·唯一約束

·唯一約束可以保證記錄的唯一性

·位置約束的字段可以為空(null)只能乙個空值

·每張資料表可以存在多個唯一約束

舉例:create table tb5(id smallint unsigned auto_increment primary key, username varchar(30) not null unique key,age tinyint unsigned);

show columns from tb5;

insert tb5(username,age) values (『rose』,22);

成功 insert tb5(username,age) values (『rose』,22);

失敗mysql初涉預設約束

default

·預設值

·當插入記錄,若無幅值,則預設值

舉例create table tb6(id smallint unsigned auto_increment primary key, username varchar(30) not null unique key, *** enum(『1』,』2』,』3』) default 『3』);

show columns from tb6;

insert tb6(username) values (『tom』);

select * from tb6;

MySQL之資料型別與運算元據表

檢視資料庫 show databases 建立資料庫 create database test 進入開啟資料庫 use test 檢視當前在哪個資料庫 select database 建立乙個表 create table th1 column name data type,檢視資料表 show ta...

MySQL 資料型別與運算元據表 二

一 資料型別 整型 浮點型 日期時間型別 字元型 建立資料表 creater table if not exist table name column name data type,檢視資料表 show tables from db name like pattern where expr 檢視資料...

MySQL資料型別和運算元據表

tinyint 1位元組 有符號值 128到127 2 7 到 2 7 1 無符號值 0 到 255 0 到 2 8 1 smallint 2位元組 有符號值 3278 到 32767 2 15 到 2 15 1 無符號值 0 到 65535 0 到 2 16 1 mediumint 3位元組 有符...