python day34 數(shù)據(jù)庫一 創(chuàng)建表增加表create table 表名(
字段名 列表型[可選的參數(shù)], #必須加逗號
字段名 列表型[可選的參數(shù)] #最后一行不加逗號
)charset = utf8 #后面
增加數(shù)據(jù)#語法
insert into 表名(列1,列2) values (值1,值2);
#例子
insert into t1 (id,name) values (1,'momo');
查詢數(shù)據(jù)#語法
select 列1,列2 from 表名; (*表示查詢所有的列)
show tables;
#列子
select id,name from t1;
修改數(shù)據(jù)#1.修改表名 語法
alter table 舊表名 rename 新表名;
alter table t1 rename t11;
#2.增加字段 語法
#添加的列永遠是添加在最后一列之后
alter table 表名
add 字段名 列表型[可選的參數(shù)];
alter table t11 add name varchar(32) not null defaut '';
#添加的列永遠是添加在第一列
alter table 表名
add 字段名 列表型[可選的參數(shù)] first;
alter table t11 add name varchar(32) not null defaut '' first;
#添加的列永遠是添加在....列之后
alter table 表名 add 字段名 列表型[可選的參數(shù)] after 字段名;
alter table t11 add name varchar(32) not null defaut '' after age;
#3.刪除字段 語法
alter table 表名 drop 字段名 ;
alter table t11 drop name;
#4.修改字段 語法
#修改字段數(shù)據(jù)類型
alter table 表名 modify 字段名 數(shù)據(jù)類型[可選的參數(shù)];
alter table t11 modify name char(30);
#修改字段名和數(shù)據(jù)類型
alter table 表名 change 字段名 字段名 數(shù)據(jù)類型[可選的參數(shù)];
alter table t11 change name name2 char(30) not null default '';
刪除數(shù)據(jù)#刪除數(shù)據(jù) 語法
drop table 表名;
drop table t1;
復制表結(jié)構create table t11 like t111;
二 查看表結(jié)構#方法一
describe 表名;
#方法二
desc 表名;
#方法三 查看創(chuàng)建表的SQL語句
show create table 表名
三 MySQL支持的數(shù)據(jù)類型整型| tinyint | 1字節(jié) | (-128,127) | (0,255) | 小整數(shù)值 | | smallint | 2字節(jié) | (-32 768,32 767) | (0,65 535) | 大整數(shù)值 | | mediumint | 3 字節(jié) | (-8 388 608,8 388 607) | (0,16 777 215) | 大整數(shù)值 | | int或integer | 4 字節(jié) | (-2 147 483 648,2 147 483 647) | (0,4 294 967 295) | 大整數(shù)值 | | bigint | 4 字節(jié) | (-9 233 372 036 854 775 808,9 223 372 036 854 775 807) | (0,18 446 744 073 709 551 615) | 大整數(shù)值 | | float | 4 字節(jié)float(255,30) | (-3.402 823 466 E 38,-1.175 494 351 E-38),0,(1.175 494 351 E-38,3.402 823 466 351 E 38) | 0,(1.175 494 351 E-38,3.402 823 466 E 38) | 單精度 浮點數(shù)值 | | float | 8 字節(jié)double(255,30) | (-1.797 693 134 862 315 7 E 308,-2.225 073 858 507 201 4 E-308),0,(2.225 073 858 507 201 4 E-308,1.797 693 134 862 315 7 E 308) | 0,(2.225 073 858 507 201 4 E-308,1.797 693 134 862 315 7 E 308) | 雙精度 浮點數(shù)值 | | decimal | 對DECIMAL(M,D) ,如果M>D,為M 2否則為D 2 double(65,30) | 依賴于M和D的值 | 依賴于M和D的值 | 小數(shù)值 |
#浮點型
create table t1(
id int auto_increment primary key,
salary decimal(16,10),
num float
)charset=utf8;
#float : 精確到小數(shù)點兩位
#decimal : 可以控制精確的小數(shù)點位 decimal(m,n) m是數(shù)字的總個數(shù)(負號不算),n是小數(shù)點后個數(shù)
字符串| CHAR | 0-255字節(jié) | 定長字符串 | | VARCHAR | 0-65535 字節(jié) | 變長字符串 | | TINYBLOB | 0-255字節(jié) | 不超過 255 個字符的二進制字符串 | | TINYTEXT | 0-255字節(jié) | 短文本字符串 | | BLOB | 0-65 535字節(jié) | 二進制形式的長文本數(shù)據(jù) | | TEXT | 0-65 535字節(jié) | 長文本數(shù)據(jù) | | MEDIUMBLOB | 0-16 777 215字節(jié) | 二進制形式的中等長度文本數(shù)據(jù) | | MEDIUMTEXT | 0-16 777 215字節(jié) | 中等長度文本數(shù)據(jù) | | LONGBLOB | 0-4 294 967 295字節(jié) | 二進制形式的極大文本數(shù)據(jù) | | LONGTEXT | 0-4 294 967 295字節(jié) | 極大文本數(shù)據(jù) |
#char(長度): 定長
create table t1(
id int unsigned auto_increment primary key,
name char(10) not null default 'momo'
)charset=utf8;
#varchar(長度): 變長
create table t2(
id int auto_increment primary key,
name varchar(10) not null default 'momo'
)charset=utf8;
'''區(qū)別:
char: 定長, 無論插入的字符是多少個,永遠固定占規(guī)定的長度
場景:
1. 身份證
2. 手機號 char(11)
3. md5加密之后的值,比如密碼 等 char(32)
varchar: 變長, 根據(jù)插入的字符串的長度來計算所占的字節(jié)數(shù),但是有一個字節(jié)是用來保存字符串的大小的
注意:如果,不能確定插入的數(shù)據(jù)的大小,一般建議使用 varchar(255)
'''
日期時間類型| DATE | 3 | 1000-01-01/9999-12-31 | YYYY-MM-DD | 年月日 | | TIME | 3 | '-838:59:59'/'838:59:59' | HH:MM:SS | 時分秒 | | YEAR | 1 | 1901/2155 | YYYY | 年份值 | | DATETIME | 8 | 1000-01-01 00:00:00/9999-12-31 23:59:59 | YYYY-MM-DD\HH:MM:SS | 年月日時分秒 | | TIMESTAMP | 4 | 1970-01-01 00:00:00/2038 結(jié)束時間是第 2147483647 秒,北京時間 2038-1-19 11:14:07,格林尼治時間 2038年1月19日 凌晨 03:14:07 | YYYYMMDD\HHMMSS | 混合日期和時間值,時間戳 |
create table t1(
d date,
t time,
dt datetime
);
四 表的完整型約束auto_increment : 自增 primary key : 主鍵索引,可以加快查詢速度,列的值不能重復 not null : 標識該字段不能為空 default : 為該字段設置默認值 #列子1
create table t1(
id int,
name char(5)
)charset = utf8;
#例子2
create tabel t2(
id int auto_increment primary key,
name char(10)
)charset = utf8
#例子3
create table t3(
id int unsigned auto_increment primary key,
name char(10) not null defualt 'hello',
age int not null default 0
)charset = utf8
五 枚舉ENUM 中文名稱叫枚舉類型,它的值范圍需要在創(chuàng)建表時通過枚舉方式顯示。ENUM只允許從值集合中選取單個值,而不能一次取多個值。 create table t1 (
id int auto_increment primary key,
gender enum('male','female')
)charset utf8;
mysql> insert into t9 (gender) values ('male');#不是male或者female就會報錯
六 操作表數(shù)據(jù)行增#語法
insert into 表名 (列1, 列2) values (值1,'值2');
#例子
insert into t1 (id, name) values (1, 'simple');
刪#語法
delete from 表名 where 條件;
#例子
mysql> delete from t1 where id=1;
delete from 表名; #刪除表中所有的數(shù)據(jù)
truncate 表名; #沒有where條件的
區(qū)別:
1. delete之后,插入數(shù)據(jù)從上一次主鍵自增加1開始, truncate則是從1開始
2. delete刪除, 是一行一行的刪除, truncate:全選刪除 truncate刪除的速度是高于delete的
改#語法
update 表名 set 列名1=新值1,列名2=新值2 where 條件;
mysql> update t11 set name='momo' where id=1;
查#語法
select 列1, 列2 from 表名; (*代表查詢所有的列)
select * from t66 where id=1;
#between..and...: 取值范圍是閉區(qū)間
select * from t11 where id between 1 and 3;
#避免重復DISTINCT
mysql> select distinct name from t11;
#通過四則運算查詢
mysql> select name, age*10 as age from t1;
# in 用法
mysql> select * from t11 where id in (1,3,5);
#like : 模糊查詢
mysql> select * from t11 where name like 'm%';
mysql> select * from t11 where name like '%o';
mysql> select * from t11 where name like '%mo%'; 來源:https://www./content-2-533951.html
|