MySQL笔记大全

SQL语句笔记大全

第一章 DDL数据定义语言

1.1数据库的相关操作


-- 数据库的查看
show databases;

-- 数据库的创建
-- create database [if not exists] 库名 
-- if not exists:表示如果不存在就创建(非必须条件)存在了就不创建了
create database school;
create database if not exists school;

-- 数据库的修改
-- rename database 原来的库名 to 现在的库名
rename database school to s_school;       -- 修改库名 
-- 数据库的删除
-- drop database 库名
drop database school;
-- drop database [if exists]库名 如果存在就删除
drop database if exists school;



1.2数据表的相关操作

-- 表的创建
-- 在创建表之前需要使用数据库(也就是需要进入数据库)
-- create table[if not exists]表名(
-- 字段名1 字段类型 [约束],    -- 约束非必须条件 类型必须填
-- 字段名2 字段类型 [约束],
-- 字段名3 字段类型 [约束]
--);
use school;-- 进入数据库
-- 创建表格
create table if not exists  students(
  sid int,-- 约束暂时不添加 后面会专门讲约束
  ssex char(2),
  sname varchar(10),
  sage int,
  sclassid int 
);

-- 表的修改(修改表结构 其实就是增删改)
-- 增加表中列
-- alter table 表名 add column 列名 类型;
alter table students add column sscore float;

-- 修改表中列
-- alter table 表名 modify column 列名 新类型;
alter table students modify column s_name varchar(20);

-- 修改列名
-- alter table 表名 change column 旧列名 新列名 类型;
alter table students change column sid snum;

-- 删除表中列
-- alter table 表名 drop column 列名;
alter table students drop column sclassid;

-- 修改表名
-- rename table 原表名 to 现表名;
rename table students to students_tb;

-- 查看数据表结构
desc students;

-- 查看数据表
-- 这里查看的是数据库中所有的数据表(不是这数据表中的数据)
show tables;

-- 删除数据表
-- drop [if exists] 表名;
drop if exists students;
-- truncate table 表名;
truncate table students;

-- 另外一种创建表的方式(数据表的拷贝(备份))
-- 拷贝表结构
-- create table 现表名 like 原表名
create table students_tb1 like students;

-- 拷贝表的某些字段
-- create table 新表名 select 字段1,字段2,.....from 原表名 ;
create table students_tb1  select sid,sname,sage from students;

-- 拷贝整张表
-- create table 新表名 slect *from 原表名;
create table students_tb2 select *from students;

-- 其他的类似(就不过多写了)
 


1.3 数据类型(提一下常用的,与c/c++有许多相同数据类型)

1.浮点型:
float :4个字节

double: 8字节

2.字符型:
char():固定长度字符 char(m) :表示最大长度不能够大于m而且表示一共给了2m个字节的空间,不管你用没用完都是这么多。

varchar():可变长度的字符类型,varchar(m)表示最大长度不能够超过m,但是在不超过m长度的范围内,储存空间是用了多少就是多少。 (比较灵活)

3.数值类型(不多说)

4.日期类型(数据库函数(日期函数在详细介绍))

1.4约束(本身也是一个对象,用来限制表中数据的)

一、分类:

  1. not null:非空约束
  2. unique:唯一约束 (可以为空)
  3. 主键约束(primary key):unique+not null+索引(值不能够为空而且必须唯一不能够有重复值)
  4. 外键约束(foreign key):外键可以为空,外键可以不唯一,外键的值等于引用字段的值
  5. 检查约束 :not null check();
    二、废话不多说 (上代码)
--创建表格
create table student (
  sid int primary key,-- 设置id 为主键(第一种添加主键的方式)
  sname varchar(20) not null,-- 非空约束
  ssex char(2) not null check(sex='男'or sex='女') -- 检查约束
);
create table sc(
   sid int not null;
   score float not null;
);
-- 由于约束本身就是对象,所以可以用DDL语句来操作
-- 这里就展示主键,外键,其他的类似(更简单)
-- 添加主键
-- constraint 给约束取别名
-- alter table 表名 add constraint 约束名 primary key (字段名)
alter table sc add constraint pk_sc_score primary key (score);

-- 删除主键
-- alter table 表名 drop primary key;
alter table sc drop primary 
  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值