在线咨询
手机请直接输入:如1860086xxxx
座机前加区号:如0105992xxxx
输入您的电话号码,点击通话,稍后您将接到我们的电话,该通话对您完全免费,请放心接听!
座机前加区号:如0105992xxxx
输入您的电话号码,点击通话,稍后您将接到我们的电话,该通话对您完全免费,请放心接听!
扫描二维码
数据表是数据库最重要的组成部分之一,是其它对象的基础
数据表是存储数据的数据结构
数据表是包含了特定实体类型的数据
数据表由行(row)和列(column)构成的二维网络
数据表一定先有表结构,再有数据
数据表至少有一列,可以没有行或者多行
数据表名称要求唯一,而且不要包含特殊字符
CREATE TABLE [IF NOT EXISTS] tbl_name( 字段名称 字段类型 [完整性约束条件] ... )ENGINE=引擎名称 CHARSET='编码方式';
完整性约束条件
PRIMARY KEY主键
AUTO_INCREMENT自增长
FOREIGN KEY外键
NOT NULL非空
UNIQUE KEY唯一
DEFAULT默认值
mysql> SHOW DATABASES; +--------------------+ | Database | +--------------------+ | information_schema | | abc | | maizi1 | | maizi2 | | mysql | | performance_schema | | test | | test1 | +--------------------+ 8 rows in set (0.00 sec)
mysql> DROP DATABASE abc; Query OK, 0 rows affected (0.22 sec)
mysql> DROP DATABASE maizi1; Query OK, 0 rows affected (0.00 sec)
mysql> DROP DATABASE maizi2; Query OK, 0 rows affected (0.00 sec)
mysql> DROP DATABASE test1; Query OK, 0 rows affected (0.01 sec)
mysql> SELECT NOW(); +---------------------+ | NOW() | +---------------------+ | 2014-12-27 12:37:59 | +---------------------+ 1 row in set (0.03 sec)
mysql> # SELECT NOW(); mysql> # SELECT NOW(); mysql> # SELECT NOW(); mysql> -- SELECT DATABASE(); mysql> \s --------------
Connection id: 7 Current database: Current user: root@localhost SSL: Not in use Using delimiter: ; Server version: 5.6.22-log MySQL Community Server (GPL) Protocol version: 10 Connection: localhost via TCP/IP Server characterset: utf8 Db characterset: utf8 Client characterset: utf8 Conn. characterset: utf8 TCP port: 3306 Uptime: 3 hours 10 min 55 sec
Threads: 1 Questions: 57 Slow queries: 0 Opens: 75 Flush tables: 1 Open tables: 66 Queries per second avg: 0.004 -------------- mysql> SET NAMES GBK; Query OK, 0 rows affected (0.04 sec) mysql> \s -------------- Connection id: 7 Current database: Current user: root@localhost SSL: Not in use Using delimiter: ; Server version: 5.6.22-log MySQL Community Server (GPL) Protocol version: 10 Connection: localhost via TCP/IP Server characterset: utf8 Db characterset: utf8 Client characterset: gbk Conn. characterset: gbk TCP port: 3306 Uptime: 3 hours 11 min 2 sec Threads: 1 Questions: 61 Slow queries: 0 Opens: 75 Flush tables: 1 Open tables: 66 Queries per second avg: 0.005 -------------- mysql> SELECT DATABASE(); +------------+ | DATABASE() | +------------+ | NULL | +------------+ 1 row in set (0.00 sec) mysql> CREATE TABLE IF NOT EXISTS `user`( -> id SMALLINT, -> username VARCHAR(20), -> age TINYINT, -> sex ENUM('男,'女,'保密'), -> email VARCHAR(50), -> addr VARCHAR(200), -> birth YEAR, -> salary FLOAT(8,2), -> tel INT, -> married TINYINT(1) COMMENT '〃〃' -> )ENGINE=INNODB CHARSET=UTF8; ERROR 1046 (3D000): No database selected mysql> # 注释内容 mysql> -- 注释内容 mysql> -- 创建maizi数据库 mysql> CREATE DATABASE IF NOT EXISTS `maizi` DEFAULT CHARACTER SET 'UTF8'; Query OK, 1 row affected (0.02 sec)
mysql> mysql> USE `maizi`; Database changed mysql> mysql> -- 创建学院表(user) mysql> -- 编号 id mysql> -- 用户名 username mysql> -- 年龄 age mysql> -- 性别 sex mysql> -- 邮箱 email mysql> -- 地址 addr mysql> -- 生日 birth mysql> -- 薪水 salary mysql> -- 电话 tel mysql> -- 是否结婚married mysql> -- 注意:当需要输入中文的时候,需要临时转换客服端的编码方式 mysql> -- SET NAMES GBK; mysql> -- 字段注释通过COMMENT 注释内容给字段添加注释? mysql> CREATE TABLE IF NOT EXISTS `user`( -> id SMALLINT, -> username VARCHAR(20), -> age TINYINT, -> sex ENUM('男’,'女,'保密'), -> email VARCHAR(50), -> addr VARCHAR(200), -> birth YEAR, -> salary FLOAT(8,2), -> tel INT, -> married TINYINT(1) COMMENT '0代表未结婚,1代表结婚' -> )ENGINE=INNODB CHARSET=UTF8; Query OK, 0 rows affected (0.24 sec) mysql> mysql> mysql> mysql> mysql> mysql> mysql> mysql> mysql> mysql> mysql> mysql> mysql> mysql> mysql>
查看数据库下的数据表
SHOW TABLES mysql> SHOW TABLES; +-----------------+ | Tables_in_maizi | +-----------------+ | user | +-----------------+ 1 row in set (0.00 sec) mysql> CREATE TABLE IF NOT EXISTS course( -> cid TINYINT, -> courseName VARCHAR(50), -> courseDesc VARCHAR(200) -> ); Query OK, 0 rows affected (0.19 sec) mysql> SHOW TABLES; +-----------------+ | Tables_in_maizi | +-----------------+ | course | | user | +-----------------+ 2 rows in set (0.00 sec)
Copyright© 2019 巨立鑫 All Rights Reserved 皖ICP备17011067号-4 网址:http://www.jlxpx168.com 技术支持:巨立鑫软件技术部