數(shù)據(jù)庫常用操作
操作 | 語句 |
---|
創(chuàng)建數(shù)據(jù)庫 | create database if not exists 數(shù)據(jù)庫名; |
查看所有數(shù)據(jù)庫 | show databases; |
切換數(shù)據(jù)庫 | use 數(shù)據(jù)庫名; |
刪除數(shù)據(jù)庫 | drop database if exists 數(shù)據(jù)庫名; |
修改數(shù)據(jù)庫編碼 | alter database 數(shù)據(jù)庫名 character set utf8; |
表結(jié)構(gòu)常用操作
操作 | 語句 |
---|
創(chuàng)建表 | create table if not exists 表名(字段名 數(shù)據(jù)類型, 字段名 數(shù)據(jù)類型, ...); |
查看當(dāng)前數(shù)據(jù)庫所有表 | show tables; |
查看表結(jié)構(gòu) | desc 表名; |
查看指定表的創(chuàng)建語句 | show create table 表名; |
刪除表 | drop table 表名; |
添加列 | alter table 表名 add 列名 數(shù)據(jù)類型(長度); |
修改列名和類型 | alter table 表名 change 舊列名 新列名數(shù)據(jù)類型(長度); |
刪除列 | alter table 表名 drop 列名; |
修改表名 | rename table 表名 to 新表名; |
增刪改
操作 | 語句 |
---|
向表中插入列 | insert into 表名(列1, 列2, 列3) values(值1, 值1, 值1), (值2, 值2, 值2); |
向表中所有列插入數(shù)據(jù) | insert into 表名 values(值1, 值1, 值1); |
數(shù)據(jù)修改 | update 表名 set 字段名=值, ... , 字段名=值 where 條件 |
刪除表中數(shù)據(jù) | delete from 表名 where 條件 |
刪除表 | truncate 表名 |
MYSQL約束
操作 | 語句 |
---|
主鍵 | primary key |
刪除主鍵約束 | alter table 表名 drop primary key; |
自增長 | auto_increment |
非空 | not null |
創(chuàng)建表前指定 | create table 表名(id int not null, account varchar(20) not null); |
創(chuàng)建表后指定 | alter table 表名 modify id int not null; |
刪除非空約束 | alter table 表名 modify 字段 類型 |
唯一 | unique |
刪除唯一約束 | alter table 表名 drop index 唯一約束名 |
默認(rèn) | default |
刪除默認(rèn)約束 | alter table 表名 modify 列名 類型 default null |
零填充 | zerofill |
刪除零填充約束 | alter table 表名 modify 字段 類型 |
外鍵 | foreign key |
如果您覺得有幫助,請幫忙點(diǎn)一下公眾號文中和最底部的廣告,點(diǎn)一下就可以,謝謝~
本站僅提供存儲(chǔ)服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請
點(diǎn)擊舉報(bào)。