注意:
①不可和其他数据库重名
②名称可以是任何字母、阿拉伯数字、“_”、“$”组层但不能是单独数字
③名称最长为64个字符
④不能用MySQL关键字为库
create database/schema [if not exists] 数据库名称 [[default]character set charset_name][[default]collate collation_name]
mysql > create database if not exists mysqltest;
query OK, 1 row affected(0.01sec)
输入 use 数据库名;使用数据库
mysql > use mysqltest;
Database changed
输入 alter database/schema数据库名称[[default]character set charset_name][[default]collate collation_name] 修改数据库的参数
mysql > alter database mysqltest
-> default character set gb2312
-> collate gb2312_chinese_ci;
` Query OK, 1row affected (0.00sec)
输入show create database 数据库名称; 查看结构信息
mysql> show create database teaching;
+----------+------------------------------------------------------------------------------------------------------------------------------------+
| Database | Create Database |
+----------+------------------------------------------------------------------------------------------------------------------------------------+
| teaching | CREATE DATABASE `teaching` /*!40100 DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci */ /*!80016 DEFAULT ENCRYPTION='N' */ |
+----------+------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
删除已经存在的数据库和所以数据
输入drop database [if exists] 数据库名;
mysql> drop database mysqltest;
create 关键字 [if not exists] 表名称 [(column_definition),…]等字段定义;
mysql> create table if not exists student
(......);
进入选中的表格中输入show tables;查看表格
mysql> show tables;
+--------------------+
| Tables_in_teaching |
+--------------------+
| course |
| score |
| student |
| teach_course |
| teacher |
+--------------------+
5 rows in set (0.01 sec)
输入describe 表名称;查看表格的基本定义(字段名类型主键默认值等)
mysql> describe student;
+-----------+-------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------+-------------------+------+-----+---------+-------+
| studentno | char(11) | NO | PRI | NULL | |
| sname | char(8) | NO | | NULL | |
| sex | enum('男','女') | YES | | 男 | |
| birthdate | int | YES | | NULL | |
| entrance | int | YES | | NULL | |
| phone | varchar(12) | NO | | NULL | |
| Email | varchar(20) | NO | | NULL | |
+-----------+-------------------+------+-----+---------+-------+
7 rows in set (0.00 sec)
输入show create table;查看表格的结构
mysql> show create table student;
+---------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+---------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| student | CREATE TABLE `student` (
`studentno` char(11) NOT NULL COMMENT '学号',
`sname` char(8) NOT NULL COMMENT '姓名',
`sex` enum('男','女') DEFAULT '男' COMMENT '性别',
`birthdate` int DEFAULT NULL COMMENT '出生日期',
`entrance` int DEFAULT NULL COMMENT '入学成绩',
`phone` varchar(12) NOT NULL COMMENT '电话',
`Email` varchar(20) NOT NULL COMMENT '电子信箱',
PRIMARY KEY (`studentno`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci |
+---------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
①添加字段
输入alter table 数据表名称
->add 项目名 字符串 被添加的项目
②修改表名
输入alter table 表原名 rename to 新名字
③修改字段数据类型
输入alter table 表名->modify type 定义 default 新定义
④删除字段
输入alter table 表名 drop 字段
删除已存在的表。会删除表中的所以数据
输入drop table 表名;
mysql> use demo1;
Database changed
mysql> Create table example(
-> today datetime,
-> name char(20)
-> );
Query OK, 0 rows affected (1.64 sec)
mysql> desc example;
+-------+----------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+----------+------+-----+---------+-------+
| today | datetime | YES | | NULL | |
| name | char(20) | YES | | NULL | |
+-------+----------+------+-----+---------+-------+
2 rows in set (0.00 sec)
mysql> drop table example;
Query OK, 0 rows affected (0.66 sec)
①用inser/replace插入多行数据
insert/replace into 表名称 values->(…,…);
②用load data 将文件中的输入导入
load data local infile “文件地址” into table 表名称;
③用set子句直接赋值插入数据
insert into 表名称 ->set 列名称 =‘’’;
用update 。。set、、、修改一个表中的数据
update 表名称 set 修改的列名称=数据 ->where
delete from 表名称 where 项目名和范围;
①select …from…
输入 select * from 表名称;
mysql> use teaching;
Database changed
mysql> select * from course;
Empty set (0.00 sec)
②select 要查询的项目
select 项目名,项目名等;
信息加载中,请等待
微信客服(速回)
微信客服(慢回)