常见的MySQL语句命令:
进入mysql 命令行:mysql -uroot -p;
查看所有数据库:show databases;
创建数据库:create database hlwk charset utf8;
删除数据库:drop database hlwk;
选择数据库:use databases;
查看所有表:show tables;
查看创建数据库的语句:show create database hlwk;
查看创建表的语句:show create table hlwk;
查看表结构:desc tablename;
MySQL常见的数据类型表达形式:int 、varchar()、date、decimal()
1. 使用终端连接MySQL服务器:
mysql -u root -p
2. 增删改查操作:
创建数据库:
create database hlwk;
删除数据库hlwk:
drop database hlwk;
使用数据库:
use dvwa;
3. 对表的操作:
创建一个数据库表,表名是:employee,该表中包含id、name、sex、birthday、job字段;
- create database hlwk;
- use hlwk;
- create table employee(
-
- id int,
- name varchar(20),
- sex char(4),
- birthday date,
- job varchar(100),
-
- );
在表中增加一个字段image,并查看employee这张表的整个描述:
- alter table employee add image blob;
- describe employee;
修改job的值,使得其长度位60:
alter table employee modefy job varchar(60);
删除sex字段:
alter table employee drop sex;
将表名employee更改为user:
rename table employee to user;
修改表中的字符编码位UTF-8:
alter table employee character set utf8;
查询字符集:
show full columns from employee;
将字段name修改位username:
alter table employee change column name username varchar(60);
删除表:
drop table emplyee;
4. 增删改查实例:
- #创建表
- create table employee(
-
- id int,
- name varchar(40),
- sex varchar(4),
- birthday date,
- enrty_date date,
- salary decimal(8,2),
- resume text,
-
- )
-
- #插入数据
- insert into employee(id,name,sex,birthday,entry_date,salary resume) values(1,'zhangsan','male','1990-03-04','2000-09-10','10000','I am a teacher');
-
- #修改数据类型
- alter table employee change sex sex varchar(4) character set utf8 not null;
- alter table employee change name name varchar(20) character set utf8 not null;
-
-
- #指定某些列插入数据
- insert into employee(id) values(6);
- #修改表中的数据:将所有的员工的薪水改为5000¥
- update employee set salary=5000;
-
-
- #将姓名为‘zs’的员工的薪水修改为3000¥
- update employee set salary=3000 where name='zs';
-
- #将id=6的员工的薪水修改为4000¥,name修改为haha
- update empolyee set name='haha',salary=4000 where id=6;
-
- #将id=1的薪水在原有的基础上再增加1000¥
- update employee set salary=salary+1000 where id=1;
-
- #删除表中的某一条数据
- delete from employee where id=1
- #删除表中的所有记录
- delete from employee;
- #使用truncate删除表中的记录
- truncate table employee;
-
-
- #查询
- select * from hlwk.employee;
- 指定1-4条数据
- select * from hlwk.employee limit 1,3;
- 指定id,name,sex
- select id,name,sex from hlwk.employee;
- 指定id位于(1,3,5,7,9)中的数据
- select * from hlwk.emlpoyee where id IN (1,3,5,7,9);
- 查询id再某一个区间的数据
- select * from hlwk.employee where id between 1 and 10;
-
-
-
后续更新。