1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
| #创建新的数据库 CREATE DATABASES mynews; #查看数据库 SHOW DATABASE; #使用mynews库 USE mynews; #创建一个名为tb_users表 CREATE TABLE `tb_users`( #id非空自增 `id` int(10) NOT NULL AUTO_INCREMENT, #用户名非空唯一 `username` VARCHAR(50) NOT NULL UNIQUE, `password` VARCHAR(50) NOT NULL, `email` VARCHAR(50) NULL UNIQUE, `nickname` VARCHAR(50) NOT NULL, `safepoint` VARCHAR(50) NULL, #头像默认 `avatarimg` VARCHAR(100) DEFAULT '/images/default_avatar.jpg', #时间 `reg_time` TIMESTAMP DEFAULT CURRENT_TIMESTAMP(), `lastlogin_time` TIMESTAMP DEFAULT CURRENT_TIMESTAMP(), `lastlogin_ip` VARCHAR(255) NULL, PRIMARY KEY(`id`) ); #向tb_users表插入一条数据 INSERT INTO `mynews`.`tb_users`(`username`, `password`, `email`, `nickname`) VALUES ('zs', 'zs123', 'zs123@icq.com', 'zs123')
#创建一个名为tb_news表 create table `tb_news`( `id` int(0) not null auto_increment, `title` varchar(255) not null, `contents` text not null, `author` varchar(50) not null, `new_img` varchar(100) null, `add_time` timestamp default current_timestamp(), `hot` int(0) default 0, primary key(`id`) ); #向tb_news表插入数据 INSERT INTO `mynews`.`tb_news`(`title`, `contents`, `author`) VALUES ('打完宽面', '吃面!吃面!', 'zs'); INSERT INTO `mynews`.`tb_news`(`title`, `contents`, `author`) VALUES ('鸡你太美', '太美!太美!', 'ww');
#创建一个名为tb_comment表 create table `tb_comment`( `id` int(0) not null auto_increment, `new_id` int(0) not null, `comment_content` text not null, `comment_user` varchar(50) not null, `comment_time` timestamp default current_timestamp(), primary key(`id`) ); #向tb_comment表插入数据 INSERT INTO `mynews`.`tb_comment`(`new_id`, `comment_content`, `comment_user`) VALUES ('1', '吴亦凡', 'ww'); INSERT INTO `mynews`.`tb_comment`(`new_id`, `comment_content`, `comment_user`) VALUES ('2', '坤坤', 'zs'); #修改数据 UPDATE `mynews`.`tb_news` SET `author` = 'ls' WHERE `id` = 1; UPDATE `tb_comment` SET `comment_content` = '蔡徐坤' WHERE `id` = 2;
ALTER TABLE `mynews`.`tb_users` ADD UNIQUE (`username`);
|