一、使用Window命令行登陆服务器(运行MySQL)
窗口键+R 输入 cmd 命令,按回车键进入DOS窗口。
输入以下命令
cd C:\Program Files\MySQL\MySQL Server 8.0\bin
继续回车
输入 mysql -u root -p 命令,按回车键后输入密码。
出现mysql> ,表示已经成功登录 MySQL服务器。
如未成功,右键此电脑,点击管理,找到“服务和应用程序”,点击“服务”,查找MySQL服务,启动服务即可。
二、示例代码运行
输入 show databases;
查看服务器已有数据库
输入create database stusys;
创建名为“stusys”的数据库
输入use stusys;
选择stusys数据库
输入alter database stusys 回车 default character set gb2312 回车 default collate gb2312_chinese_ci;
输入drop databases stusys;
删除stusys数据库
创建新数据库stusys
输入use stusys; 回车 create table student
创建一个表格
输入
(
sno char(6) not null primary key,
sname char(8) not null,
ssex char(2) not null default'男',
sbirthday date not null,
speciality char(12) null,
tc tinyint null
);
输入use stusys; 回车 输入 create table student1 like student;
输入use stusys; show tables;
查看表
输入show columns from studenr;或desc student;
查看数据库stusys中student表的基本结构。
输入show create table student\G;
查看数据库stusys中student表的详细结构。
输入alter table stusys.student1 回车 add column sid int not null unique auto_increment first;
在数据库stusys的student1 表中增加一列sid,添加表的第一列,不为空,取唯一值并自动增加。
使用DESC语句查看student1表。
DESC stusys.student1;
输入alter table stusys.student1 回车 change column sbirthday sage tinyint default 18;
将数据库stusys的student1表的sbirthday列修改为sage ,将数据类型修改为tinyint,可为空,默认为18。
使用DESC查看表 DESC stusys.student1;
输入alter table stusys.student1 回车 drop column sid;
在alter table stusys 语句中,可通过drop column子句完成删除列的功能。
输入alter table stusys.student1 回车 rename to stusys.student2;
在数据库stusys中,将 student1 表重命名为 student2 表。
输入rename table stusys.student2 to stusys.student3;
输入drop table stusys.student3;
输入show engines;
查看存储引擎