1. 程式人生 > 其它 >SQL語言DDL

SQL語言DDL

MySQL資料庫基本操作-DDL

-- ctrl+/和# :註釋
-- SQL語言不區分大小寫;

DDL:資料定義語言;

 

對資料庫的常用操作;

-- 檢視所有的資料庫;
show databases;
-- 建立資料庫 create database shanghai; create database if not exists shanghai;
-- 選擇要操作的資料庫 use shanghai
-- 刪除資料庫 drop database shanghai; drop database if exists shanghai;
-- 修改資料庫編碼 alter database shanghai character
set utf8;

對錶結構的常用操作;

建立表
是構建一張空表,指定這個表的名字,
這個表有幾列,每一列叫什麼名字,以及每一列儲存的資料型別。 建立表格式: create table (if not exists) 表名( 列名 型別 (寬度) (約束條件) (comment ‘欄位說明‘), 列名 型別 (寬度) (約束條件) (comment ‘欄位說明‘) );
#建立表
create table if not exists student1(
        sid int,
        name varchar
(20), gender varchar(20), age int, birth date, address varchar(20), score double );
資料型別是指在建立表的時候為表中欄位指定資料型別,只有資料符合型別要求才能儲存起來。
(使用資料型別的原則是:夠用就行,儘量使用取值範圍小的,而不用大的,這樣可以更多·的節省儲存空間;)
數值型別:
int:整數值; float:浮點值; 字串型別: varchar:字串; varchar20); 日期型別: date:年月日(1000-10-10);
其它操作
-- 檢視當前資料庫的所有表名稱; show tables;
-- 檢視指定某個表的建立語句; show create table student1;
-- 點選執行結果的create table, -- ctrl+a全選,ctrl+c複製,ctrl+v貼上 CREATE TABLE `student1` ( `sid` int DEFAULT NULL, `name` varchar(20) DEFAULT NULL, `gender` varchar(20) DEFAULT NULL, `age` int DEFAULT NULL, `birth` date DEFAULT NULL, `address` varchar(20) DEFAULT NULL, `score` double DEFAULT NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3
-- 查看錶結構 desc student1;
-- 刪除表 drop table student1;

修改表結構;

-- 修改表結構

-- 修改表新增列:
-- alter  table  表名  add  列名   型別(長度)  (約束);
alter table student1 add num int; 
-- 修改列名和型別
-- alter table  表名  change 舊列名   新列名  型別(長度) (約束);
alter table student1 change num number int; 
-- 刪除列
-- alter table 表名 drop 列名;
alter table student1 drop number;
-- 修改表名
-- rename table 表名 to 新表名;
rename table student1 to student;