sql命令(二) 資料型別與運算元據表

2021-09-24 11:00:46 字數 1889 閱讀 7170

2019獨角獸企業重金招聘python工程師標準》

1,資料型別:

m是有效數字個數

d是小數點個數

2,建立資料表

use t1;//使用(開啟)資料庫t1

select database();//檢視當前開啟的資料庫

create table tb1 ( //建立表tb1

username varchar (20),

age tinyint unsigned,

salary float (8, 2) unsigned

);show tables;//檢視資料表列表

show tables from mysql;//檢視指定資料的資料列表

show create table provinces;//檢視表provinces的建立命令

3,檢視資料表結構

show columns from tb1;//檢視資料表tb1 的資料結構

4,記錄的插入

insert tb1 values('tom',25,789.25);//為所有字段插入值

insert tb1(username,salary) values('john',4500.32);//為部分字段插入值

5,記錄的查詢

select * from tb1;//檢視資料表中的全部資料

6,空值與非空值

null //在記錄插入時,可以不對該字段插入值

not null //在插入記錄時,該欄位必須要有值並且值不能為null

create table tb2 (

username varchar (20) 

not null

,  //建立**tb2,username欄位不能為null

age tinyint unsigned

);7,自動編號與主鍵約束

主鍵約束:主鍵自動為not null,主鍵保證記錄的唯一性,每張表只能存在乙個主鍵。

create table tb3 (   //自動編號必須是主鍵,

id smallint unsigned

auto_increment primary key

,username varchar(30) not null

);8,唯一約束(unique key)

唯一約束可以為空值(只能有乙個空值)

每張表可以存在多個唯一約束。

create table tb5 (

id smallint unsigned auto_increment primary key,

username varchar (20) not null

unique key

,age tinyint unsigned

);9,預設約束

在寫入記錄時,如果沒有明確為字段賦值,則自動賦予預設值

create table tb6(

id smallint unsigned auto_increment primary key,

username varchar(20) not null unique key,

*** enum('1','2','3') 

default 

'3');

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

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

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

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

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位元組 有符...