码迷,mamicode.com
首页 > 数据库 > 详细

MySQL数据库基本操作及案例

时间:2018-08-08 13:32:46      阅读:154      评论:0      收藏:0      [点我收藏+]

标签:范围   分类   sql   成绩表   mon   建表   字段名   auto   cached   

一、数据库

1.1数据库:持久化报存数据的软件系统,通过该系统可以创建数据库服务器

二、数据库分类

1.2(常规)数据库分类:关系型数据库、层次式数据库、网络式数据库

1.3流行的分类:关系型数据库和非关系型数据库

 

                                                关系型数据库:表格及表格之间的关系来描述存储数据

                                                非关系型数据库:

  1. 键值存储数据库Redis、Memcached
  2. 列存储数据库Cassandra
  3. 面向文档MongoDB

三、数据库

/*

                                                数据库的创建hzdldb

                                                MYSQL数据库不区分大小写

                                               

*/

-- CREATE DATABASE hzdldb;

 

-- 建表

/*

                                                3要素:表明、表字段名、每个字段的数据类型

*/

/*

                                                CREATE TABLE users(

                                                        id INTEGER not null,

                                                        uname VARCHAR(20) DEFAULT ‘张三‘ NOT null

                                                );

*/

-- 判断是否存在users,不存在就创建

