您好,登录后才能下订单哦!
1.数据类型
数据类型 | 存储范围 | 字节 | |
TINYINT | 有符号型:-128~127(-2^7~2^7 -1),无符号型0~255(0~2^8 -1) | 1 | |
SMALLINT | 有符号型:-2^15~2^15 -1,无符号型0~2^16 -1 | 2 | |
MEDIUMINT | 有符号型:-2^23~2^23 -1,无符号型0~2^24 -1 | 3 | |
INT | 有符号型:-2^31~2^31 -1,无符号型0~2^32 -1 | 4 | |
BIGINT | 有符号型:-2^63~2^63 -1,无符号型0~2^64 -1 | 8 | |
数据类型 | 存储范围 | 字节 | |
FLOAT[(M,D)] | -3.402823466E+38~-1.175494351E-38、0和 1.175494351E-38 ~3.402823466E+38 | 4 | |
DOUBLE[(M,D)] | 2.2250738585072014E-308~ 1.7976931348623157E+308 | 8 | |
列类型 | 字节 | 存储范围 | |
YEAR | 1 | YEAR[(2|4)] 缺省为4位格式,4位格式取值范围为1901 - 2155, | |
TIME | 3 | -838:59:59~838:59:59 | |
DATE | 3 | 1000-1-1~9999-12-31 | |
DATETIME | 8 | 日期时间, 1000-1-1 00:00:00~9999-12-31 23:59:59 | |
TIMESTAMP | 4 | 时间戳,1970-1-1 00:00~2037年的某个时刻 | |
列类型 | 存储需求 | |
CHAR(M) | (定长,不足用空格自动补全)M个字节,0<=M<=255 | |
VARCHAR(M) | (变长,输入多少就是多少)L+1个字节,其中L<=M,0<=M<=65535 | |
TINYTEXT | L+1个字节,其中L<2^8 (1个字节表示最大存储范围,下同) | |
TEXT | L+2个字节,其中L<2^16 | |
MEDIUMTEXT | L+3个字节,其中L<2^24 | |
LONGTEXT | L+4个字节,其中L<2^32 | |
ENUM('value1','value2',…) | (最终选择其中的一个value)1或2个字节,取决于枚举值的个数(最多65535个) | |
SET ('value1','value2',…) | (集合,可在其中做任意的排列组合)1、2、3、4或者8个字节,取决于set成员的 | |
2.数据表操作
mysql> USE <数据库名称>;
mysql> SELETE DATABASES;
创建数据库表
CREATE TABLE [IF NOT EXISTS] table_name (column_name列名称 data_type数据类型,....)
mysql> USE t1; mysql> CREATE TABLE IF NOT EXISTS t1( -> username VARCHAR(20), -> age TINYINT UNSIGNED, -> salary FLOAT(8,2) UNSIGNED -> );
mysql> SHOW TABLES;
mysql> SHOW TABLES FROM mysql;
mysql> SHOW COLUMNS FROM t1;
3.MySQL记录的插入与查找
INSERT [INTO] tbl_name [(col_name,...0] VALUES (val,...)
mysql> INSERT t1 VALUES('Tom',25,7863.25);
SELECT expr,... FROM tbl_name
SELECT * FROM t1;
4.MySQL空值与非空
mysql> USE t2; mysql> CREATE TABLE IF NOT EXISTS tb2( -> username VARCHAR(20) NOT NULL, -> age TINYINT UNSIGNED NULL, -> salary FLOAT(8,2) UNSIGNED -> );
5.MySQL主键约束
6.MySQL自动编号 AUTO_INCREMENT
mysql> CREATE TABLE tb3( -> id SMALLINT UNSIGNED AUTO_INCREMENT PRIMARY KEY, -> username VARCHAR(30) NOT NULL -> ); mysql> SHOW COLUMNS FROM tb3;+----------+----------------------+------+-----+---------+----------------+| Field | Type | Null | Key | Default | Extra |+----------+----------------------+------+-----+---------+----------------+| id | smallint(5) unsigned | NO | PRI | NULL | auto_increment || username | varchar(30) | NO | | NULL | |+----------+----------------------+------+-----+---------+----------------+
mysql> INSERT tb3(username) VALUES('Tom') mysql> INSERT tb3(username) VALUES('John') mysql> INSERT tb3(username) VALUES('Rose') mysql> INSERT tb3(username) VALUES('Dimitar') mysql> SELECT * FROM tb3;+----+----------+| id | username |+----+----------+| 1 | Tom || 2 | John || 3 | Rose || 4 | Dimitar |+----+----------+
7.MySQL唯一约束 UNIQUE KEY
mysql> CREATE TABLE tb4( -> id SMALLINT AUTO_INCREMENT PRIMARY KEY, -> username VARCHAR(20) NOT NULL UNIQUE KEY, -> age TINYINT UNSIGNED -> ); mysql> SHOW COLUMNS FROM tb4;+----------+---------------------+------+-----+---------+----------------+| Field | Type | Null | Key | Default | Extra |+----------+---------------------+------+-----+---------+----------------+| id | smallint(6) | NO | PRI | NULL | auto_increment || username | varchar(20) | NO | UNI | NULL | || age | tinyint(3) unsigned | YES | | NULL | |+----------+---------------------+------+-----+---------+----------------+
mysql> INSERT tb4(username,age) VALUES('Tom',25); Query OK, 1 row affected (0.00 sec)
mysql> INSERT tb4(username,age) VALUES('Tom',23); ERROR 1062 (23000): Duplicate entry 'Tom' for key 'username'
8.MySQL默认约束
mysql> CREATE TABLE tb6( -> id SMALLINT UNSIGNED AUTO_INCREMENT PRIMARY KEY, -> username VARCHAR(20) NOT NULL UNIQUE KEY, -> sex ENUM('1','2','3') DEFAULT '3' -> ); mysql> SHOW COLUMNS FROM tb6;+----------+----------------------+------+-----+---------+----------------+| Field | Type | Null | Key | Default | Extra |+----------+----------------------+------+-----+---------+----------------+| id | smallint(5) unsigned | NO | PRI | NULL | auto_increment || username | varchar(20) | NO | UNI | NULL | || sex | enum('1','2','3') | YES | | 3 | |+----------+----------------------+------+-----+---------+----------------+
mysql> INSERT tb6(username) VALUES('Tom'); mysql> INSERT tb6(username) VALUES('John'); mysql> INSERT tb6(username) VALUES('Mimiter');
mysql> SELECT * FROM tb6; +----+----------+------+| id | username | sex |+----+----------+------+| 1 | Tom | 3 || 2 | John | 3 || 3 | Mimiter | 3 |+----+----------+------+
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。