數(shù)據(jù)庫和表操作

-- 創(chuàng)建數(shù)據(jù)庫
-- 創(chuàng)建 classroom 數(shù)據(jù)庫
create database 數(shù)據(jù)庫名 default character set 字符編碼 collate 排序規(guī)則;
    eg:
create database classroom default character set utf8 collate utf8_general_ci;

-- 查看所有數(shù)據(jù)庫
show databases;

-- 選擇數(shù)據(jù)庫
use 數(shù)據(jù)庫名;
    eg:
use classroom;

-- 刪除數(shù)據(jù)庫
drop database 數(shù)據(jù)庫名;
    eg:
drop database classroom;


-- 創(chuàng)建表
-- 創(chuàng)建 class 表
create table 表名(字段名 數(shù)據(jù)類型[長度] 屬性[非空性 默認值 自增 主鍵 注釋])charset=utf8,engine=innodb;
    eg:
create table class (
id int (11) not null auto_increment primary key comment '學號',
name varchar(20) not null comment '姓名',
sex varchar(2) not null comment '性別',
age int (3) not null comment '年齡',
address varchar(255) not null comment '重慶'
);


-- 修改 表
-- 添加字段
alter table 表名 add 字段名 數(shù)據(jù)類型 屬性;
    eg:
alter table class add stu_name varchar(255) null;
alter table class add birthday timestamp;    -- 時間 日期

-- 修改字段
alter table 表名 change 字段 新字段 類型(參數(shù)) 屬性;
    eg:
alter table class change stuname name varchar(20) not null;


-- 刪除字段
alter table 表名 drop 字段;
    eg:
alter table class drop name;

-- 增加主鍵
alter table 表名 add primary key (字段);
    eg:
alter table class add primary key (id);

-- 修改 表名
alter table 表名 rename to 新名;
    eg:
alter table class rename to class_one;


-- 復制 表
-- 方法1:不能復制鍵;
create table 新表 select * from 舊表; 
    eg:
create table class1 select * from class;


-- 方法2:全表賦值;
create table 新表 like 舊表;
    eg:
create table class1 like class;


-- 刪除 表
-- 刪除 單表
drop table 表名;
    eg:
drop table class1;

-- 刪除 多表
drop table 表名1,表名2,...表名n;
drop table class,class1,class2;

標題名稱:數(shù)據(jù)庫和表操作
文章路徑:http://www.muchs.cn/article48/ghgshp.html

成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供App設計、網(wǎng)站內鏈企業(yè)建站、搜索引擎優(yōu)化、網(wǎng)站策劃網(wǎng)站制作

廣告

聲明:本網(wǎng)站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)

搜索引擎優(yōu)化