-- 1.CREATE TABLE IF not EXISTS users(

-- 2.先删除后创建

DROP TABLE IF EXISTS users;

                                                CREATE TABLE users(

                                                id INTEGER not null,

                                                uname VARCHAR(20) DEFAULT ‘张三‘ NOT null,

                                                usex VARCHAR(6) DEFAULT ‘男‘ not null,

                                                uage INTEGER DEFAULT 18 not null,

                                                join_date datetime DEFAULT NOW()

                                               

);

-- 获取当前时间

-- SELECT NOW();

 

 

-- 添加语句(插入一条语句)

insert into users

VALUES(1,‘杰克‘,‘男‘,25,NOW());

insert into users

VALUES(2,‘龙杰克‘,‘男‘,35,NOW());

SELECT *from users

 

/*

                                                创建约束范围--constraint

                                                作用:限制表里(时间)的字段取值规划和范围

*/

drop TABLE IF EXISTS persons;

CREATE TABLE persons(

 id int not null, -- not null非空约束

 per_name VARCHAR(30) DEFAULT ‘某人‘ not null,

--  per_sex char(6) DEFAULT ‘男‘ not null,

 per_sex enum(‘男‘,‘女‘) DEFAULT ‘男‘ not null,

-- constraint chk_ss CHECK(per_sex=‘男‘ OR per_sex=‘女‘)-- INNODB默认mysql中不支持check约束,用enum解决

 UNIQUE(id),-- 唯一约束

 UNIQUE(per_name)-- 唯一约束

);

 

INSERT into persons VALUES(1000,DEFAULT,‘露‘);  -- 无法插入per_sex受enum约束

INSERT into persons VALUES(1001,‘小妹‘,‘女‘);

INSERT into persons VALUES(1003,‘小哥‘,‘男‘);

INSERT into persons VALUES(1002,DEFAULT,‘迷人‘);

 

--  查询persons全表

SELECT *from persons

 

/*

                                                为person表主键

*/

DROP TABLE IF EXISTS person;

CREATE TABLE person(

                                                id INTEGER PRIMARY key,

                                                uname VARCHAR(6) DEFAULT ‘大人‘ not null,

                                                uage INTEGER DEFAULT 18 not null

);

INSERT into person VALUES(3,DEFAULT,16);

SELECT * FROM person;

 

-- 案例:学生表和学生成绩表

drop TABLE IF exists students;

CREATE TABLE students(

-- auto_increment自动增长

                                                id INTEGER primary key auto_increment,

                                                stu_name varchar(20) not null,

                                                stu_pro VARCHAR(20) not null

 

);

 

-- 成绩表

drop TABLE IF exists scores;

CREATE TABLE scores(

-- auto_increment自动增长

                                                id INTEGER primary key auto_increment,

                                                score INTEGER DEFAULT 0 not null,

--  创建stu_id外键参考student主键       

                                                stu_id INTEGER not null,

                                                CONSTRAINT FK_stuid FOREIGN KEY(stu_id) REFERENCES students(id)

);

 

INSERT into students (stu_name,stu_pro)

VALUES(‘李四‘,‘英语‘);

 

insert into scores (score,stu_id)

VALUES(80,4);

 

SELECT * FROM students;

SELECT * FROM scores;

 

/**

                                                新建数据库hzdlshop

*/

 

CREATE DATABASE hzdlshop;

 

/*

                                                增删改查

*/

 

DROP TABLE IF EXISTS users;

                                                CREATE TABLE users(

                                                id INTEGER PRIMARY KEY auto_increment,

                                                uname VARCHAR(20) DEFAULT ‘张三‘ NOT null,

                                                upass char(6) not null,

                                                usex VARCHAR(6) DEFAULT ‘男‘ not null,

                                                uage INTEGER DEFAULT 18 not null,

                                                ubirth datetime DEFAULT NOW() not null,

                                                score INTEGER DEFAULT 100 not null

);

 

-- 创建商品表

drop TABLE if exists type;

CREATE TABLE type(

                                                id INTEGER PRIMARY KEY auto_increment,

                                                name VARCHAR(30) not null

);

 

-- 商品表

DROP TABLE IF EXISTS products;

CREATE TABLE products(

                                                pno INTEGER PRIMARY KEY auto_increment,

                                                pname VARCHAR(30) DEFAULT ‘糖果‘ not null,

                                                price DECIMAL(11,2) DEFAULT 0.00 not null,

                                                type_id INTEGER not null -- 参考商品类表文件

                                               

);

 

 

/**

                                                新建数据库hzdlshop

*/

 

-- CREATE DATABASE hzdlshop;

 

/*

                                                增删改查

*/

 

DROP TABLE IF EXISTS users;

                                                CREATE TABLE users(

                                                id INTEGER PRIMARY KEY auto_increment,

                                                uname VARCHAR(20) DEFAULT ‘张三‘ NOT null,

                                                upass char(6) not null,

                                                usex VARCHAR(6) DEFAULT ‘男‘ not null,

                                                uage INTEGER DEFAULT 18 not null,

                                                ubirth datetime DEFAULT NOW() not null,

                                                score INTEGER DEFAULT 100 not null

);

 

-- 创建商品表

drop TABLE if exists type;

CREATE TABLE type(

                                                id INTEGER PRIMARY KEY auto_increment,

                                                name VARCHAR(30) not null

);

 

-- 商品表

DROP TABLE IF EXISTS products;

CREATE TABLE products(

                                                pno INTEGER PRIMARY KEY auto_increment,

                                                pname VARCHAR(30) DEFAULT ‘糖果‘ not null,

                                                price DECIMAL(11,2) DEFAULT 0.00 not null,

                                                type_id INTEGER not null -- 参考商品类表文件

                                               

);

ALTER TABLE products

add CONSTRAINT FK_product_type FOREIGN KEY(type_id)  references type(id);

 

 

-- 录入测试

INSERT INTO users

VALUES(2,‘mary‘,‘123456‘,‘男‘,19,‘2008-12-19 12:00:12‘,300);

 

 

INSERT INTO users (usex,upass,uname,uage,ubirth,score)

VALUES(‘男‘,‘123456‘,‘王者荣涛‘,19,‘2008-12-19 12:00:12‘,300);

 

INSERT INTO users (usex,upass,uage,ubirth,score)

VALUES(‘男‘,‘123456‘,19,‘2008-12-19 12:00:12‘,300);

 

 

 

-- 录入服装类型

INSERT into type (name)

VALUES(‘食品‘);

INSERT into type (name)

VALUES(‘饮料‘);

INSERT into type (name)

VALUES(‘coffee‘);

INSERT into type (name)

VALUES(‘衣服‘);

INSERT into type (name)

VALUES(‘鞋‘);

 

INSERT into type (name)

VALUES(‘比基尼‘);

 

INSERT into type (name)

VALUES(‘生活用品‘);

 

-- 插入产品

INSERT into products(pname,price,type_id)

VALUES (‘方便米娜‘,300.1,3);

INSERT into products(pname,price,type_id)

VALUES (‘tuoba‘,300.1,3);

INSERT into products(pname,price,type_id)

VALUES (‘垃圾桶‘,300.1,3);

 

-- 删除表

DELETE from users;

SELECT * from users;

DELETE from users WHERE id>=15;

 

 

-- 同配符删除

 DELETE from users;

 

-- 同配符删除,指定删除

DELETE from users

WHERE DATE_FORMAT(ubirth,‘%Y‘)=2008 AND usex=‘男‘;

 

 

-- 查询表

SELECT * from type;

 

 

-- 同配符按日期查询

SELECT DATE_FORMAT(ubirth,‘%Y‘) from users;

-- 同配符按日期指定查询

SELECT DATE_FORMAT(ubirth,‘%Y‘) from users

WHERE DATE_FORMAT(ubirth,‘%Y‘)=2008 AND usex=‘男‘;

 

MySQL数据库基本操作及案例

标签:范围   分类   sql   成绩表   mon   建表   字段名   auto   cached   

原文地址:https://www.cnblogs.com/2070393244com/p/9441858.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!