iis服务器助手广告广告
返回顶部
首页 > 资讯 > 数据库 >mysql sql语句学习(一)
  • 679
分享到

mysql sql语句学习(一)

2024-04-02 19:04:59 679人浏览 独家记忆
摘要

     为了学习数据库,先熟悉一下sql语言,也就以Mysql为例子开始学习了! (参考书籍《深入浅出mysql 数据库开发、优化、管理维护》) &

     为了学习数据库,先熟悉一下sql语言,也就以Mysql为例子开始学习了!

(参考书籍《深入浅出mysql 数据库开发优化、管理维护》)

   下边是执行的sql语句,主要是建立表和修改表:

首先进入windows命令提示符下:

Microsoft Windows XP [版本 5.1.2600]
(C) 版权所有 1985-2001 Microsoft Corp.

C:\Documents and Settings\Administrator>mysql -uroot -proot
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.1.41-commUnity MySQL Community Server (GPL)

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| infORMation_schema |
| mysql              |
| test               |
+--------------------+
3 rows in set (0.02 sec)

mysql> use test;
Database changed
mysql> create table emp(ename varchar(20) not null primary key,hirdate date,sal
decimal(10,2),deptno int(2));
Query OK, 0 rows affected (0.08 sec)

mysql> desc emp;
+---------+---------------+------+-----+---------+-------+
| Field   | Type          | Null | Key | Default | Extra |
+---------+---------------+------+-----+---------+-------+
| ename   | varchar(20)   | NO   | PRI | NULL    |       |
| hirdate | date          | YES  |     | NULL    |       |
| sal     | decimal(10,2) | YES  |     | NULL    |       |
| deptno  | int(2)        | YES  |     | NULL    |       |
+---------+---------------+------+-----+---------+-------+
4 rows in set (0.02 sec)

mysql> show create table emp;
+-------+-----------------------------------------------------------------------
--------------------------------------------------------------------------------
-----------------------------------------------------------------+
| Table | Create Table

                                                                 |
