溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

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

發(fā)布時間:2020-08-10 19:21:18 來源:ITPUB博客 閱讀:166 作者:疑惑尋解 欄目:MySQL數(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;
向AI問一下細節(jié)

免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經查實,將立刻刪除涉嫌侵權內容。

AI