**
韩伟伟数据库增删改查:
**
-
创建数据库:
create database databasename default charset utf8;
创建一个名字为name的数据库,支持所有语言。支持图片格式是将utf8改成utf8mb4即可。 -
删除数据库:
drop database databasename;
不要轻易删除数据库 -
使用数据库:
use databasename; -
创建数据库的表:
create table tablename(
列名1 属性,
列名 2 属性,
主键:primary key (主键名),
外键:foreign key(外键) references 外部表名 (外部表主键;即是外键),
);
唯一性约束:
alter table tb_student add constraint uni_score_stuid_couid unique(stuid,couid); -
删除数据表:
drop table tablename; -
修改数据表:
(1).添加列 alter table tablename add column columnname 属性;
(2).修改列 alter table tablename change column columnname 属性;
(3).删除列 alter table tablename drop column columnname 属性; -
插入数据:
插入所有数据:insert into tablename values (内容1,内容2);
插入部分数据:insert into tablename (列1,列2)values (内容1,内容2);
插入部分数据时可以不按照列的顺序书写,但是后面的内容一定要与前面的列名相对应。 -
删除数据:
delete from tablename where 列名=具体的号码;
例子:
delete from tb_student where stuid= “1004”;
删除tb_student中学号为1004的用户。 -
更新数据表:
update tablename set 列名=属性;
例子1:
update tb_student set stuaddr=“四川成都” where stuid=1005 and stuid=1006;
更新tb_student表中学号为1005和学号为1006的学生住址为四川成都。
例子2:
update tb_student set stuaddr=“四川成都” where stuid in(1005,1006);
更新tb_student表中学号为1005和学号为1006的学生住址为四川成都。
改俩项时:update tb_student set stuaddr =“陕西榆林”, stuname= “韩伟伟” where stuid=1005; -
查询select:
select * from tablename;
"*"代表所有的信息;
( 投影和别名)
select couname,coucredit from tb_course;
输出:
couname coucredit
python开发 3
select couname as 课程名称,coucredit as 学分 from tb_course;
输出:
课程名称 学分
python开发 3
alias ll =“ls - l”(别名)
(筛选where)
select stuname as 姓名,case stusex when 1 then ‘男’ else ‘女’ end as 性别 from tb_student;
筛选tb_student 中(stuname,stusex)姓名和性别这俩列,并将stusex中为1的显示为男,为0的显示为女。