当前位置: 首页 > news >正文

点样用外网访问自己做的网站免费学校网站建设

点样用外网访问自己做的网站,免费学校网站建设,网站开发中设置会员等级,铁岭做网站公司信息DDL 数据定义语言 目录概述一、库的管理1、库的创建2、库的修改【一般不修改,容易出现错误】3、库的删除二、表的管理【重要】1、表的创建2、表的修改3、表的删除4、表的复制 【可以跨库复制】练习题概述 数据定义语言 库和表的管理 一、库的管理 创建、修改、删除…

DDL 数据定义语言

目录

    • 概述
    • 一、库的管理
      • 1、库的创建
      • 2、库的修改【一般不修改,容易出现错误】
      • 3、库的删除
    • 二、表的管理【重要】
      • 1、表的创建
      • 2、表的修改
      • 3、表的删除
      • 4、表的复制 【可以跨库复制】
    • 练习题

概述

数据定义语言
库和表的管理
一、库的管理
创建、修改、删除(删除的是整个表而不是数据)
二、表的管理
创建、修改、删除

创建:create
修改:alter
删除:drop

一、库的管理

1、库的创建

语法:

create databaseif not exists】 库名【 character set 字符集名称】;

案例:创建库Books

create database Books;

【补充】MySQL库中数据的存储路径
记录在电脑中的位置:D:\MySQLData\MySQL\MySQL Server 8.0\Data
在这里插入图片描述

2、库的修改【一般不修改,容易出现错误】

可以更改库的字符集

alter database books character set gbk;#utf8mb4

要修改库名的话需要进本地的data文件夹重命名来改,并重新启动MySQL80服务

3、库的删除

drop database books;
drop database if exists books;

二、表的管理【重要】

create database if not exists books;
use books;

