本文共 5497 字,大约阅读时间需要 18 分钟。
也可以直接通过mkdir在数据目录创建目录, 则MySQL会识别为一个数据库,并在执行show databases命令时可以看 到
mysql> create database test3; -- 创建数据库成功 Query OK, 1 row affected (0.00 sec)mysql> create database test3; -- 再次创建数据库失败ERROR 1007 (HY000): Can't create database 'test3'; database existsmysql> create database if not exists test3; -- 语句执行成功 Query OK, 1 row affected, 1 warning (0.00 sec)mysql> use test3; -- 切换到test3数据库
if not exists表示当相同的表名存在时,则不执行此创建语句,避 免语句执行错误
create table students2(sid int,sname varchar(10));create table test3.students2(sid int,sname varchar(10)); --在test3这个数据库下创建students2表create table if not exists students2(sid int,sname varchar(10));
表示创建的是临时表,临时表仅对本链接可见,另外的数据库链接不可见, 当本链接断开时,临时表也自动被drop掉
mysql> create temporary table temp1(sid int,sname varchar(10)); Query OK, 0 rows affected (0.00 sec)mysql> insert into temp1 values(1,'a'); Query OK, 1 row affected (0.00 sec)mysql> select * from temp1;+------+-------+|sid |sname|+------+-------+| 1 |a |+------+-------+1 row in set (0.00 sec)
另一个数据库链接执行相同的查询语句查不到数据
mysql> select * from temp1;ERROR 1146 (42S02): Table 'test.temp1' doesn't exist -- 本数据库链接断开后再链接,临时表也不存在mysql> select * from temp1;ERROR 1146 (42S02): Table 'test.temp1' doesn't exist
表示基于另外一个表的定义复制一个新的空表,空表上的字段属性和索 引都和原表相同
mysql>create table students_copy like students; Query OK, 0 rows affected (0.01 sec) mysql> show create table students_copy;CREATE TABLE `students_copy` ( `sid` int(11) DEFAULT NULL, `sname` varchar(20) DEFAULT NULL,`sex` int(11) DEFAULT NULL, UNIQUE KEY `idx_st_sid` (`sid`),KEY `idx_st_union` (`sname`,`sex`) )ENGINE=InnoDB DEFAULT CHAR SET=latin1
mysql> create table students_copy2 as select * from students;Query OK, 0 rows affected (0.03 sec)Records: 0 Duplicates: 0 Warnings: 0mysql> desc students_copy2;+-------+-------------+------+-----+---------+-------+| Field | Type | Null | Key | Default | Extra |+-------+-------------+------+-----+---------+-------+| sid | int(11) | YES | | NULL | || sname | varchar(20) | YES | | NULL| sex | int(11) | YES | | NULL | |+-------+-------------+------+-----+---------+-------+
mysql> create table students2(sid int not null,sname varchar(10)); Query OK, 0 rows affected (0.00 sec)mysql> insert into students2(sname) values('eee');ERROR 1364 (HY000): Field 'sid' doesn't have a default value
create table students3(sid int,sname varchar(10),gender int default 0); insert into students3 values(1,'a',default);insert into students3(sid,sname) values(2,'b');mysql> select * from students3;+------+-------+--------+|sid |sname |gender | +------+-------+--------+ |1|a|0| |2|b|0|
一个表中只容许有一个自增字段,且该字段必须有key属性,不能含有default属性,且插入负值会被当成很大的正数
mysql>create table students4(sid int auto_increment,sname varchar(10));ERROR 1075 (42000): Incorrect table definition; there can be only one auto column and it must be defined as akeymysql>create table students4(sid int primary key auto_increment,sname varchar(10));Query OK, 0 rows affected (0.01 sec)mysql> insert into students4(sname) values('a');Query OK, 1 row affected (0.00 sec)mysql> insert into students4(sid,sname) values(3,'b');Query OK, 1 row affected (0.01 sec)mysql> insert into students4(sname) values('c');Query OK, 1 row affected (0.00 sec) mysql> select * from students4;+-----+-------+|sid |sname | +-----+-------+ |1|a| |3|b| |4|c|
表示为主键、唯一键、外键等约束条件命名,如果没有命名则MySQL会默认给一个
一个表中只能有一个主键,主键可以包含一个或多个字段
表示索引字段
表示该字段为唯一属性字段,且允许包含多个null值
表示该字段为外键字段
CREATE TABLE `gender` (gender_id int(11) NOT NULL,name varchar(10) DEFAULT NULL,PRIMARY KEY (gender_id) );create table students5(sid int not null primary key auto_increment,sname varchar(10) unique,gender int,constraint for_1 foreign key (gender) references gender(gender_id));
mysql> create database course;mysql> use course;mysql> create table dept(id int auto_increment primary key,dept_name varchar(64));mysql> create table students(sid int auto_increment primary key,sname varchar(54),gender varchar(12),dept_id int not null,constraint for_1 foreign key(dept_id) references dept(id));mysql> create table teacher(id int auto_increment primary key,name varchar(64),dept_id int not null, constraint for_2 foreign key(dept_id) references dept(id)); mysql> create table course(id int auto_increment primary key,course_name varchar(64),teacher_id int,constraint for_3 foreign key(teacher_id) references teacher(id));
转载地址:http://fjgda.baihongyu.com/