select concat(name,id) as stuinfo from 表名; 查看當前表名中的名字和id
去除左右兩邊的空格 SELECT CONCAT(TRIM(name),id) as info FROM students;
查看表的信息
show create table 表名;
show create table 表名 \G;
修改表的自增起始值
ALTER ATBLE 表名 AUTO_INCREMENT=起始值
ALTER TABLE students AUTO_INCREMENT=180720;
改變當前默認字符集 alter table students charset=gbk;
注意:一個表只能有一個自增 并且一般都會是主鍵為自增
設置步長(起始值) 會話級別的步長
查看原始步長show session variables like 'auto_inc%';
設置步長(自增的偏移量)set session auto_increment_increment=2;會話級別的步長
set session auto_increment_offset=10;會話級別的起始值
設置步長 全局級別的步長
查看原始步長show global variables like 'auto_inc%';
設置步長(自增的偏移量)set global auto_increment_increment=2;全局級別的步長
set global auto_increment_offset=10;全局級別的起始值
給電話號碼設置加密
INSERT INTO students(phone) values(password('12333'));
創(chuàng)建計算字段
創(chuàng)建的計算字段原本并不存在我們的表里面
我們通過mysql 的函數(shù)或者算術運算得到一個結果,我們把這個結果起一個別名
加密函數(shù)
PASSWORD('123455') MD5('344')
創(chuàng)建計算字段
IF(條件,v1,v2):條件滿足為true返回v1否則返回v2 SELECT IF(age>30,age,0) from students;
IFNULL(v1,v2)if v1 not null 返回v1 否則返回v2 SELECT IFNULL(phone,'00000000000') from students;
SELECT name,CASE WHEN phone is null THEN '00000000000' ELSE phone END from students;
三范式
一范式1NF 列不可再分(能滿足需求的情況下拆分)
二范式2NF 必須有主鍵(由一個列或多個列組成主鍵)非主鍵的列必須完全依賴于主鍵 而不是部分依賴
三范式 在第二范式的基礎上 不能存在傳遞依賴 非主鍵的列必須直接依賴于主鍵而不能存在傳遞依賴的關系(關聯(lián))
E-R模型
E:表示實體 其實就是根據(jù)某一個事物的特征 添加描述信息 我們將這些描述信息的添加在一個表里面那么這個表就相當于一個實體
R:relationship關系,實體跟實體之間的關系 表與表之間的關系
一對一:個人信息與身份證
個人信息表
create table user(
id int auto_increment,
name varchar(10) not null,
clsid int,
外鍵 當前表里面的外鍵必須是外面表里面的主鍵
contrasint fk_idcard foreign key( clsid) references ident表名 (id)
);
身份證
create table ident (
id int auto_increment,
id_num varchar(50) not null,
primary key(id)
);
一對多:
create table students(
stu_id int auto_increment,
stu_name varchar(20) not null,
parmary key(stu_id)
);
班級表
create table class(
cla_id int auto_increment,
cla_name varchar(20) not null,
cla_stu_num int default 0,
parmary key(cla_id)
);
多對多
create table students(
stu_id int auto_increment,
stu_name varchar(20) not null,
parmary key(stu_id)
);
課程
create table courses(
cour_id int auto_increment,
cour_name varchar(20) not null,
parmary key(cour_id)
);