MySql 数据库、数据表的基本操作

1.数据库的基本操作

使用版本:

 

 进入cmd后首先需要:Mysql -uroot -proot

D:\phpStudy>Mysql -uroot -proot
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.5.53 MySQL Community Server (GPL)

Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>

使用国标库

 set names gbk;

mysql> set names gbk;
Query OK, 0 rows affected (0.00 sec)

mysql>

首先要创建一个数据库:

mysql> create database PiwRin ;
Query OK, 1 row affected (0.03 sec)

查看这个新建的数据库

show create database PiwRin;

 

 删除数据库:

 drop database PiwRin;

 

 查询出MySQL中所有的数据库MySQL命令:

show databases;

 

切换数据库 MySQL命令:

use PiwRin;

查看当前使用的数据库 MySQL命令:

select database();

 

 

2.数据表的基本操作

数据库创建成功后可在该数据库中创建数据表(简称为表)存储数据。请注意:在操作数据表之前应使用“USE 数据库名;”指定操作是在哪个数据库中进行先关操作,否则会抛出“No database selected”错误。

create table 表名(
         字段1 字段类型,
         字段2 字段类型,
         …
         字段n 字段类型
);

2.1 创建数据表

创建学生表 MySQL命令:

 

 2.2 查看数据表

show tables;

 

 查表的基本信息 MySQL命令:

show create table student;

 

 查看表的字段信息 MySQL命令:

desc student;desc student;

 

 

2.3 修改数据表

有时,希望对表中的某些信息进行修改,例如:修改表名、修改字段名、修改字段 数据类型…等等。在MySQL中使用alter table修改数据表.

修改表名 MySQL命令:

alter table student rename to stu;

 

 

 修改字段名 MySQL命令:

alter table stu change name sname varchar(10);

 

 修改字段数据类型 MySQL命令:

alter table stu modify sname int;

 

 增加字段 MySQL命令:

alter table stu add address varchar(50);

 

 

 

 删除字段 MySQL命令:

alter table stu drop address;

 

 2.4 删除数据表

drop table 表名;

 

posted @ 2022-05-09 16:54  しっ  阅读(117)  评论(0)    收藏  举报