-- 创建数据库
/*
数据库名,表名大小写 取决于当前操作系统,推荐使用下划线 例如php_one
标示符不以数字开头, 但可以加上 `1234` 例如这样的就可以
// 中文数据库名也可,但要设置编码,
set names gbk;
创建数据库后 会产生db.opt 文件保存数据库选项信息,
如果是特殊字符,使用编码形式保存
*/
create database db_name;
-- 查看数据库名
show databases;
-- 查看数据库属性, 创建语句
show create database db_name;
-- 数据库删除
drop database db_name;
-- 修改数据库信息(属性)
alter database db_name character set gbk;
-- 修改名字,1 可以直接修改文件夹的名字;不通用,2,新建,导入数据库文件
-------------------------------------------------------------------------
-- 强制指明数据库
use database_name
-- 创建表,可以通过 "." 语法指明数据库
/*
特殊表名用 ` ` 包裹
*/
create table php_one.php_class(
class_no varchar(50),
date_start date
);
-- 显示所有的表
show tables;
+-------------------+
| Tables_in_php_one |
+-------------------+
| exam_question |
| exam_student |
| info_student |
| php_class |
+-------------------+
/*
表名前缀,为了区分相同逻辑表名不同应用
% 通配符 ,
like 也使用在show databases
show tables like 'exam_%'
*/
-- 学生管理系统
create table info_student(
name varchar(20),
stu_no varchar(50)
);
-- 在线考试系统
create table exam_student(
name varchar(20),
stu_no varchar(50),
score int
);
create table exam_question(
name varchar(20),
stu_no varchar(50),
score int
);
show tables like 'exam_%'
-- 某个表的创建信息
show create table exam_student;
-- 用\G 来代替 ; 简易图形化界面
show create table exam_student\G
+-------------------------------------------+
|table: exam_student |
|Create Table: CREATE TABLE `exam_student` (|
| `name` varchar(20) DEFAULT NULL, |
| `stu_no` varchar(50) DEFAULT NULL, |
| `score` int(11) DEFAULT NULL |
|) ENGINE=InnoDB DEFAULT CHARSET=gbk |
|1 row in set (0.00 sec) |
+-------------------------------------------+
-- 查看表结构
desc exam_student;
+--------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------+-------------+------+-----+---------+-------+
| name | varchar(20) | YES | | NULL | |
| stu_no | varchar(50) | YES | | NULL | |
| score | int(11) | YES | | NULL | |
+--------+-------------+------+-----+---------+-------+
-- 删除表
drop table table_name;
drop table like 's%';
drop table if exists table_name;
-- 慎用
Select CONCAT( 'drop table ', table_name, ';' ) FROM information_schema.tables Where table_name LIKE 's_%';
-- 修改表 --
--------------------------------------------------------------------------------
-- 修改表名:rename 支持跨数据库重命名,支持多个表同时操作用","隔开
rename table php_class to newName
-- 跨数据库 相当于剪切,可以实现数据库重命名
rename table php_class to dbname.tableName;
-- 修改列定义;
alter table table_name [add|drop|change|modify]
-- 1、增加一个列
add
alter table exam_student add heig int;
-- 2、删除一个列
drop
alter table exam_student drop heig;
-- 3、重命名一个列,连带新列定义
change
alter table exam_student change height height int;
-- 4、修改一个列定义
modify
alter table exam_student modify height varchar(4);
-- 修改表选项:
alter table exam_student character set utf8;
--------------------------------------------------------------------------------
----------------------------------对数据的操作----------------------------------
-- 创建数据
insert into table_name(?,?,?,?) values(?,?,?,?);
insert into exam_student(name, stu_no,score,height) values('刘瑞', '1121',100, 180);
-- 修改数据
update table_name set 字段名=字段值 where 条件;
update exam_student set name='刘瑞斌' where stu_no='1121';
update exam_student set stu_no='1121010733' where name='刘瑞斌';
-- 获得数据
select [字段列表| *] from table_name where 查询条件;
+------+--------+-------+--------+
| name | stu_no | score | height |
+------+--------+-------+--------+
| 刘瑞 | 1121 | 100 | 180 |
+------+--------+-------+--------+
select name from exam_student where score > 90;
+------+
| name |
+------+
| 刘瑞 |
+------+
-- 删除数据
delete from table_name where 条件;
delete from exam_student where name like '%次%';
mysql 学习笔记 day01
最新推荐文章于 2024-09-14 15:21:37 发布