mysql學習筆記一,mysql學習筆記

來源:互聯網
上載者:User

mysql學習筆記一,mysql學習筆記
資料庫相關操作

1、建立資料庫
create database databasename
成功返回Query OK
失敗返回Error 1007
2、查看所有的資料庫
show databases
注意:是databases,複數
3、選擇資料庫
use databasename
返回是Database changed  知道這個在使用expect時有用
失敗返回Error 1049
4、刪除
drop database databasename
成功返回Query OK
失敗返回Error
資料庫引擎
儲存引擎指定了表的類別,即如何儲存和索引資料,是否支援事務等,同時也決定了表在電腦中的儲存方式
1、查看支援的引擎
show engines
2、查詢預設引擎
show variables like "storage_engine%"
我的事MyISAM
3、修改預設引擎
修改my.ini設定檔
4、選擇引擎
根據需要
資料類型
1、整數類型,1,2,3,4,8,單位Byte
2、浮點類型,4,8
3、定點數類型:
DEC(M,D)和DECIMAL(M,D) M+2Byte
4、位類型
BIT(M)
5、日期和時間類型
DATE41000-01-01~9999-12-31
DATATIME8 1000-01-01 00:00:00~9999-12-31 23:59:59
TIMESTAMP4 19700101080001~2038年的某個時刻
TIME3-835:59:59~835:59:59
YEAR11901~2155
6、字串類型
varchar(M)M:0-65535
char(M)M:0-255
tinytext0-255Byte
text0-65535  2,3萬個漢字
Mediumtext0-167772150  7,8千萬個漢字
Longtext0-4294967295 20億個漢字
#少量二進位,圖片,音樂,視頻
binary(M)0-M
varbinary(M)0-M
#大量二進位
tinyblob0-255
blob0-2^160-64K
mediumblob0-2^24 0-16M
longblob0-2^32 0-4G
表操作

格式:
 命令(create,desc,alter,drop) table tablename [動作]
1、建立
create table tablename(
columnname,type
...
)
create table t_table(id int,money float,date date,time time,desc varchar(500),picture mediumblob) ;
添加失敗,由於關鍵字date,time,desc
create table t_table(id int,money float,tdate date,ttime time,tdesc varchar(500),picture mediumblob) ;
或者使用:
create table t_table(id int,money float,`date` date,`time`time,`desc` varchar(500),picture mediumblob) ;


2、查看
describe table_name
desc table_name
查看錶的定義
show create table table_name \G
注意:\G顯示的更加人性化,美觀(語句結束符 ;,\g,\G)
3、修改(add modify change rename drop)

格式:alter table tablename [rename/modify/add/change/drop] 

0、修改表名

rename:重新命名

alter table old_table_name rename [to] new_table_name
alter table t_table rename t_test
1、修改列名

a、修改列名和資料類型類型

change:改變,更換

alter table t_table change oldcolumnname newcolumnname newdatatype

b、只修改資料類型

modify:修改

alter table t_table modify columnname datetype
資料類型修改後,資料值可能會改變,但是不會提醒,轉換要謹慎。
2、插入新列在末尾
alter table tablename add columnname datatype
alter table t_test add new1 tinyint;
desc t_test 
3、插入新列在開頭
alter table table_name add columnname datatype first;
alter table t_test add first1 bigint first;
注意:沒有long型,只有bigint
tinyint smallint mediumint int/Integer bigint
4、指定新列的位置
alter table tablename add columnname datatype after column
desc t_test
添加到第三列
alter table  tablename add three bigint after id
desc t_test
5、調整某些列的位置
alter table table_name modify column1 datatype first|alfter column2
屬性名稱1和屬性名稱2都必須存在
alter table t_test modify three bigint after picture;
6、刪除列
alter table tablename drop columnname
desc t_test
alter table tablename drop new1
desc t_test
4、刪除
drop table tablename

相關文章

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.