mysql庫和表 MySQL庫和表的管理

2021-10-18 22:21:35 字數 1947 閱讀 5229

mysql資料庫服務配置好後,系統會有4個預設的資料庫.

information_schema:虛擬物件,其物件都儲存在記憶體中

performance_schema:伺服器效能指標庫

mysql:記錄使用者許可權,幫助,日誌等資訊

test:測試庫

mysql資料庫及表的管理

1.查詢所有資料庫

mysql> show databases;

2.建立資料庫

語法:create [if not exists] db_name

預設指定編碼格式為utf-8

mysql> create database if not exist db_name;

自定義編碼格式

mysql> create database db_name default character set utf8;

刪除資料庫

mysql> drop database if exists db_name;

3.修改資料庫

查詢編碼格式

mysql> show create database db_name;

修改編碼格式

alter database db_name default character set gbk;

4.管理表

1.建立表及檢視表

檢視表use db_name;

show tables;

檢視表結構

desc tbl_name;

檢視表中的列

show columns from tbl_name;

檢視表的狀態資訊

show table status like 『tbl_name『;

建立表語法:

create [temporary] table [if not exists] tbl_name (

... 字段定義 ...,

model varchar(20) not null,

... 字段定義 ...

建立表:

mysql> create table tbl_name(

-> id int(11) not null auto_increment,

-> name char(16) not null,

-> age int default null,

-> address char(50) not null,

-> primary key(id)

-> )engine=innodb default charset=utf8

query ok, 0 rows affected (0.02 sec)

5.修改表的結構,使用alter命令

給表中新增字段

alter table tbl_name add column 欄位名 varchar(5);

在表中刪除字段

alter table tbl_name drop column 欄位名;

在表中新增id欄位

alter table tbl_name add id int not null

primary key auto_increment first;

刪除資料表:

mysql> drop table tbl_name;

將表中記錄清空:

mysql> delete from tbl_name;

mysql>truncate table tbl_name;

重新命名表:

rename table table_name to tbl_name;

複製表資料(資料一樣結構不同)

create table t2 select * from t1;

複製表結構

create table 新錶 select * from 舊表where 1=2

或者create table 新錶 like 舊表

原文:

mysql庫和表 MySQL庫和表的管理

mysql資料庫服務配置好後,系統會有4個預設的資料庫.information schema 虛擬物件,其物件都儲存在記憶體中 performance schema 伺服器效能指標庫 mysql 記錄使用者許可權,幫助,日誌等資訊 test 測試庫 mysql資料庫及表的管理 1.查詢所有資料庫 m...

mysql庫和表 Mysql中關於庫和表的管理

一 庫的管理 1.庫的建立 create database if not exists 庫名 2.庫的修改 一般建立後不修改 rename database 原庫名 to 新庫名 3.庫的刪除 drop database if exists 庫名 二 表的管理 1.表的建立 create table...

mysql跨庫複製 mysql跨庫表結構和資料複製

1.將資料從現有表複製到新的資料,例如備份資料和複製生產資料進行測試。我們要想將資料從舊表複製到新錶,那我們需要使用的sql案例如下 create table new table select col,col2,col3 from existing table 首先使用create table語句中...