版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
1、连接mysql
格式:mysql -h主机地址 -u用户名 -p用户密码
2、连接到远程主机上的MYSQL。
??假设远程主机的IP为:110.110.110.110,用户名为root,密码为root。则键入以下命令:
mysql -h110.110.110.110 -uroot -proot; # (注:p与root之间可以不用加空格)。
一、创建数据库
1.创建一个名为student的数据库
create database student;
2.显示数据库
show databases;
3.删除数据库
drop database student;
4.连接数据库
use student;
5.修改数据库编码方式
alter database student default character set gbk collate gbk_bin;
二、创建数据表 hello
create table hello(
ID int(11),
name varchar(20),
grade float
);
1.查看数据表状态
(1)show tables;
(2)show create table hello;
2.查看数据表
(1)describe hello;
describe 可以简写为desc
(2)desc hello;
3.修改数据表
1)修改表名:
alter table hello rename to grade;
2)修改字段名:
alter table hello change name usename varchar(20);
3)修改字段的数据类型:
alter table hello modify id int(20);
4)添加字段:
alter table hello add age int(10);
5)删除字段:
alter table hello drop age;
6)修改字段排列位置:
将数据表hello的username字段修改为表的第一个字段
alter table hello modify username varchar(20) first;
将数据表hello的ID字段插入到grade字段的后面
alter table hello modify ID int(20) after grade;
4.删除数据表
drop table hello;
三、表的约束
主键约束:primary key
非空约束:not null
唯一约束:unique
默认约束:default 后面跟默认值
设置表的字段值的自动增加: 字段名 数据类型 auto_increment
四、增删改查
1)增添数据
扫描二维码关注公众号,回复:
7594910 查看本文章
//create database chapter01;
//use chapter01;
//create table student (
id int(4),
name varchar(20) not null,
grade float );
insert into student (id, name, grade)
values(0,'zhangsan',98.5);
同时添加多条语句
insert into student values
(1,'lilei',99),
(2,'libai',98),
(3,'zhangfei',97);
2)更改数据
update student
set name=' caocao',grade=50
where id=1;
3)删除数据
delete from student;删除所有数据
delete from student
where ID=11;删除ID=11的数据;
五、查询表格,单表查询
select * from student;查询所有字段
select ID, name,grade from student;查询ID ,name,grade字段
按条件查询
select * from student
where name=‘张飞’;
select * from student where ID int(1,2,3)
select * from student where ID between 2 and 5;