語(yǔ)法格式:
create table tableName(
columnName dataType(length),
………………..
columnName dataType(length)
);
set character_set_results='gbk';
show variables like '%char%';
創(chuàng)建表的時(shí)候,表中有字段,每一個(gè)字段有:
* 字段名
* 字段數(shù)據(jù)類(lèi)型
* 字段長(zhǎng)度限制
* 字段約束
MySql常用數(shù)據(jù)類(lèi)型
類(lèi)型 |
描述 |
Char(長(zhǎng)度) |
定長(zhǎng)字符串,存儲(chǔ)空間大小固定,適合作為主鍵或外鍵 |
Varchar(長(zhǎng)度) |
變長(zhǎng)字符串,存儲(chǔ)空間等于實(shí)際數(shù)據(jù)空間 |
double(有效數(shù)字位數(shù),小數(shù)位) |
數(shù)值型 |
Float(有效數(shù)字位數(shù),小數(shù)位) |
數(shù)值型 |
Int( 長(zhǎng)度) |
整型 |
bigint(長(zhǎng)度) |
長(zhǎng)整型 |
Date |
日期型 |
BLOB |
Binary Large OBject(二進(jìn)制大對(duì)象) |
CLOB |
Character Large OBject(字符大對(duì)象) |
其它………………… |
|
建立學(xué)生信息表,字段包括:學(xué)號(hào)、姓名、性別、出生日期、email、班級(jí)標(biāo)識(shí)
create table t_student(
student_id int(10),
student_name varchar(20),
sex char(2),
birthday date,
email varchar(30),
classes_id int(3)
)
向t_student表中加入數(shù)據(jù),(必須使用客戶(hù)端軟件,我們的cmd默認(rèn)是GBK編碼,數(shù)據(jù)中設(shè)置的編碼是UTF-8)
insert into t_student(student_id, student_name, sex, birthday, email, classes_id) values(1001, 'zhangsan', 'm', '1988-01-01', 'qqq@163.com', 10)
向t_student表中加入數(shù)據(jù)(使用默認(rèn)值)
drop table if exists t_student;
create table t_student(
student_id int(10),
student_name varchar(20),
sex char(2) default 'm',
birthday date,
email varchar(30),
classes_id int(3)
)
insert into t_student(student_id, student_name, birthday, email, classes_id)
values
(1002, 'zhangsan', '1988-01-01', 'qqq@163.com', 10)