mysql資料庫和資料表的簡單操作

來源:互聯網
上載者:User

標籤:表結構   utf8   from   set   div   sql   劃線   ice   arch   

一.資料庫的增刪改查

1.建立資料庫

CREATE DATABASE 資料庫名 charset utf8;

資料庫名規則:可以由字母、數字、底線、@、#、$ 區分大小寫, 不能使用關鍵字如 create select, 不能單獨使用數字, 最長128位

2.查看資料庫

show databases;show create database 資料庫名;

3.選擇資料庫

USE 資料庫名;

4.刪除資料庫

DROP DATABASE 資料庫名;

5.修改資料庫字元編碼

alter database 資料庫名 charset utf8;

二.資料表的增刪改查

1.建立表

create table 表名(欄位名1 類型[(寬度) 約束條件],欄位名2 類型[(寬度) 約束條件],......);

注意:同一張表中欄位名不能相同,欄位名和類型必選,寬度和約束條件可選

create table test(id int, name varchar(50),age int(3),sex enum(‘male‘,‘female‘));

表中插入資料

insert into test values(1,‘tom‘,18,‘male‘),(2,‘jerry‘,15,‘female‘);

2.查看錶結構

describe test;show create table test\G; #查看錶詳細結構,可加\G顯示

3.修改表結構

a.修改表名

ALTER TABLE 表名 RENAME 新表名;

b.增加表欄位

ALTER TABLE 表名 ADD 欄位名  資料類型 [完整性條件約束條件…],ADD 欄位名  資料類型 [完整性條件約束條件…]; #添加一個或多個欄位ALTER TABLE 表名 ADD 欄位名  資料類型 [完整性條件約束條件…]  FIRST; #在最前面增加欄位ALTER TABLE 表名 ADD 欄位名  資料類型 [完整性條件約束條件…]  AFTER 欄位名; #在某個欄位後增加欄位
alter table student add name varchar(20) not null,add age int(3) not null default 22;alter table student add stu_num varchar(10) not null after name; alter table student add sex enum(‘male‘,‘female‘) default ‘male‘ first; 

c.刪除欄位

ALTER TABLE 表名 DROP 欄位名;
alter table student drop sex;

d.修改欄位

ALTER TABLE 表名  MODIFY  欄位名 資料類型 [完整性條件約束條件…];ALTER TABLE 表名 CHANGE 舊欄位名 新欄位名 舊資料類型 [完整性條件約束條件…];ALTER TABLE 表名 CHANGE 舊欄位名 新欄位名 新資料類型 [完整性條件約束條件…];
alter table student modify id int(11) not null primary key auto_increment; #修改id欄位類型非空主鍵自動成長alter table student modify id int(11) not null; #刪除自動成長alter table student drop primary key; #刪除主鍵

4.複製表

複製表結構+記錄 (不會複製: 主鍵、外鍵和索引)

create table new_service select * from service;

只複製表結構

create table new1_service select * from service where 1=2; #設定條件為假使資料查不到只有結構

5.刪除表

DROP TABLE 表名;

 

mysql資料庫和資料表的簡單操作

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.