mysql(一)

mysql(一)

連接數據庫

windwos下
#127.0.0.1是本機地址 也可以進行遠程連接

mysql -h127.0.0.1 -P3306 -uroot -p

mysql常用數據類型

整形

  1. INT (int)
  2. SMALLINT (smallint)
  3. MEDIUMINT (mediumint)
  4. BIGINT (bigint)
type Storage Minumun Value Maximum Value
(Bytes) (Signed/Unsigned) Signed/Unsigned)
tinyint 1 -128 127
smallint 2 -32768 32768
mdeiument 3 -8388608 8388607
int 4 -2147483648 2147483647
bigint 8 -9223372036854775808 9223372036854775807

浮點型

屬性 存儲空間 精度 精確性
Float 4 bytes 單精度 非精確
Double 8 bytes 雙精度 比Float精度高

字符創

1.char
2.varchar

  • CAHR與VARCHAR

CHAR和VARCHAR存儲的單位都是字符
CHAR存儲定長,容易造成空間的浪費
VARCHAR存儲變長,節省存儲空間

  • TEXT與CHAR和VARCHAR的區別

CHAR和VARCHAR存儲單位爲字符
TEXT存儲單位爲字節,總大小爲65535字節,約爲64KB
CHAR數據類型最大爲255字符
VARCHAR數據類型爲變長存儲,可以超過255個字符
TEXT在MySQL內部大多存儲格式爲溢出頁,效率不如CHAR

常用命令

#展示所有數據庫
show databases;
#使用名爲testdao的數據庫
use testdao;
#展示testdao數據庫中的所有表
show tables;
#創建表,表名爲stu;
#寫法不唯一,如果用的表名和字段名是關鍵字,要用``包含
create table stu1(
id int(10) auto_increment primary key comment "id",
name varchar(20) ,
score int(10) 
)engine=innodb default charset=utf8;
# 查看創建好的表
show create table stu1;
#產看錶的結構
desc stu1;
#添加一個age字段
alter table stu1 add column age int(10);
#將score的默認值設爲0;
alter table stu1 alter column score  set default 0;
# 修改一個字段
alter table stu modify column age int(40);
# 刪除一個字段
alter table stu drop column int;
#插入一行數據,可以插入全部的字段,也可以只有某些字段
insert into stu1 value(1,"zxc",90,13);
insert into stu1(name,age) value("zdy",12);
# 刪除表中的數據
delete from stu1 where id=2;
#刪除空字段
delete from stu1 where age is null;
# 更新語句
update stu1 set age=29 where id=1;
#查找數據
select name,age,score from stu1 where id=1;
#刪除表
drop stu1;

( 於2016年6月18日,http://blog.csdn.net/bzd_111

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章