1、表的创建

  • 语法:
      create table 表名(列名 列的类型【(类型对应的长度) 列的约束】,create table 表名(列名 列的类型【(类型对应的长度) 列的约束】,……,create table 表名(列名 列的类型【(类型对应的长度) 列的约束】);
    
  • 案例:创建表book
    create table book(id int,bName varchar(20),price double,authorid int,publishdate datetime);
    create table if not exists book(id int,bName varchar(20),price double,authorid int,publishdate datetime);
    desc book;
    
  • 案例:创建authors表
    create table authors(id int,au_name varchar(20),nation varchar(20));
    desc authors;
    

2、表的修改

  1. 修改列名
    alter table 表名 change 【column 】原列名 新列名 列的类型;
    
  2. 修改列的类型或约束
    alter table 表名 modify column 列名 要修改的列的类型;
    
  3. 添加列(默认将新列添加到所有列的后面)
    alter table 表名 add column 列名 列的类型;
    
    要添加到指定位置(不支持last)
    alter table 表名 add column 列名 列的类型 first;#将新列作为第一列
    alter table 表名 add column 列名 列的类型 after 字段名;#将新列  放在某一列的后面
    
  4. 删除列
    alter table 表名 drop column 列名;
    
  5. 修改表名
    alter table 原表名 rename to 新表名;
    
alter table 表名 add|drop|change|modify column 列名 【列的类型 约束】;
#案例:将book表中的publish列的名字修改为pubdate
alter table book change column publishdate pubdate datetime;
desc book;
#案例:将book表中的publish列的类型修改为timestamp
alter table book modify column pubdate timestamp;
desc book;
#案例:在authors表中添加年薪列annual,类型为double
alter table authors add column annual double;#必须加column
desc authors;
#案例:删除authors表中的annual列 
alter table authors drop column annual;
desc authors;
#注:删除列的时候无法使用if exists关键字
#案例:
alter table authors rename to book_author;
desc book_author;

3、表的删除

语法:

drop tableif not exists】 表名;

案例:删除作者的信息表

drop table book_author;
#添加容错性处理 
drop table if exists book_author;
show tables;#查看当前数据库的所有表 

通用的写法

drop database if exists 旧库名;
create database 新库名;drop table 旧表名;
create table 表名();

4、表的复制 【可以跨库复制】

create table authors(id int,au_name varchar(20),nation varchar(20));
insert into authors values(1,'村上春树','日本'),(2,'莫言','中国'),(3,'冯唐','中国'),(4,'金庸','中国');
select * from authors;
  1. 仅仅只能复制表的结构
     create table copy1 like authors;desc copy1;select * from copy1;
    
  2. 复制表的结构加数据
    create table copy2 select * from authors;
    desc copy2;
    select * from copy2;
    
  3. 只复制部分数据(可选部分字段)
    create table copy3 select id,au_name from authors where nation='中国';
    desc copy3;
    select * from copy3;
    

推广:仅仅复制某些字段(不加数据)

create table copy4 select id,au_name from authors where 1=2;
create table if not exists copy4 select id,au_name from authors where 0;
select * from copy4;

练习题

  1. 创建表 dept1
    nameNull?type
    idint(7)
    namevarchar(25)
    create database if not exists test;
    use test;
    create table dept1(id int(7),name varchar(25));
    desc dept1;
    
  2. 将表 departments 中的数据插入新表 dept2 中
    create table dept2 select * from myemployees.departments;
    select * from dept2;
    
  3. 创建表 emp5
    nameNull?type
    idint(7)
    First_nameVarchar (25)
    Last_nameVarchar(25)
    Dept_idint(7)
    create table emp5(id int(7),First_name varchar(25),Last_name varchar(25),Dept_id int(7));
    desc emp5;
    
  4. 将列 Last_name 的长度增加到 50
    alter table emp5 modify column Last_name varchar(50);
    desc emp5;
    
  5. 根据表 employees 创建 employees2
    create table employees2 like myemployees.employees;
    desc employees2;
    select * from employees2;
    
  6. 删除表 emp5
    drop table emp5;
    show tables;
    
  7. 将表 employees2 重命名为 emp5
    alter table employees2 rename to emp5;
    show tables;
    
  8. 在表 dept1 和 emp5 中添加新列 test_column,并检查所作的操作
    alter table dept1 add column test_column int;
    alter table emp5 add column test_column int;
    desc dept1;
    desc emp5;
    
  9. 直接删除表 emp5 中的列 test_column
    alter table emp5 drop column test_column;
    desc emp5;
    
http://www.yayakq.cn/news/514411/

相关文章:

  • 淘宝网站建设哪个类目做一晚水泥工歌曲网站
  • linux做网站1G内存够不自己能开发app软件吗
  • php本地建站工具3g版和wap网站
  • 高校建设主流网站网页ui设计尺寸
  • 贸易网站建设网哪些网站可以在线做动图
  • 网络服务商的责任保定网站优化排名
  • 手机ps软件如何做ppt下载网站彩票网站建设成本
  • 松岗做网站哪家便宜鲁权屯网站建设
  • 网站开发的关系图和e-r图网站开发行业标准
  • 怎么建设银行网站打不开如何快速推广app
  • 公司手机网站效果图淮北哪有做网站的
  • 安徽区块链虚拟币网站开发方案mxd 主题Wordpress
  • 网站开发工具c医院建筑设计案例
  • 网站模块怎么恢复手工制作国庆节作品图片
  • 苏州网站建设网媒体资源网官网
  • 网站备案密码是什么样的西安的商城网站
  • 广州网络营销首荐佐兰网络vip徐州整站优化
  • 法治网站的建设整改措施wordpress 多重筛选
  • 北京市建设工程发包承包交易中心网站个网站能申请贝宝支付接口
  • 大数据营销笔记本人员优化是什么意思
  • 重庆网站平台如何推广word里网站的超链接怎么做
  • 免费可商用素材网站怎么把图片做成链接形式
  • 访问wordpress数据库上海做网站优化公司
  • 湘潭学校网站建设 z磐石网络苏州短视频运营
  • 淘宝联盟网站模板唯品会官网一家做特卖的网站
  • 个人网站域名快速备案基于jsp网站开发
  • 做别人一样的网站吗可以做问卷的网站有哪些
  • 免费的ai绘图网站有哪些网站logo制作软件
  • 仙居做网站公司企业类网站模版
  • 保定建站方案广州的网站建设