数据库模式定义语言并非程序设计语言,DDL数据库模式定义语言是SQL语言(结构化查询语言)的组成部分。
SQL语言包括:四种主要程序设计语言类别的语句:数据定义语言(DDL,Data Definition Language),数据操作语言(Data Manipulation Language, DML),数据控制语言(DCL)和事务控制语言(TCL)。
这里主要介绍DDL和DML语言
常见的DDL和DML有:
DDL{ CREATE DATABASE 创建数据库,CREATE TABLE 创建数据库表格,ALTER TABLE 修改数据库表格,DROP TABLE删除数据库表格,CREATE VIEW 创建查询命令,ALTER VIEW 修改查询命令,DROP VIEW删除查询命令}
DML{SELECT,INSERT,UPDATE,DELETE}
1.登录数据库:mysql -u root -p
例如:
sue@suepc:~/abc$ mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 7
Server version: 5.7.19 MySQL Community Server (GPL)
2.显示当前所有数据库:show databases;
例如:
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| abc |
| boss |
| eg |
| example |
| lxh |
| mysql |
| performance_schema |
| sys |
+--------------------+
9 rows in set (0.38 sec)
3.连接数据库:use database(database为数据库名),以下使用的是abc数据库;
例如:
mysql> use abc;
Database changed
4.查看当前连接的数据库:select database();
例如:
mysql> select database();
+------------+
| database() |
+------------+
| abc |
+------------+
1 row in set (0.00 sec)
5.查看当前数据库包含的表信息: show tables;(注意:最后有个s)
mysql> show tables;
+---------------------------+
| Tables_in_abc
+---------------------------+
| user |
+---------------------------+
1rows in set (0.00 sec)
6.删除数据库:drop database abc;删除后查看数据库是否存在:use abc;(下列例子数据库不存在所以报错)
例如:
mysql> drop database abc;
Query OK, 0 rows affected (0.36 sec)
mysql> use abc;
ERROR 1049 (42000): Unknown database 'abc'
7.下边开始建立一个新的数据库并进行增删查改。注意:表操作,操作之前应连接某个数据库。
例如:
/*新建一个名为suedatabase的数据库*/
mysql> create database sue_database;
Query OK, 1 row affected (0.04 sec)
/*连接数据库*/
mysql> use sue_database;
Database changed
/*建表*/
mysql> CREATE TABLE mytable (name VARCHAR(20), sex CHAR(1),birth DATE, birthaddr VARCHAR(20));
Query OK, 0 rows affected (0.19 sec)
/*插入数据*/
/*Insert是DML语言,主要是插入数据*/
/*Alter是DDL语言,主要是比如修改一些表结构,修改用户等*/
mysql> insert into mytable value ('aaa','f','1985-11-6','us');
Query OK, 1 row affected (0.04 sec)
/*在表中增加字段:*/
mysql> alter table mytable add column num int(30)
-> ;
Query OK, 0 rows affected (0.46 sec)
Records: 0 Duplicates: 0 Warnings: 0
/*查看表结构*/
mysql> desc mytable;
+-----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------+-------------+------+-----+---------+-------+
| name | varchar(20) | YES | | NULL | |
| sex | char(1) | YES | | NULL | |
| birth | date | YES | | NULL | |
| birthaddr | varchar(20) | YES | | NULL | |
| num | int(30) | YES | | NULL | |
+-----------+-------------+------+-----+---------+-------+
5 rows in set (0.02 sec)
mysql> insert mytable value('abc','m',1991-11-12,'jp',50);
ERROR 1292 (22007): Incorrect date value: '1968' for column 'birth' at row 1
mysql> select * from mytable;
+------+------+------------+-----------+------+
| name | sex | birth | birthaddr | num |
+------+------+------------+-----------+------+
| aaa | f | 1985-11-06 | us | NULL |
+------+------+------------+-----------+------+
1 row in set (0.00 sec)
/*注意:“Insert 向数据库表添加是数据行”,Alter 修改数据库表属性(如新增资料行)*/
/*DML语言update ,delete, select ,insert*/
/*更新字段内容*/
mysql> update mytable set num=50 where birth = "1985-11-06" ;
Query OK, 1 row affected (0.09 sec)
Rows matched: 1 Changed: 1 Warnings: 0
/*查看表 mytable 中所有数据*/
mysql> select * from mytable;
+------+------+------------+-----------+------+
| name | sex | birth | birthaddr | num |
+------+------+------------+-----------+------+
| aaa | f | 1985-11-06 | us | 50 |
+------+------+------------+-----------+------+
1 row in set (0.00 sec)
8.导入数据库abc.sql
例如:
方法1:
sue@suepc:~/abc$ mysqldump -u root -p 123456 abc < /home/sue/abc/abc.sql
Enter password:
-- MySQL dump 10.13 Distrib 5.7.19, for Linux (x86_64)
--
-- Host: localhost Database: 123456
-- ------------------------------------------------------
-- Server version 5.7.19
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 34
Server version: 5.7.19 MySQL Community Server (GPL)
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| abc |
+--------------------+
2 rows in set (0.12 sec)
方法二:
导入数据库,常用source 命令进入mysql数据库控制台,
如mysql -u root -p
mysql>use 数据库
然后使用source命令,后面参数为脚本文件(如这里用到的.sql)
mysql>source /home/sue/abc/defg.sql;
备份数据库:sue@suepc:~$ mysqldump -u root -p sue_database > 123.sql (出错)
mysqldump: Couldn't execute 'SHOW VARIABLES LIKE 'gtid\_mode'': Table 'performance_schema.session_variables' doesn't exist (1146)