+-------+-----------------------------------------------------------------------
--------------------------------------------------------------------------------
-----------------------------------------------------------------+
| emp   | CREATE TABLE `emp` (
  `ename` varchar(20) NOT NULL,
  `hirdate` date DEFAULT NULL,
  `sal` decimal(10,2) DEFAULT NULL,
  `deptno` int(2) DEFAULT NULL,
  PRIMARY KEY (`ename`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 |
+-------+-----------------------------------------------------------------------
--------------------------------------------------------------------------------
-----------------------------------------------------------------+
1 row in set (0.00 sec)

mysql> alter table emp modify ename varchar(30);
Query OK, 0 rows affected (0.19 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> desc emp;
+---------+---------------+------+-----+---------+-------+
| Field   | Type          | Null | Key | Default | Extra |
+---------+---------------+------+-----+---------+-------+
| ename   | varchar(30)   | NO   | PRI |         |       |
| hirdate | date          | YES  |     | NULL    |       |
| sal     | decimal(10,2) | YES  |     | NULL    |       |
| deptno  | int(2)        | YES  |     | NULL    |       |
+---------+---------------+------+-----+---------+-------+
4 rows in set (0.00 sec)

mysql> alter table emp add column age int(3);
Query OK, 0 rows affected (0.19 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> desc emp;
+---------+---------------+------+-----+---------+-------+
| Field   | Type          | Null | Key | Default | Extra |
+---------+---------------+------+-----+---------+-------+
| ename   | varchar(30)   | NO   | PRI |         |       |
| hirdate | date          | YES  |     | NULL    |       |
| sal     | decimal(10,2) | YES  |     | NULL    |       |
| deptno  | int(2)        | YES  |     | NULL    |       |
| age     | int(3)        | YES  |     | NULL    |       |
+---------+---------------+------+-----+---------+-------+
5 rows in set (0.00 sec)

mysql> alter table emp change age agenum int(4);
Query OK, 0 rows affected (0.05 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> desc emp;
+---------+---------------+------+-----+---------+-------+
| Field   | Type          | Null | Key | Default | Extra |
+---------+---------------+------+-----+---------+-------+
| ename   | varchar(30)   | NO   | PRI |         |       |
| hirdate | date          | YES  |     | NULL    |       |
| sal     | decimal(10,2) | YES  |     | NULL    |       |
| deptno  | int(2)        | YES  |     | NULL    |       |
| agenum  | int(4)        | YES  |     | NULL    |       |
+---------+---------------+------+-----+---------+-------+
5 rows in set (0.00 sec)

mysql> alter table emp drop agenum;
Query OK, 0 rows affected (0.17 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> desc emp;
+---------+---------------+------+-----+---------+-------+
| Field   | Type          | Null | Key | Default | Extra |
+---------+---------------+------+-----+---------+-------+
| ename   | varchar(30)   | NO   | PRI |         |       |
| hirdate | date          | YES  |     | NULL    |       |
| sal     | decimal(10,2) | YES  |     | NULL    |       |
| deptno  | int(2)        | YES  |     | NULL    |       |
+---------+---------------+------+-----+---------+-------+
4 rows in set (0.00 sec)

mysql> alter table emp add column birth date after ename;
Query OK, 0 rows affected (0.16 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> desc emp;
+---------+---------------+------+-----+---------+-------+
| Field   | Type          | Null | Key | Default | Extra |
+---------+---------------+------+-----+---------+-------+
| ename   | varchar(30)   | NO   | PRI |         |       |
| birth   | date          | YES  |     | NULL    |       |
| hirdate | date          | YES  |     | NULL    |       |
| sal     | decimal(10,2) | YES  |     | NULL    |       |
| deptno  | int(2)        | YES  |     | NULL    |       |
+---------+---------------+------+-----+---------+-------+
5 rows in set (0.00 sec)

mysql> alter table emp modify sal first;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that
corresponds to your MySQL Server version for the right syntax to use near 'first
' at line 1
mysql> alter table emp modify sal decimal(10,2) first;
Query OK, 0 rows affected (0.16 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> desc emp;
+---------+---------------+------+-----+---------+-------+
| Field   | Type          | Null | Key | Default | Extra |
+---------+---------------+------+-----+---------+-------+
| sal     | decimal(10,2) | YES  |     | NULL    |       |
| ename   | varchar(30)   | NO   | PRI |         |       |
| birth   | date          | YES  |     | NULL    |       |
| hirdate | date          | YES  |     | NULL    |       |
| deptno  | int(2)        | YES  |     | NULL    |       |
+---------+---------------+------+-----+---------+-------+
5 rows in set (0.00 sec)

mysql> alter table emp change ename name varchar(20) first;
Query OK, 0 rows affected (0.16 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> desc emp;
+---------+---------------+------+-----+---------+-------+
| Field   | Type          | Null | Key | Default | Extra |
+---------+---------------+------+-----+---------+-------+
| name    | varchar(20)   | NO   | PRI |         |       |
| sal     | decimal(10,2) | YES  |     | NULL    |       |
| birth   | date          | YES  |     | NULL    |       |
| hirdate | date          | YES  |     | NULL    |       |
| deptno  | int(2)        | YES  |     | NULL    |       |
+---------+---------------+------+-----+---------+-------+
5 rows in set (0.00 sec)

mysql> alter table emp change birth birthday date after hirdate;
Query OK, 0 rows affected (0.23 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> desc emp;
+----------+---------------+------+-----+---------+-------+
| Field    | Type          | Null | Key | Default | Extra |
+----------+---------------+------+-----+---------+-------+
| name     | varchar(20)   | NO   | PRI |         |       |
| sal      | decimal(10,2) | YES  |     | NULL    |       |
| hirdate  | date          | YES  |     | NULL    |       |
| birthday | date          | YES  |     | NULL    |       |
| deptno   | int(2)        | YES  |     | NULL    |       |
+----------+---------------+------+-----+---------+-------+
5 rows in set (0.00 sec)

mysql> alter table emp rename emptable;
Query OK, 0 rows affected (0.22 sec)

mysql> show tables;
+----------------+
| Tables_in_test |
+----------------+
| emptable       |
+----------------+
1 row in set (0.00 sec)

mysql> drop table emptable;
Query OK, 0 rows affected (0.06 sec)

mysql> show tables;
Empty set (0.00 sec)

mysql> exit;
Bye

C:\Documents and Settings\Administrator>

您可能感兴趣的文档:

--结束END--

本文标题: mysql sql语句学习(一)

本文链接: https://www.lsjlt.com/news/46677.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • mysql sql语句学习(一)
         为了学习数据库,先熟悉一下sql语言,也就以mysql为例子开始学习了! (参考书籍《深入浅出mysql 数据库开发、优化、管理维护》) &...
    99+
    2022-10-18
  • 【MySQL学习笔记】2、SQL语句
    一、SQL语句SQL:Structured Query Language,结构化查询语言,是客户端和SQL服务器进行对话的语言用于跟SQL服务器对话属于解释执行的编程语言,其代码文件通常称为脚本,直接被解释...
    99+
    2022-10-18
  • MYSQL学习系列--DML语句(一)
    引言: 数据操纵语言(Data Manipulation Language, DML)是SQL语言中,负责对数据库对象运行数据访问工作的指令集,以INSERT、UPDATE、DELETE三种指令为核...
    99+
    2022-10-18
  • mysql学习笔记(三)--- 基本的SQL语句
    【正文】主要内容:一、cmd命令行的常用命令二、数据定义语言(DDL)三、数据操纵语言(DML)四、数据查询语言(DRL)五、事务控制语言(TCL)一、cmd命令行的常用命令:当我们使用MySQL 5.5 ...
    99+
    2022-10-18
  • SQL语句学习之路3
    到目前为止,我们已学到如何藉由  SELECT  及  WHERE   这两个指令将资料由表格中抓出。不过我们尚未提到这些资料要如何排列。这其实是一个很重要的问题。事实上,我们经常需要能够将抓出的资料做一个有系统的显示。这可...
    99+
    2023-01-31
    之路 语句 SQL
  • 学习MySQL的select语句
    select语句可 以用回车分隔 $sql="select * from article where id=1&...
    99+
    2022-10-18
  • Oracle 学习之性能优化(一)SQL语句处理
      当向Oracle提交一个sql命令时,Oracle到底做了哪些事情?对这个问题有很好的理解,能帮助你更好的分析sql语句的优化。  执行一条sql语句从开始到结束,需要经历4个步骤:...
    99+
    2022-10-18
  • MYSQL学习系列--DDL语句
    DDL语句: 对数据库内部的对象进行创建、删除、修改等操作的语言,DDL语句更多的是由数据库管理员(DBA)使用,开发人员一般很少使用登录mysql之后就可以使用sql语句对数据库进行各种操作啦! 实践操作...
    99+
    2022-10-18
  • SQL -- 简单语句学习总结7条
    --if object_id('guestbook') is  null--CREATE TABLE guestbook (id int PRIMARY KEY, visitor VARCHA&#...
    99+
    2022-10-18
  • sql语句的学习方法有哪些
    本篇内容介绍了“sql语句的学习方法有哪些”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!超强:SQL命令中...
    99+
    2022-10-18
  • Oracle学习篇之SQL语句的优化
    Oracle学习篇之SQL语句的优化①在使用SELECT语句查询时,不要用“*”代替所有列名,因为这样的写法对Oracle系统来说会存在解析的动态问题。Oracle系统会通过查询数据字典来将“*”转...
    99+
    2022-10-18
  • MYSQL学习系列--DML语句(二)
    引言: 数据操纵语言(Data Manipulation Language, DML)是SQL语言中,负责对数据库对象运行数据访问工作的指令集,以INSERT、UPDATE、DELETE三种指令为核...
    99+
    2022-10-18
  • mysql 一些常用sql语句
    -- 修改表注释 ALTER table table_name comment "需要修改注释的信息"; -- 修改root 密码 ALTER USER "root"@"localhost" IDENTIFIED BY "123456"...
    99+
    2020-11-06
    mysql 一些常用sql语句
  • MySQL一些常用高级SQL语句
    MySQL高级SQL语句 use kgc; create table location (Region char(20),store_name char(20)); insert into l...
    99+
    2022-05-11
    mysql高级sql语句 mysql sql语句
  • 50个SQL语句(MySQL版) 问题一
    --------------------------表结构-------------------------- student(StuId,StuName,StuAge,StuSex) 学生表 teacher(TId,Tname)...
    99+
    2017-05-02
    50个SQL语句(MySQL版) 问题一
  • 重新学习MySQL数据库12:从实践sql语句优化开始
    本文转自互联网 本系列文章将整理到...
    99+
    2022-10-18
  • python中pass语句学习
    pass语句什么也不做,一般作为占位符或者创建占位程序,pass语句不会执行任何操作,比如: while False:passpass通常用来创建一个最简单的类:class MyEmptyClass:passpass在软件设计阶段也经...
    99+
    2023-01-31
    语句 python pass
  • MySQL 数据库SQL语句---DDL语句
    SQL语句---DDL语句==============================================================================概述:=========...
    99+
    2022-10-18
  • java学习之switch语句与循环语句
    switch语句int a = 1,b =2; switch(a+b){ case 1: System.out.print(1); case 3: System.out.print(3); case 4: System.o...
    99+
    2014-05-20
    java switch语句 循环语句
  • 50个SQL语句(MySQL版) 问题十一
    --------------------------表结构-------------------------- student(StuId,StuName,StuAge,StuSex) 学生表 teacher(TId,Tname)...
    99+
    2021-10-15
    50个SQL语句(MySQL版) 问题十一
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作