~MySQL命令中 所有字母大小写及单双引号都可看心情输~
关系型数据库和非关系型数据库
MySQL属于关系型数据库管理系统,属于SQL(关系型数据库)
MySQL基本操作
mysql -u root -p //登陆mysql(用户为root)
create database test00; //创建一个名为“test00”的数据库
show test00; //查看有哪些数据库
use test00; //进入/切换数据库(“test00为数据库名“)
create table student(id int,name varchar(20),age int); //创建表格(以后学)
show tables; //查看数据库中有多少表格
desc student; //查看表格内容
drop database test00; //删除数据库
MySQL库/表级操作
insert into student values(1,”baiyjiang”,18);//全字段插入表格行
select * from student; //全字段查询(*为查看所有,也可改为name、age等)
select name from student where age = 19; //有条件的查询(搜索)
select * from student; //全字段查询
insert into student(name) value(“momandai”); //指定字段插入(其它值为空)
insert into student values(3,”jinhefan”,19),(4,”shaohui”,21),(5,”huyongping”,22); //多行插入
update student set id = 1; //修改所有数据(将所有id改为1)
update student set id = 2 , age = 25; //修改多个
update student set id = 5 where name = “baiyjiang”; //有条件的修改(指定修改)(不写where条件会修改全部值)
delete from student where name = “momandai”; //指定删除
MySQL数据类型
数值类型
字符类型
// char如果字符不够长也会占到指定的长度
// Varchar插入一个字节会多占一个字节,如果长的话可能会多2个
enum //枚举
create table st (sex enum(“man”,”woman”,”tj”)); //创建一个指定插入的表格
insert into st value(“man”); //插入“boy”会报错,只能插入以上指定的man、woman、tj
时间日期类型
时间日期类型使用不多
作业
- 建一张学生表 包含(id,name,age,sex)
- 增加四条数据
- 查询表中sex为男的数据
- 删除id =3的数据,
- 将sex为女的,修改为男
create table students(id int,name varchar(50),age int,sex enum("man","woman"));
desc students;
insert into students values(1,"wangsicong",16,"man"),(2,"zhangzhuxin",16,"woman"),(3,"luyutong",14,"woman"),(4,"zhangboxiang",16,"man");
select * from students;
delete from students where id = 3;
select * from students;
update students set sex = "man" where sex = "woman";
select * from students2;
- 建一张学生表 包含(id,name,age,sex, class)
- 增加四条数据
- 查询表中sex为”M”的数据
- 删除id =3的数据,
- 将sex为”F”的,修改为”M”
create table students(id int,name varchar(50),age int,sex enum("M","W","F"),class int);
desc students;
insert into students values(1,"wangsicong",16,"M",7),(2,"zhangzhuxin",16,"W",4),(3,"luyutong",14,"W",2),(4,"zhangboxiang",16,"F",3);
select * from students;
select * from students where sex = "M";
delete from students where id = 3;
select * from students;
update students set sex = "M" where sex = "F";
select * from students;