14 MySQL學習筆記 十四 DDL語言

2021-09-29 20:56:40 字數 2335 閱讀 5367

資料定義語言

庫和表的管理

一、庫的管理

建立,修改,刪除

二、表的管理

建立、修改、刪除

建立:create

修改:alter

刪除:drop

一、庫的管理

庫的建立

語法:

create database【if not exists】庫名;
案例:建立庫books

create database if not exists books;

庫的修改

更改庫的字符集

alter database books character set gbk;

庫的刪除

drop database if exists books;

二、表的管理

​ 表的建立

語法:

create table 【if not exists】表名(

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

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

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

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

)

案例1:建立表book

create table book(

id int,#編號

bname varchar(20),#圖書名

price double,#**

authorid int,#作者編號

publishdate datetime#出版日期book

);

案例2:建立表author

create table author(

id int,

au_name varchar(20),

nation varchar(10)

);

表的修改

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

修改列名

語法:alter table 表名 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 author drop column annual;

修改表名

alter table author rename to book_author;

表的刪除

drop table if exists book_author;

通用的寫法:

建庫

drop database if exists 舊庫名;

create database 新庫名;

建表

drop table if exists 舊表名;

create table 表名();

表的複製

僅僅複製表的結構

create table copy1 like author;

複製表的資料和結構+資料

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;

14 mysql 常見資料型別

總覽1 數值型 整型 小數 定點數,浮點數 2 字元型 較短的文字 char varchar 較長的文字 text blob 較長的二進位制資料 3 日期型整型分類 tinyint smallint mediumint int integer bigint 1 2 3 4 8 位元組數 如何設定無符...

14 MySQL基礎之流程控制函式

1 if expr1,expr2,expr3 函式 功能 若expr1為true,則返回expr2,否則返回expr3 示例 selectif 10 5,1,2 2 case表示式 語法一 case 要判斷的字段或表示式 when 常量1 then 要顯示的值1 或 語句1 when 常量2 the...

MySQL學習 十四

utf8的bom問題 在xp下,用記事本建立utf8檔案的時候,前面多了3個位元組,這3個位元組不用來顯示,是用來辨識編碼用的,ef bb bf告訴記事本,這是utf8編碼。儲存引擎和事務簡單介紹 引擎是mysql儲存資料的不同方式。myisam不支援事務,innodb支援事務 事務應該具有的4個屬...