2019.4.1日mysql查询学习笔记

1.为表增加列,更改结构
为表增加家庭地址字段,数据类型为char

alter table stu
add 家庭地址  char(20);

2.增加(add),删除(drop),修改(modify,change,rename)
2019.4.1日mysql查询学习笔记
3.可做算术运算的保存数,它的前后不用加’…’
数的类型有:tinyint,smallint,int,bigint,decimal,float,double
4.使用insert into为表插入数据的时候,省略列名就必须输入所有列对应的值,空值则用Null代替
2019.4.1日mysql查询学习笔记
5.将数据表中的sex="男"的数据单独筛选处理
2019.4.1日mysql查询学习笔记
6.复制A表的数据到B表(设两个表结构一样,B表已经存在):

insert into B表
select * from A表;

7.将stu表中所有女生的记录复制到stu1:

insert  into  stu1
select * from stu
where  sex=' 女';

8.复制旧表的数据到新表(假设两个表的结构不一样):

insert into 新表(字段1,字段2,......)
        select  字段1,字段2,.......from 旧表

9.新建空表stu3,stu3只有学号、姓名和系号字段,然后把stu表学号、姓名和系号列的值复制到stu3表:

create table stu3
(stuid int,
stuname char(3)
depid char(4));

insert into stu3(stuid,stuname,depid)
                 select stuid,stuname,depid from stu;

10.数据更新命令的一般格式:
update 表名
set 列名= 表达式1,列名= 表达式2…
where 条件表达式;
2019.4.1日mysql查询学习笔记
11.将 scores表中20070102学生的各科成绩均增加5分:

update scores
set score = score + 5
where stuid = '20070102';

将没有联系电话的学生的联系电话改为中文的‘未知’:

update stu
set phone='未知'
where phone is null;

// null: 表示一个不确定的值
将A001号的课程名改为大学英语,任课教师改为李刚:

update course
set couname= '大学英语' ,teachername = '李刚'
where couid='A001';

将20070103学生的各科成绩均提高10%:

update scores
set score= score*1.1
where stuid=20070103;