1.查看表结构
mysql中能够用describe查看表的基本定义,包括字段名,字段数据类型,主键,默认值等,describe能够用desc来简写,效果是同样的,语法结构为:
desc table_name;
eg:mysql
- desc host;
mysql> desc host;
+-----------------------+---------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------------------+---------------+------+-----+---------+-------+
| Host | char(60) | NO | PRI | | |
| Db | char(64) | NO | PRI | | |
| Select_priv | enum('N','Y') | NO | | N | |
| Insert_priv | enum('N','Y') | NO | | N | |
| Update_priv | enum('N','Y') | NO | | N | |
| Delete_priv | enum('N','Y') | NO | | N | |
| Create_priv | enum('N','Y') | NO | | N | |
| Drop_priv | enum('N','Y') | NO | | N | |
| Grant_priv | enum('N','Y') | NO | | N | |
| References_priv | enum('N','Y') | NO | | N | |
| Index_priv | enum('N','Y') | NO | | N | |
| Alter_priv | enum('N','Y') | NO | | N | |
| Create_tmp_table_priv | enum('N','Y') | NO | | N | |
| Lock_tables_priv | enum('N','Y') | NO | | N | |
| Create_view_priv | enum('N','Y') | NO | | N | |
| Show_view_priv | enum('N','Y') | NO | | N | |
| Create_routine_priv | enum('N','Y') | NO | | N | |
| Alter_routine_priv | enum('N','Y') | NO | | N | |
| Execute_priv | enum('N','Y') | NO | | N | |
| Trigger_priv | enum('N','Y') | NO | | N | |
+-----------------------+---------------+------+-----+---------+-------+
20 rows in set (0.00 sec)sql
查看表结构的详细语句:
show create tabel table_name;
eg:数据库
- show create table host;
2.建立数据表:
看到表的基本结构,咱们就能够根据需求建立相应的表,其语法形式为:
create table table_name (属性名 数据类型 [完整性约束条件],
.
.
.
属性名 数据类型 [完整性约束条件]);
其中表名不能为sql语言的关键字。
eg:ide
- create table example0(id int
- ,name varchar(20)
- ,sex boolean);
mysql> desc example0;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
| name | varchar(20) | YES | | NULL | |
| sex | tinyint(1) | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+
3 rows in set (0.00 sec)spa
表的完整性约束条件
ci
约束条件 | 说明 |
primary key | 主键 |
foreign key | 外键 |
not null | 非空 |
auto_increment | 惟一 |
default | 设置 |
设置表的单字段主键,语法以下:
属性名 数据类型 primary key
eg:在example1中,设置stu_id为主键rem
- create table example1(stu_id int primary key
- ,stu_name varchar(20)
- ,stu_sex boolean);
mysql> desc example1;
+----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| stu_id | int(11) | NO | PRI | NULL | |
| stu_name | varchar(20) | YES | | NULL | |
| stu_sex | tinyint(1) | YES | | NULL | |
+----------+-------------+------+-----+---------+-------+
3 rows in set (0.00 sec)string
设置表的多字段主键:
primary key (属性名1,属性名2,...属性名n)
eg:
it
- create table example2(stu_id int,
- course_id int,
- grade float,
- primary key(stu_id,course_id));
mysql> create table example2(stu_id int, course_id int, grade float, primary key(stu_id,course_id));
Query OK, 0 rows affected (0.00 sec)table
设置表的外键
原则:必须依赖于数据库中已经存在的父表的主键。
语法:
- constraint foregin_key_alias foreign key (attribute1.1 ...,attribute1.n)
- references table_name(attribute2.1...,attribute2.n)
eg:在example3中设置stu_id,course_id为外键,关联example1中的主键stu_id,course_id:
- create table example3(id int primary key,
- stu_id int,
- course_id int,
- constraint c_fk foreign key(stu_id,course_id)
- references example1(stu_id,course_id));
设置表的非空约束:
attribute data_type not null
eg:设置example4表中的id和name为非空约束
- create table example4(id int primary key not null,
- name varchar(20) not null,
- stu_id int,
- constraint d_fk foreign key(stu_id)
- references example1(stu_id));
设置表的惟一性约束:
attribute data_type unique
eg:在example5表中设置id,stu_id 惟一性约束
- create table example5(id int primary key,
- stu_id int unique,
- name varchar(20) not null);
设置表的属性值自动增长,其主要做用是为表中插入的新记录自动生成惟一的id。一个表只有一个字段能使用auto_increment约束,且必须为主键的一部分。
attribute data_type auto_increment
eg:在example6表中,这是id的值自动增长
- create table example6(id int primary key auto_increment,
- stu_id int unique,
- name varchar(20) not null);
设置表的默认值
attribute data_type default default_value
eg:
- create table example7(id int primary key auto_increment,
- stu_id int unique,
- name varchar(20) not null,
- english varchar(20) default 'zero'
- ,math float default 0,
- computer float default 0);