第一步,新建库
Create Database 数据库名
第二步,切换数据库
use 数据库名字
第三步,创建表并且添加字段
create table 表名(
-- 新增id字段,字段为主键自增长
id int PRIMARY KEY auto_increment,
stu_id INT ,
sut_name VARCHAR(20) ,
stu_sex VARCHAR(1),
stu_age INT ,
stu_teacher_name VARCHAR(20)
)
或者:
-- 学生表
CREATE TABLE studenttab(
sid int PRIMARY KEY auto_increment,
-- 学生名
sname VARCHAR (20),
-- 性别
gender VARCHAR(2),
-- 学生id
class_id INT
)
第四步,给创建字段添加值:
insert into 表名(字段名,字段名,字段名) VALUES ("值","值",值),("值","值",值),("值","值",值)
INSERT INTO studenttab (sname,gender,class_id) VALUES ("刚蛋","女",1),("铁锤","女",1),("山炮","男",2)
-- Insert into 表名(字段名) values (字段值)
-- 新加学生表值
-- INSERT INTO studenttab (sname,gender,class_id) VALUES ("刚蛋","女",1),("铁锤","女",1),("山炮","男",2)
-- 新加字段
-- ALTER TABLE teachertab add id_card INT NOT NO
-- 新建数据库:
CREATE DATABASE 数据库名
新建表:
CREATE TABLE text2(
-- curriculum 课程 achievement 成绩 name姓名 student——id 学号
id int PRIMARY key auto_increment UNIQUE,
curriculum VARCHAR (20),
name VARCHAR(10),
student_id INT,
achievement INT
)
-- 给字段插入数据
-- INSERT INTO text2 (curriculum,name,student_id,achievement)VALUES("生物","张大大",1,65),("生物","张二货",2,15),("物理","许三多",3,95),("物理","赵四",4,75)
-- 删除字段
-- Alter table 表名 drop 字段名
Alter table text2 drop curriculum
-- 新加字段
-- Alter table 表名 add 字段名 数据类型
Alter table text2 add physics_class VARCHAR(10)
-- 删除数据
-- delete from 表名 where 删除条件
delete from text2 where id=14
-- 查询姓“李”的老师的个数;
SELECT COUNT(*) from text2 WHERE name like "李%%"
-- 查询姓“张”的学生名单;
SELECT * FROM text2 WHERE name LIKE "张%%"
-- 2、查询“生物”课程比“物理”课程成绩高的所有学生的学号;
-- Select * from 表名 where 条件
-- curriculum 课程 biology_achievement 生物成绩 physics_achievement 物理成绩 name 姓名 student_id 学号 physics_class 物理课 biology_class 生物课
CREATE TABLE text2(
id int PRIMARY key auto_increment UNIQUE,
curriculum VARCHAR (20),
name VARCHAR(10),
student_id INT,
achievement INT
)
INSERT into text2 (physics_achievement,biology_achievement,student_id,name)VALUES("100","90",1,"张*"),("80","70",2,"李*"),("40","20",3,"杨*")
Select * from text2 where biology_achievement > physics_achievement
-- 排序查询 order by 升序 asc (mysql中默认的就是asc)降序 desc
Select * from 表名 order by stu_age asc/desc
Select biology_achievement from text2 order by biology_achievement desc
-- 分组查询 group by
-- Select * from 表名 group by sex =‘男’
Select * from text2 group by sex ="女"
-- 计算总值(求和)
-- Select sum(biology_achievement) from 表名
Select sum(biology_achievement) from text2
-- 求平均值
--Select avg(biology_achievement) from 表名
Select avg(biology_achievement) from text2
-- 求最大值
--Select max(biology_achievement) from 表名
Select max(biology_achievement) from text2
-- 求最小值
--Select MIN(biology_achievement) from 表名
Select MIN(biology_achievement) from text2
-- 求总的行数
--Select count(biology_achievement) from 表名
Select count(biology_achievement) from text2