mysql 基本操作

來源:互聯網
上載者:User

標籤:建立   小數   address   完整性   預設   無法   表數   資訊   alt   

資料庫操作:

---查看warnings內容
show warnings

---查看當前在那個資料庫
select database();

---查看有那些資料庫
show databases;

---查看資料庫建立資訊
show create database s3;

---建立資料庫
create database if not exists s3;

---建立資料庫 設定編碼
create database if not exists s4 character set utf8;

---修改資料庫編碼
alter database s4 character set gbk;

---刪除資料庫
drop database s4;

建立表

/////////////表處理(主鍵:非空且唯一) 非空:not null 唯一:unique float(4,2)表示6位元,小數點數2位 最大99.99
---建立表
create table emp(
id INT primary KEY auto_increment,
name VARCHAR(30),
age TINYINT DEFAULT 100,
salary FLOAT(9,2)
);

 

---多欄位聯合主鍵
create table users2(
id INT,
name varchar(20),
city varchar(20),
primary key(name,id)
);

 

查看錶資訊

---查看錶建立資訊
desc tab_name 查看錶結構
show columns from tab_name 查看錶結構
show tables 查看當前資料庫中的所有的表
show create table tab_name 查看當前資料庫表建表語句


修改表資訊

----修改表結構
--增加列(欄位) alter table tab_name add [column] 列名 類型[完整性條件約束條件][first|after 欄位名];
ALTER TABLE emp ADD address TEXT;
#添加多個欄位
ALTER TABLE emp ADD date DATE not NULL,ADD work VARCHAR(22) not NULL;

--刪除列
alter TABLE emp DROP WORK; 刪除work 欄位
#刪除多個欄位
alter TABLE emp DROP date,DROP address;

--修改欄位屬性
ALTER TABLE emp MODIFY age SMALLINT NOT NULL; #age欄位類型修改成SMALLINT NOT NULL
#修改到那個欄位後面[first|after 欄位名] 放到第一 after 放到那個欄位後面
ALTER TABLE emp MODIFY age SMALLINT NOT NULL AFTER id; #AFTER id放到ID 後面

--修改列名
alter table tab_name change [column] 列名 新列名 類型 [完整性條件約束條件][first|after 欄位名];
ALTER TABLE emp CHANGE age uuid VARCHAR(20) not null; #age 修改成uuid


--修改表明
RENAME TABLE emp to emppp;

--添加主鍵,刪除主鍵
alter table tab_name add primary key(欄位名稱,...)
alter table users drop primary key;

eg:
mysql> create table test5(num int auto_increment);
ERROR 1075 (42000): Incorrect table definition; there can be only one auto column and it must be defined as a key
create table test(num int primary key auto_increment);
-- 思考,如何刪除主鍵?
alter table test modify id int; -- auto_increment沒了,但這樣寫主鍵依然存在,所以還要加上下面這句
alter table test drop primary key;-- 僅僅用這句也無法直接刪除主鍵

--唯一索引
alter table tab_name add unique [index|key] [索引名稱](欄位名稱,...)

alter table users add unique(name)-- 索引值預設為欄位名show create table users;
alter table users add unique key user_name(name);-- 索引值為user_name

-- 添加聯合索引
alter table users add unique index name_age(name,age);#show create table users;

-- 刪除唯一索引
alter table tab_name drop {index|key} index_name
 
--插入表資料
INSERT into emp(name,salary) VALUES("張三丰",1900.99);
#插入多行
INSERT into emp(name,salary) VALUES("張三丰",1900.99),("張無忌",2900.99);
 
 

 

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.