1.在命令行中运行mysql
a.首先安装mysql-installer-community-5.7.14.0.msi;
b.然后找到安装后的文件夹,把bin文件夹添加系统环境的path中;
c.打开命令行,输入mysql -uroot -p;(如果安装mysql的时候没有改变端口,只是设置了密码)
d.输入自己设定的密码,正确无误后,这时就可以执行mysql语句了。
2.mysql创建数据库及表的操作
a.创建数据库语句:create database 库名;
如:create database school;
b.在数据库中创建表:create table 表名(字段定义);
如:use school;//首先要声明在哪个数据库里创建表
create table teacher
(
id int(3) not null primary key,//int类型,最多是三位数,非空且是主键
name char(10),
year date
);
c.显示数据库:show databases;
d.使用数据库:use 库名;(如:use school;)
e.显示数据: show tables;
f.显示表结构:describe 表名;
3.mysql记录的基本操作
a.添加记录:insert into 表名 values(各字段值);
如:insert into teacher values(001,'xiaoming','2016-8-16');//单引号也表示字符串
b.查询记录:select 字段 from 表名 where 条件;
如:select * from teacher;//查询所有字段
select name from teacher where id=001;
c.删除记录:delete from 表名 where 条件;
如:delete from teacher where id=002;
delete from teacher;(全部删除)
d.更新记录:update 表名 set 字段名=值 where 条件;//主键值不能改
如:update teacher set name="xiaohong" where id=002;
4.mysql关于字段的操作
a.往表里添加某个字段:alter table 表名 add 字段名称 字段类型 是否为空;
如:alter table teacher add address varchar(20);
b.修改表中某个字段的类型:alter table 表名 modify 字段名称 字段类型 是否为空;
如:alter table teacher modify year char(20);
c.修改表中某个字段的名称:alter table 表名 change 字段原名称 字段新名称 字段类型 是否为空
如:alter table teacher change year birth date;
d.删除表中某个字段:alter table 表名 drop 字段名称;
如:alter table teacher drop name;
5.有关表及数据库的操作
a.修改表的名称:alter table 原表名 rename 新表名;
如:alter table teacher rename student;
b.删除表:drop table 表名;
如:drop table teacher;
c.删除数据库:drop database 库名;
如:drop database school;
Mysql基础知识——基本操作
最新推荐文章于 2024-05-20 14:38:13 发布