程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 數據庫知識 >> MYSQL數據庫 >> MySQL綜合教程 >> mysql學習筆記一

mysql學習筆記一

編輯: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、日期和時間類型
DATE 41000-01-01~9999-12-31
DATATIME 8 1000-01-01 00:00:00~9999-12-31 23:59:59
TIMESTAMP 4 19700101080001~2038年的某個時刻
TIME 3-835:59:59~835:59:59
YEAR 11901~2155
6、字符串類型
varchar(M) M:0-65535
char(M) M:0-255
tinytext 0-255Byte
text 0-65535 2,3萬個漢字
Mediumtext 0-167772150 7,8千萬個漢字
Longtext 0-4294967295 20億個漢字
#少量二進制,圖片,音樂,視頻
binary(M) 0-M
varbinary(M) 0-M
#大量二進制
tinyblob 0-255
blob 0-2^160-64K
mediumblob 0-2^24 0-16M
longblob 0-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

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved