mysql庫和表的管理

2021-09-24 03:55:34 字數 2194 閱讀 8316

ddl(data definition language):資料定義語言

庫和表的管理

一、庫的管理

建立、修改、刪除

二、表的管理

建立、修改、刪除

建立:create

修改:alter

刪除:drop

一、庫的管理

1.庫的建立

語法:create database [if not exists]庫名;

create database if not exists books ;

2、庫的修改

rename database books to 新庫名;

#更改庫的字符集

alter database books character set gbk;

3、庫的刪除

drop database if exists books;

二、表的管理

1.表的建立

語法:

create table 表名(

列名 列的型別【(長度) 約束】,

列名 列的型別【(長度) 約束】,

列名 列的型別【(長度) 約束】,

…列名 列的型別【(長度) 約束】

#案例:建立表book

create table book(

id int,#編號

bname varchar(20),#圖書名

price double,#**

authorid int,#作者編號

publishdate datetime#出版日期

);

2.表的修改

語法

alter table 表名 add|drop|modify|change column 列名 【列型別 約束】;

①修改列名

alter table book change column publishdate pubdate datetime;

#②修改列的型別或約束

alter table book modify column pubdate timestamp;

#③新增新列

alter table author add column annual double;

#④刪除列

alter table book_author drop column annual;

#⑤修改表名

alter table author rename to book_author;

3.表的刪除

drop table if exists book_author;

#通用的寫法:

drop database if exists 舊庫名; create database 新庫名;

drop table if exists 舊表名; create table 表名();

4.表的複製

insert into author values

(1,'村上春樹','日本'),

(2,'莫言','中國'),

(3,'馮唐','中國'),

(4,'金庸','中國');

1.僅僅複製表的結構

create table copy like author;

#2.複製表的結構+資料

create table copy2

select * from author;

#只複製部分資料

create table copy3

select id,au_name

from author

where nation='中國';

#僅僅複製某些字段

create table copy4

select id,au_name

from author

where 0;

mysql庫和表 MySQL庫和表的管理

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

mysql庫和表 MySQL庫和表的管理

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

mysql 庫和表的管理

主要包含庫和表的管理 語法 create database if not exists 庫名 character set 字符集名 create database if not exists books 一般不修改庫名。要是修改的話,找到data資料夾裡去修改。更改庫的字符集 alter datab...