一、创建数据库
创建之前先判断数据库存在不存在,如果存在的话先删除在创建,
以创建school数据库为例:
语法:
drop database if exists school;
create database school;
二、创建表
创建之前先判断表存在不存在,如果存在的话先删除在创建,
以创建books表为例:
语法:
drop table if exists books;
create table books(
bno int(4) not null primary key auto_increment,#不为空 主键 自增
bname varchar(20) not null unique,#不为空 唯一
author vatchar(20) not null,
)auto_increment=101;#可指定自动增长开始值
借阅表
drop TABLE if EXISTS info;
CREATE TABLE info(
id int(4) PRIMARY key auto_increment,
bno int(4) REFERENCES books(bno),#外键约束
rdate datetime
);
三、插入数据
INSERT into books(bno,bname,author,price,quanitity)values(1001,"红楼梦","施耐庵",15.6,100);
插入多条
INSERT into books(bno,bname,author,price,quanitity)values(1003,'水浒传',"xxx",50.6,10),(1004,'三国演义',"xxx",50,50);
四、查询数据
select * from books;#查询所有
SELECT bname,price,quanitity from books;#查询指定列
五、更新数据
UPDATE into set price=100 WHERE bname="红楼梦";
六、删除数据
select * from info;
DELETE from info where cno=105;