数据库_基本操作
1.登录和退出数据库
(1)命令:mysql -uroot -p
(2)说明:-u后面是登录的用户名,-p后面是登录密码
(3)显示当前的时间:select now();
(4)退出数据库:quit/exit/ctrl+d
2.数据库操作的SQL语句
(1)显示所有的数据库:show databases;
(2)创建数据库:create database python charset=utf8;
(3)使用数据库:use python;
(4)查看当前使用的数据库:select database();
(5)删除数据库:drop database python;
3.表结构操作的SQL语句
(1)查看当前数据库中所有表:show tables;
(2)创建表:create table students(id int unsigned primary key auto_increment not null,name varchar(10) not null,age tinyint default 0,sex enum(“男”,“女”) default “男”);
(3)修改表–添加字段:alter table students add birthday datetime not null;
(4)修改表–修改字段类型:alter table students modify birthday date null;
(5)修改表–修改字段名和字段类型:alter table students change birthday birth datetime not null;
(6)修改表–删除字段:alter table students drop birth;
(7)查看表结构:desc students;
(8)查看创建表SQL语句:show create table students;
(9)查看创建库SQL语句:show create database python;
(10)删除表:drop table students;
4.表数据操作的SQL语句
(1)查询数据:
- 查询所有列数据:select * from student;
- 查询指定列数据:select name,age from student;
(2)添加数据:
-
全列插入:insert into student values(0,‘lily’,18,‘女’);
-
部分列插入:insert into student(name,age) values(‘lisi’,67);
-
全列多行插入:insert into student values(0,‘liming’,35,default),(0,‘lisa’,23,‘女’);
-
部分列多行插入:insert into student(name,age) values(‘wangwu’,90),(‘liming’,34);
(3)修改数据:
- 修改一列数据:update student set age=20 where id=3;
- 修改多列数据:update student set age=56,sex=‘女’ where id=6;
(4)删除数据:
-
物理删除:delete from student where id=6;
-
逻辑删除:添加一个标识字段