MySql 操作表的語句以及常用的字段型別

2021-07-05 20:00:42 字數 2027 閱讀 8432

一.欄位型別

字元:varchar(12)

二級製大資料:vlob

大文字:text

整形:tinyint,smallint,int,bigint

浮點型:float,double

邏輯型:bit

日期型:date,time,datetime,timestamp

二.表的建立

示例建立乙個員工表employee:

create table employee(

id int,

name varchar(20),

gender bit,

birthday date,

entry_date date,

job varchar(40),

salary double,

resume text);

主鍵約束:在建立表的時候在字段後寫上primary key則為主鍵,主鍵不能重複也不能為空;

primary key   

例子:create table employee(

id int primary key   ,

name varchar(20),

gender bit,

birthday date,

entry_date date,

job varchar(40),

salary double,

resume text );

自增長:

由於主鍵不能為空不能重複,所以在插入資料的時候為了保證滿足以上條件我們可以把主鍵設定為自增長

auto_increment;

在建立表的字段後加上這句話則該字段會自增長。

例子:create table employee(

id int

primary key  auto_increment,

name varchar(20),

gender bit,

birthday date,

entry_date date,

job varchar(40),

salary double,

resume text );

唯一約束:使得字段不能重複

unique

在建立表的時候,在字段後加上unique.那這個欄位就不能為重複的了。

非空約束:使得字段不能為空

not null

在建立表的時候,在字段後加上 not null,那麼這個欄位就不能為空的了。

例子:

create table employee(

id int

primary key  auto_increment,

name varchar(20) not null,

gender bit,

birthday date,

entry_date date,

job varchar(40),

salary double unique,

resume text  );

三.檢視表的結構

desc [表名]

四.刪除表

drop table [表名];

五.修改表

增加乙個字段:

alter table employee add image blob;

修改 job varchar(40)的長度為45:

alter table employee modify job varchar(45);

刪除乙個字段:

alter table employee drop gender;

修改表名:

修改表名employee為employee2

rename table employee to employee2;

修改表的字符集:

alter table employee2 character set gbk;

修改欄位名或型別:

alter table employee2  change name jobb varchar(50);

mysql操作語句 mysql常用操作語句

2.列出資料庫 3.選擇資料庫 use databases name 4.列出資料表 5.顯示 列的屬性 show columnsfromtable name describe table name 6.匯出整個資料庫 my例如 my 7.匯出乙個表 mysqldump u user name p ...

常用的MySQL操作語句 以及資料的增刪改查

顯示當前資料庫的時間 檢視所有資料庫 建立資料庫 檢視建立資料庫的建立語句 刪除資料庫 使用資料庫 檢視當前使用的資料庫 建立資料表 例如 creat table hero id int primary key not null auto increment,name varchar 30 例如 c...

MySQL常用操作語句

建立utf 8格式的資料庫 create database dbname default character set utf8 collate utf8 general ci修改資料庫字符集 alter database dbname default character set utf8 colla...